【发布时间】:2021-11-30 01:04:48
【问题描述】:
我正在尝试使用节点模块 @google-cloud/pubsub 批量发布消息。我的批量发布代码如下所示。
const { PubSub } = require("@google-cloud/pubsub");
const grpc = require("grpc");
const createPublishEventsInBatch = (fastify, topic) => {
const pubSub = new PubSub({ grpc });
const batchPublisher = pubSub.topic(topic, {
batching: {
maxMessages: 100,
maxMilliseconds: 1000
}
});
return (logTrace, data, eventInfo, version) => {
const { entityType, eventType } = eventInfo;
fastify.log.debug({
logTrace,
eventType: eventType,
data,
message: `Publishing batch events for ${entityType}`
});
const event = createEvent(data, entityType, eventType, logTrace, version);
batchPublisher.publish(Buffer.from(JSON.stringify(event)));
fastify.log.debug({
traceHeaders: logTrace,
tenant: data.tenant,
message: "Event publish completed",
data
});
};
};
Pubsub 和 gRPC 版本如下。
"@google-cloud/pubsub": "^2.18.1",
"grpc": "^1.24.11"
当我使用上述代码发布消息时,出现以下错误。
(node:6) UnhandledPromiseRejectionWarning: TypeError: Channel credentials must be a ChannelCredentials object
at new ChannelImplementation (/app/node_modules/@grpc/grpc-js/build/src/channel.js:75:19)
at new Client (/app/node_modules/@grpc/grpc-js/build/src/client.js:61:36)
at new ServiceClientImpl (/app/node_modules/@grpc/grpc-js/build/src/make-client.js:58:5)
at GrpcClient.createStub (/app/node_modules/google-gax/build/src/grpc.js:334:22)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
我只在我的生产环境和暂存环境以及我所有的较低环境中看到这个问题,这工作正常。有人可以指导我解决这个问题。
【问题讨论】:
-
您能否尝试GitHub issue 中提到的这种解决方法,即尝试在您的生产环境中重新安装模块。
标签: node.js grpc google-cloud-pubsub fastify