【发布时间】:2020-05-28 22:09:19
【问题描述】:
我刚刚开始使用 Google Cloud Pub/Sub 作为基于事件的架构的一部分。当我刚刚检查 API 仪表板并看到 StreamingPull 方法几乎 100% 错误时,我有些惊讶。但是,似乎一切都按预期进行。
我在 node.js 应用程序(在服务器上运行)中使用了一个侦听器,在本地运行的 python 脚本中使用另一个侦听器(目前正在开发中,目前不运行)。
我的node.js 实例化如下所示:
const { PubSub } = require("@google-cloud/pubsub");
client = new PubSub({
projectId: conf.GOOGLE_PROJECT_ID,
credentials: {
client_email: conf.GOOGLE_CLIENT_EMAIL,
private_key: conf.GOOGLE_CLIENT_PRIVATE_KEY
},
});
const subscription = client.subscription(subscriptionName);
subscription.on("message", (message) => {
console.log(`Received message ${message.id}:`);
console.log(JSON.parse(message.data));
const parsedMessage = JSON.parse(message.data).message;
if (parsedMessage) {
... do things ...
}
message.ack()
});
const errorHandler = function (error) {
console.error(`ERROR: ${error}`);
throw error;
};
subscription.on("error", errorHandler);
有人能解释一下吗?
【问题讨论】:
标签: node.js google-cloud-platform google-cloud-pubsub