关于nats.io:使用哪种模式

Nats which pattern to use

我问了一个问题:
微服务架构依赖关系:使用集群微服务架构时使用哪种模式。

我收到的回答是点对点应该可以工作,但是在阅读时:
https://nats.io/documentation/concepts/nats-req-rep/
感觉像所有订户都接收到事件(并因此而处理),但是只有一个订户可以响应。 当放置将触发库存微服务订阅的update-inventory-event的订单事件时(如链接中的示例),这将不起作用,即由于库存将无法在集群环境中工作,因此更新时间与 微服务实例的数量。

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

提前致谢!


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

All subscriptions with the same queue name will form a queue group.
Each message will be delivered to only one subscriber per queue group,
using queuing semantics. You can have as many queue groups as you wish.
Normal subscribers will continue to work as expected.

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

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

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

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

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

1
2
3
4
5
6
7
8
9
// 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);
});