【问题标题】:Nats which pattern to useNats 使用哪种模式
【发布时间】:2017-06-11 18:46:15
【问题描述】:

我问了一个问题: Microservice Architecture dependency 使用集群微服务架构时使用哪种模式。

我收到的答案是点对点应该可以工作,但是在阅读时: https://nats.io/documentation/concepts/nats-req-rep/ 感觉就像所有订阅者都收到了事件(并因此处理它),但只有一个订阅者响应。当放置将触发库存微服务订阅的更新库存事件的订单事件时,这将不起作用(如链接中的示例),即它不会在集群环境中工作,因为库存将被更新相同的次数微服务实例的数量。

如何使用 Nats 解决这种情况?

提前致谢!

【问题讨论】:

    标签: nats.io nats-streaming-server


    【解决方案1】:

    NATS.io 使用队列组支持此功能:

    具有相同队列名称的所有订阅将形成一个队列组。 每条消息将仅传递给每个队列组的一个订阅者, 使用排队语义。您可以拥有任意数量的队列组。 普通订阅者将继续按预期工作。

    使用队列组连接您的服务(示例为 node.js):

    https://github.com/nats-io/node-nats#queue-groups

    nats.subscribe('foo', {'queue':'job.workers'}, function() {
        received += 1;
    });
    

    然后客户端将使用库提供的请求方法:

    https://github.com/nats-io/node-nats#basic-usage

    // Request for single response with timeout.
    nats.requestOne('help', null, {}, 1000, function(response) {
      // `NATS` is the library.
      if(response.code && response.code === NATS.REQ_TIMEOUT) {
        console.log('Request for help timed out.');
        return;
      }
      console.log('Got a response for help: ' + response);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      相关资源
      最近更新 更多