Use Spring Cloud Bus to realize message based Microservices network communication

Using Spring Cloud Bus to implement message based Microservices network communication can simplify the communication between Microservices. Below are the Maven coordinates and a brief introduction to the dependency class library of Spring Cloud Bus: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> -Spring cloud starter bus mqp: This class library is an AMQP implementation of Spring Cloud Bus, which uses the AMQP protocol for message passing. Next, we will implement a sample of message based Microservices network communication and write complete Java code. First, we need to create two Microservices: Sender Microservices and Receiver Microservices. The sender Microservices is responsible for sending messages, and the receiver Microservices is responsible for receiving messages. The application class code of Sender Microservices is as follows: @SpringBootApplication @RestController public class SenderApplication { @Autowired private SimpleMessageSender messageSender; @GetMapping("/send") public String sendMessage() { messageSender.send("Hello, world!"); return "Message sent"; } public static void main(String[] args) { SpringApplication.run(SenderApplication.class, args); } } In the Sender Microservices, we use the 'SimpleMessageSender' class to send messages` SimpleMessageSender 'is a custom class used to send messages to Spring Cloud Bus. The application class code of the Receiver Microservices is as follows: @SpringBootApplication public class ReceiverApplication { public static void main(String[] args) { SpringApplication.run(ReceiverApplication.class, args); } } In Receiver Microservices, we don't need to make any changes, just start Microservices. Next, we need to define a custom message sender 'SimpleMessageSender', with the following code: @Component public class SimpleMessageSender { private final BinderAwareChannelResolver resolver; public SimpleMessageSender(BinderAwareChannelResolver resolver) { this.resolver = resolver; } public void send(String message) { MessageChannel channel = resolver.resolveDestination("output"); Message<String> msg = MessageBuilder.withPayload(message).build(); channel.send(msg); } } In the 'SimpleMessageSender' class, we use the 'BinderAwareChannelResolver' class to parse the target sending channel and then send the message through the channel. Finally, we need to configure the properties file of the application. Configuration file application.properties of Sender Microservices: properties spring.cloud.bus.enabled=true spring.cloud.stream.bindings.output.destination=output The configuration file application.properties of the Receiver Microservices: properties spring.cloud.bus.enabled=true spring.cloud.stream.bindings.input.destination=output spring.cloud.stream.bindings.input.group=receiver-group In the above configuration file, 'output' is the target for sending and receiving messages. After running the Sender Microservices and Receiver Microservices, access the '/send' interface of the Sender Microservices, and the Sender Microservices will send a message to the Spring Cloud Bus and be received by the Receiver Microservices. Conclusion: By using Spring Cloud Bus, we can realize Microservices network communication based on messages. We define a message sender class to send messages to the Spring Cloud Bus, and then other Microservices can receive these messages. Spring Cloud Bus uses AMQP protocol for message transmission. Through message transmission, decoupling and Asynchronous communication between Microservices can be achieved.