简体   繁体   中英

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.

We have another spring-boot microservice B which needs to be called from A.

Example:

We are having an endpoint /createOrder in service A.

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.

---->/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.

In this process how to hold the request in Server A and wait for a response from Server B .

Sounds like you need to make yourself familiar with Enterprise Integration Patterns and it implementation with 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 .

On the other hand Spring AMQP doesn't support AMQP 1.0 protocol, which is more likely can be processed by regular JMS API.

The JmsTemplate from Spring JMS also provides for us an API like:

/**
 * 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.

If you are confusing AMQP 0.9 with JMS, then you really can stay with Spring AMQP project and its RabbitTemplate.sendAndReceive() .

To wait for the response, you could try to use async rabbit. Here's a tutorial from baeldung: https://www.baeldung.com/spring-amqp-reactive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM