【问题标题】:Using Google Cloud Functions API as Service Discovery使用 Google Cloud Functions API 作为服务发现
【发布时间】:2018-07-11 00:32:02
【问题描述】:

我们正在开发一个采用 Google Cloud Functions 的基于微服务的架构。

我们已经开发了一些功能,并希望实现一项发现服务。该发现服务将用于确定特定功能是否存在并且是否可操作。

服务发现本身就是一个云功能。它向以下 API 发出休息请求,并使用函数模拟器和默认应用程序凭据成功进行本地开发。

Google 为此提供了一个 API [https://cloud.google.com/functions/docs/reference/rest/v1beta2/projects.locations.functions/get][1]

部署到生产环境时,我们会收到: { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }

云功能是无状态的,因此无法使用我可以看到的服务帐户。如何验证云函数以调用函数 api 以确定函数是否可用?

以下是我们如何在本地开发环境中完成此操作:

var options = {
    method: 'get',
    uri: `https://cloudfunctions.googleapis.com/v1beta2/projects/${config.PROJECT_ID}/locations/${config.LOCATION_ID}/functions/${functionName}`
  }

  console.log (options.uri);
  request(options, function (err, res, body) {
    if (!err && res.status === 200) {
      if(typeof res.body.httpsTrigger.url !== undefined) {
        console.log('found a function');
        return cb(false, res.body.httpsTrigger.url);
      }
      else {
        console.log('no function found, looking for static content');
        return cb(true, `Service doesn't exist, returned ${res.status}`)
      }
    }
    else {
      console.log('no function found, looking for static content'); 
      return cb(true, `Service doesn't exist, returned ${res.status}`);
    }
  });

【问题讨论】:

    标签: google-cloud-platform microservices google-cloud-functions


    【解决方案1】:

    所以我终于想出了如何做到这一点。这有点骇人听闻:。

    1. 下载服务帐户的 JSON 密钥文件。
    2. 将 JSON 文件添加到函数源。
    3. 安装 google-auth-library NPM 模块。
    4. 修改请求以使用来自 google-auth-library 的客户端

      const keys = require('./VirtualAssistantCred.json');
      

      const {auth} = require('google-auth-library'); 常量客户端 = auth.fromJSON(keys); client.scopes = ['https://www.googleapis.com/auth/cloud-platform']; client.authorize().then(function () { 常量 url = https://cloudfunctions.googleapis.com/v1beta2/projects/${config.PROJECT_ID}/locations/${config.LOCATION_ID}/functions/${functionName}; client.request({url}, function(err, res) { 如果(错误){ 控制台.log(Error: ${err}); 返回 cb(true, Service, ${functionName} doesn't exist, returned ${res}) } 别的 { 控制台.log(RESPONSE: ${JSON.stringify(res.data)}); 控制台.log(Found Service at ${res.data.httpsTrigger.url}); 返回 cb(false, res.data.httpsTrigger.url); } }); } );

    【讨论】:

      【解决方案2】:

      您提到的 API 包含方法: projects.locations.functions.get ,需要以下 OAuth 范围之一:

      https://www.googleapis.com/auth/cloudfunctions
      
      https://www.googleapis.com/auth/cloud-platform
      

      您可以查看Authentication in HTTP Cloud Functions 在线文档。

      【讨论】:

      • 因此,如果我将上述范围之一添加到运行该功能的服务帐户,它应该能够调用 API?
      • 对此感到非常紧张。因此,此云功能正在调用 API 以查看其他云功能的可用性。无论是使用休息请求还是 api 库,都需要一个 OAuth2 令牌。服务帐户需要密钥文件。 Cloud Functions 没有存储空间,但也许可以选择 Cloud Storage?
      猜你喜欢
      • 2020-03-19
      • 2021-02-04
      • 2020-03-15
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      • 2020-09-17
      • 2019-12-30
      相关资源
      最近更新 更多