繁体   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