简体   繁体   English

如何构建多区域 firebase 应用程序?

[英]How to structure a multi-region firebase app?

Currently, I have this structure:目前,我有这样的结构:

- A couple of functions in a single region
- A firestore database in a single region

A while ago, the whole region went down, which caused issues to the users.前段时间,整个地区都宕机了,给用户带来了问题。

I want to change the structure to be in multiple regions, both the database and the functions.我想将结构更改为多个区域,包括数据库和函数。

How is that done in the firebase world?在 firebase 世界中,这是如何完成的?

Keeping in mind that when I deploy the functions in multiple regions, there should be some way to route requests to the function in the region close to the user.请记住,当我在多个区域部署功能时,应该有某种方法可以将请求路由到靠近用户的区域中的 function。

Once the database is created, the region can't be changed.数据库创建后,区域无法更改。 You'll have to create a new project with the new region.您必须使用新区域创建一个新项目。

By default, functions run in the us-central1 region.If you have a function that is currently in the default functions region of us-central1, and you want to migrate it to asia-northeast1, you need to first modify your source code to rename the function and revise the region.默认情况下,函数运行在us-central1区域。如果你有一个function目前在us-central1的默认函数区域,你想把它迁移到asia-northeast1,你需要先修改你的源代码重命名 function 并修改区域。 You can refer to this document你可以参考这篇文档

// before
const functions = require('firebase-functions');

exports.webhook = functions
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

// after
const functions = require('firebase-functions');

exports.webhookAsia = functions
    .region('asia-northeast1')
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

Then deploy by running:然后通过运行部署:

firebase deploy --only functions:newfunction 

After renaming the function with the new region, now there are two identical functions running in us-central1 and asia-northeast1.将 function 重命名为新区域后,现在在 us-central1 和 asia-northeast1 中运行着两个相同的函数。

You can also check this stackoverflow link1 & link2 which might help您还可以查看此 stackoverflow link1 & link2这可能有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM