【问题标题】:In Springboot microservices api, how to call a another server using AMQP queue, get response from the another service queue, send the response在 Springboot 微服务 api 中,如何使用 AMQP 队列调用另一个服务器,从另一个服务队列获取响应,发送响应
【发布时间】:2019-02-08 13:06:05
【问题描述】:

我们正在编写微服务平台。我们有一个用 Spring Boot 编写的服务器 A,它将 API 暴露给外界。

我们还有另一个 spring-boot 微服务 B 需要从 A 调用。

示例:

我们在服务 A 中有一个端点 /createOrder。

当我们调用这个时,A的控制器被调用,需要使用AMQP JMS集成向服务器B发送消息,B接收队列处理控制器,将消息发送回服务器A以便可以将响应发送到API /创建订单的请求。

---->/createorder-->A 服务器 --->A 向 B 的服务器发送消息队列 ---> B 服务器处理它 ---> 向 A 发送消息 --->A 响应到请求。

在这个过程中如何将请求保存在服务器A中并等待来自服务器B的响应。

【问题讨论】:

    标签: spring-boot microservices message-queue spring-jms spring-amqp


    【解决方案1】:

    听起来您需要熟悉Enterprise Integration Patterns 并使用Spring Integration 实现它。有一个像 gateway 这样的模式,这个框架有一个 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()

    【讨论】:

      【解决方案2】:

      要等待响应,您可以尝试使用 async rabbit。这是来自 baeldung 的教程:https://www.baeldung.com/spring-amqp-reactive

      【讨论】:

        猜你喜欢
        • 2020-04-26
        • 2020-01-10
        • 1970-01-01
        • 1970-01-01
        • 2020-05-03
        • 2020-04-27
        • 2018-03-04
        • 2018-12-27
        • 1970-01-01
        相关资源
        最近更新 更多