[英]In Springboot microservices api, how to call a another server using AMQP queue, get response from the another service queue, send the response
We are writing the micro-services platform. 我们正在编写微服务平台。 We have a server A written in spring boot which is exposing API to the outside world.
我们有一个用Spring Boot编写的服务器A,它将API暴露给外界。
We have another spring-boot microservice B which needs to be called from A. 我们还有另一个需要从A调用的spring-boot微服务B。
Example: 例:
We are having an endpoint /createOrder in service A. 我们在服务A中有一个端点/ createOrder。
When we call this, A's controller gets called, need to send a message to server B using AMQP JMS integration, B receives the queue process the controller, send the message back to Server A so that response can be sent to the API request of /create order. 当我们调用此方法时,A的控制器被调用,需要使用AMQP JMS集成将消息发送到服务器B,B接收到控制器的队列进程,然后将消息发送回Server A,以便可以将响应发送到/的API请求创建订单。
---->/createorder-->A Server --->A sends a message queue to B's server ---> B server process it ---> Sends a message to A --->A responds to the request. ----> / createorder-> A服务器---> A向B的服务器发送消息队列---> B服务器对其进行处理--->向A发送消息---> A响应请求。
In this process how to hold the request in Server A and wait for a response from Server B . 在此过程中,如何将请求保留在服务器A中并等待服务器B的响应。
Sounds like you need to make yourself familiar with Enterprise Integration Patterns and it implementation with Spring Integration . 听起来您需要熟悉Enterprise Integration Patterns及其通过Spring Integration的实现。 There is a pattern like gateway and this framework has an implementation of it for JMS outbound requests/replies: https://docs.spring.io/spring-integration/docs/current/reference/html/jms.html#jms-outbound-gateway .
有一个类似网关的模式,此框架针对JMS出站请求/回复实现了它的实现: https : //docs.spring.io/spring-integration/docs/current/reference/html/jms.html#jms-outbound -gateway 。
On the other hand Spring AMQP doesn't support AMQP 1.0 protocol, which is more likely can be processed by regular JMS API. 另一方面,Spring AMQP不支持AMQP 1.0协议,更有可能由常规JMS API处理。
The JmsTemplate
from Spring JMS also provides for us an API like: Spring JMS的
JmsTemplate
还为我们提供了如下API:
/**
* Send a message and receive the reply from the specified destination. The
* {@link MessageCreator} callback creates the message given a Session. A temporary
* queue is created as part of this operation and is set in the {@code JMSReplyTO}
* header of the message.
* @param destinationName the name of the destination to send this message to
* (to be resolved to an actual destination by a DestinationResolver)
* @param messageCreator callback to create a message
* @return the reply, possibly {@code null} if the message could not be received,
* for example due to a timeout
* @throws JmsException checked JMSException converted to unchecked
* @since 4.1
*/
@Nullable
Message sendAndReceive(String destinationName, MessageCreator messageCreator) throws JmsException;
So, you may consider to use this as well, if Spring Integration is too hard to bring to your project right away. 因此,如果Spring Integration很难立即带到您的项目中,您也可以考虑使用它。
If you are confusing AMQP 0.9 with JMS, then you really can stay with Spring AMQP project and its RabbitTemplate.sendAndReceive()
. 如果您将AMQP 0.9与JMS混淆,那么您真的可以继续使用Spring AMQP项目及其
RabbitTemplate.sendAndReceive()
。
To wait for the response, you could try to use async rabbit. 要等待响应,您可以尝试使用异步Rabbit。 Here's a tutorial from baeldung: https://www.baeldung.com/spring-amqp-reactive
这是来自baeldung的教程: https ://www.baeldung.com/spring-amqp-reactive
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.