Google Cloud Pub / Sub上的重复消息ID

时间:2018-03-25 13:15:20

标签: google-cloud-platform google-cloud-pubsub

我在Google Cloud Pub / Sub上收到了重复的MessageID。请注意,我的有效负载(即数据字段)在测试期间是相同的,但理想情况下不应导致重复的MessageID。请让我知道下面的代码有什么问题:

const publish = (topicName, payload) => {

    const dataBuffer = Buffer.from(JSON.stringify(payload));

    return new Promise((resolve, reject) => {

        pubsub
        .topic(topicName)
        .publisher()
        .publish(dataBuffer)
        .then(result => {
            const messageId = result[0];
            console.log(`${messageId} published`);
            resolve(messageId);
        })
        .catch(err => {
            console.error(err);
            reject(err);
        });

    });

};

1 个答案:

答案 0 :(得分:3)

您的代码将两个不同库中的发布调用混为一谈。您正在使用的publish来自higher-performance Publisher library。在这个库中,你不会得到一系列结果;你得到一个消息ID。因此,当您执行messageId = result[0]时,您将获得messageID的第一个字符。如果您打算打印出result的所有内容,您会发现每次发布调用都不同。

结果数组来自PublisherClient publish method。此方法采用原始PublishRequest并返回响应列表,这是您需要索引所获得的响应的时间。