如何指定谷歌云函数的区域?

问题描述 投票:0回答:4

我目前正在使用 Google Cloud Function 构建我的 restful API。 但是,我发现它很慢,因为我的 Google-Cloud-Function 服务器位于“us-central”,而我的服务位于亚洲。

我尝试将我的 Google 项目的默认区域更改为“asia-west-1”并重新启动云功能——我按照 here 中列出的步骤进行操作——但不幸的是,它仍在“us-central”中。 我怎样才能改变函数的区域?

google-cloud-platform google-cloud-functions gcloud latency region
4个回答
9
投票

没有“asia-west-1”这样的区域

谷歌云平台上没有“asia-west-1”这样的区域。在撰写本文时,GCP 在亚洲提供以下区域

  • asia-east1
  • asia-northeast1
  • asia-south1
  • asia-southeast1

然而,

asia-northeast1
是目前亚洲唯一可用于 Google Cloud Functions 的 GCP 区域:

$ gcloud functions regions list
NAME
projects/xxxxxxxx/locations/europe-west1
projects/xxxxxxxx/locations/us-east1
projects/xxxxxxxx/locations/us-central1
projects/xxxxxxxx/locations/asia-northeast1

如何指定谷歌云函数的区域

使用谷歌云控制台

区域设置容易错过。它实际上隐藏在一个名为“更多”的手风琴后面:

点击它会显示一个Region下拉菜单(以及其他高级设置):

使用
gcloud

只需使用

--region
标志指定四个可用区域之一。

最小的工作示例

假设你有

$ tree .
.
└── index.js

0 directories, 1 file
$ cat index.js 
exports.hello = (req, res) => {

  res.send(`Hello World!`); 

}

然后运行

gcloud functions deploy hello --region asia-northeast1 --trigger-http

应该部署功能

hello
到区域
asia-northeast1


6
投票

Google Cloud Functions 目前似乎只在 us-central1 区域可用。如果我转到“创建函数”(https://console.cloud.google.com/functions/add),Region 下拉菜单只有一个选择,它是 us-central1。


5
投票

如果您使用的是 Firebase SDK:

取自文档-https://firebase.google.com/docs/functions/manage-functions#modify

// 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");
    });

0
投票

很难学习它,如果您没有明确定义函数的区域,它会将区域设置为

us-central1
,即使您的 firestore 和 firebase 项目已经设置为使用另一个区域: export const 通知用户 =

functions.firestore.document("tasks/{docId}").onUpdate(

要解决此问题,您需要在 index.ts 文件中为以下每个函数指定区域并再次部署它:

export const notifyUsers =
functions.region('europe-west1').firestore.document("tasks/{docId}").onUpdate(

部署后,您可以查看 firebase 控制台的 Functions 选项卡,您的函数名称现在显示更新的区域。现在,如果在部署过程中询问是/否问题时您还没有这样做,您可以删除具有旧区域值的那些。

© www.soinside.com 2019 - 2024. All rights reserved.