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

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.

相关问题 RabbitMq 在 Spring 引导微服务中看不到队列并且无法将消息从一个服务发送到另一个服务 - RabbitMq Cannot see queue and Cannot send message from one service to another in Spring Boot Microservices 如何从 React 中的获取服务(Springboot)读取响应承诺值? - How Read Response Promise value from a get service (Springboot) in React? springboot 获取令牌以调用另一个服务 - springboot get token to call another service 从 SpringBoot 中的 API 获取特定的响应值 - Get specific response value from the API in SpringBoot springboot中如何在另一个api中调用另一个post mapping api - How to call another post mapping api in another api in springboot 如何通过 Spring WebClient 的服务名称从另一个微服务调用一个微服务:Java,Spring - How to call one microservice from another microservices by service name by Spring WebClient : Java, Spring Springboot API 将请求放在队列中? - Springboot API placing requests on a queue? 如何从服务器响应中获取 session cookie 以发回 - how to get session cookie from server response to send back 如何获取SpringBoot REST API服务的响应时间? - How to get response time for SpringBoot REST API services? 在 spring boot 中发送成功响应后调用另一个服务? - call another service after sending the success response in spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM