【发布时间】: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