簡體   English   中英

在Springboot微服務api中,如何使用AMQP隊列調用另一個服務器,如何從另一個服務隊列獲取響應,發送響應

[英]In Springboot microservices api, how to call a another server using AMQP queue, get response from the another service queue, send the response

我們正在編寫微服務平台。 我們有一個用Spring Boot編寫的服務器A,它將API暴露給外界。

我們還有另一個需要從A調用的spring-boot微服務B。

例:

我們在服務A中有一個端點/ createOrder。

當我們調用此方法時,A的控制器被調用,需要使用AMQP JMS集成將消息發送到服務器B,B接收到控制器的隊列進程,然后將消息發送回Server A,以便可以將響應發送到/的API請求創建訂單。

----> / createorder-> A服務器---> A向B的服務器發送消息隊列---> B服務器對其進行處理--->向A發送消息---> A響應請求。

在此過程中,如何將請求保留在服務器A中並等待服務器B的響應。

聽起來您需要熟悉Enterprise Integration Patterns及其通過Spring Integration的實現。 有一個類似網關的模式,此框架針對JMS出站請求/回復實現了它的實現: https : //docs.spring.io/spring-integration/docs/current/reference/html/jms.html#jms-outbound -gateway

另一方面,Spring AMQP不支持AMQP 1.0協議,更有可能由常規JMS API處理。

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;

因此,如果Spring Integration很難立即帶到您的項目中,您也可以考慮使用它。

如果您將AMQP 0.9與JMS混淆,那么您真的可以繼續使用Spring AMQP項目及其RabbitTemplate.sendAndReceive()

要等待響應,您可以嘗試使用異步Rabbit。 這是來自baeldung的教程: https ://www.baeldung.com/spring-amqp-reactive

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM