微服务 - Spring AMQP/RabbitMQ,微服务之间的异步调用最佳模式
Microservices - Spring AMQP/RabbitMQ, async calls between microservices best pattern
我有三个微服务 - 服务 A、B 和 C。
服务 A 应该异步调用 B 和 C,A 应该构建基于 B 和 C 响应的响应。
我正在使用 Rabbit MQ 进行异步 ipc。
尝试使用 RabbitTemplate 的 convertSendAndRecieve 和 direct-replyTo 选项来使用,这使得当前处理线程 wait/block 完成异步调用,从而使其同步。
我不想使用 convertAndSend 并让服务 A 侦听回复队列并根据相关 ID 进行处理,因为回复队列中会有数千个响应并根据相关 ID 映射消息结果表现不佳。
为每个会话创建单独的队列不是一种选择,因为它有自己的警告(从所有集群获得对新队列创建的确认也会影响性能)
抱歉,如果这个问题之前已经解决了,我在网上找不到太多帮助。任何帮助,将不胜感激。
有 AsyncRabbitTemplate
为了您的目的不要阻止来电者,直到回复:https://docs.spring.io/spring-amqp/docs/2.0.0.RELEASE/reference/html/_reference.html#async-template:
Version 1.6 introduced the AsyncRabbitTemplate
. This has similar sendAndReceive
(and convertSendAndReceive
) methods to those on the AmqpTemplate
but instead of blocking, they return a ListenableFuture
.
RabbitConverterFuture<String> future = this.template.convertSendAndReceive("foo");
future.addCallback(new ListenableFutureCallback<String>() {
@Override
public void onSuccess(String result) {
...
}
@Override
public void onFailure(Throwable ex) {
...
}
});
我有三个微服务 - 服务 A、B 和 C。 服务 A 应该异步调用 B 和 C,A 应该构建基于 B 和 C 响应的响应。
我正在使用 Rabbit MQ 进行异步 ipc。
尝试使用 RabbitTemplate 的 convertSendAndRecieve 和 direct-replyTo 选项来使用,这使得当前处理线程 wait/block 完成异步调用,从而使其同步。
我不想使用 convertAndSend 并让服务 A 侦听回复队列并根据相关 ID 进行处理,因为回复队列中会有数千个响应并根据相关 ID 映射消息结果表现不佳。
为每个会话创建单独的队列不是一种选择,因为它有自己的警告(从所有集群获得对新队列创建的确认也会影响性能)
抱歉,如果这个问题之前已经解决了,我在网上找不到太多帮助。任何帮助,将不胜感激。
有 AsyncRabbitTemplate
为了您的目的不要阻止来电者,直到回复:https://docs.spring.io/spring-amqp/docs/2.0.0.RELEASE/reference/html/_reference.html#async-template:
Version 1.6 introduced the
AsyncRabbitTemplate
. This has similarsendAndReceive
(andconvertSendAndReceive
) methods to those on theAmqpTemplate
but instead of blocking, they return aListenableFuture
.
RabbitConverterFuture<String> future = this.template.convertSendAndReceive("foo");
future.addCallback(new ListenableFutureCallback<String>() {
@Override
public void onSuccess(String result) {
...
}
@Override
public void onFailure(Throwable ex) {
...
}
});