Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop message Micro Service by Spring Cloud

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how Spring Cloud develops messaging microservices. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preparation for the development of message microservices

We need to implement message producers and consumers in the microservice client, first set up the following projects:

Spring-server:Eureka server, port 8761, code directory "codes\ 08\ 8.4\ spring-server".

Spring-consumer: message consumer, Eureka client, registered to Eureka, port 8080, code directory "codes\ 08\ 8.4\ spring-consumer".

Spring-producer: message producer, Eureka client, registered to Eureka, port 8081, code directory "codes\ 08\ 8.4\ spring-producer".

The structure of the entire cluster plus message broker is shown in figure 8-10.

Figure 8-10 Program structure

Since Spring Cloud Stream enables me to interact with message agents, producers and consumers in the cluster do not need to care which message framework is used externally. The case in this section uses RabbitMQ by default. By default, port 5762 is connected to the local port. If you need to modify the connection information of RabbitMQ in the micro service, you can use the following configuration:

Spring: application: name: spring-producer rabbitmq: host: localhost port: 5672 username: guest password: guest author producer

In the spring-producer project, add the following dependencies:

Org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-stream-rabbit

The main introduction is "spring-coud-starter-stream-rabbit" dependency, which automatically introduces "spring-cloud-stream" and "spring-cloud-stream-binder" to our project.

Next, write the sending service, as shown in listing 8-5.

Listing 8-5:codes\ 08\ 8.4\ spring-producer\ src\ main\ java\ org\ crazyit\ cloud\ SendService.java

Public interface SendService {@ Output ("myInput") SubscribableChannel sendOrder ();

Create a new SendService interface and add the sendOrder method, which is decorated with the @ Output annotation. Using this annotation, a message channel for "myInput" is created, and when this method is called, a message is delivered to the "myInput" channel. If the @ Output annotation does not provide an argument, the method name is used as the channel name. Next, you need to have the Spring container enable the binding function. In the Application class, add the @ EnableBinding annotation, as shown in listing 8-6.

Listing 8-6:

Codes\ 08\ 8.4\ spring-producer\ src\ main\ java\ org\ crazyit\ cloud\ ProducerApplication.java

@ SpringBootApplication@EnableEurekaClient@EnableBinding (SendService.class) public class ProducerApplication {public static void main (String [] args) {SpringApplication.run (ProducerApplication.class, args);}}

In the @ EnableBinding annotation, take SendService.class as a parameter, and the Spring container will automatically bind the channel defined in the SendService API when it starts. Write the controller, call the send method of SendService, and send a message to the server, as shown in listing 8-7.

Listing 8-7:

Codes\ 08\ 8.4\ spring-producer\ src\ main\ java\ org\ crazyit\ cloud\ ProducerController.java

@ RestControllerpublic class ProducerController {@ Autowired SendService sendService; @ RequestMapping (value = "/ send", method = RequestMethod.GET) public String sendRequest () {/ / create message Message msg = MessageBuilder.withPayload ("Hello World" .getBytes ()) .build (); / / send message sendService.sendOrder (). Send (msg); return "SUCCESS";}}

In the controller, SendService is automatically injected through @ Autowired, the sendOrder method is called to get the SubscribableChannel (channel) instance, and then the send method is called to send the "Hello World" string to the "message agent". In this case, the default message agent is RabbitMQ. Next, first implement the messenger, and then integrate the test together.

Write consumers

The consumer project (spring-consumer) uses the same dependencies as the producer, first writing the channel interface to receive messages, as shown in listing 8-8.

Listing 8-8:

Codes\ 08\ 8.4\ spring-consumer\ src\ main\ java\ org\ crazyit\ cloud\ ReceiveService.java

Public interface ReceiveService {@ Input ("myInput") SubscribableChannel myInput ();

In ReceiveService, a "myInput" message input channel is defined, and then, like the producer, bind the message channel in the startup class, as shown in listing 8-9.

Listing 8-9:

Codes\ 08\ 8.4\ spring-consumer\ src\ main\ java\ org\ crazyit\ cloud\ ReceiverApplication.java

@ SpringBootApplication@EnableBinding (ReceiveService.class) public class ReceiverApplication {public static void main (String [] args) {SpringApplication.run (ReceiverApplication.class, args);} @ StreamListener ("myInput") public void receive (byte [] msg) {System.out.println ("received message:" + new String (msg));}}

In the startup class, @ EnableBinding is also used to open the binding and declare the channel's interface class. A new receive method is created, decorated with the @ StreamListener annotation, declaring messages subscribing to the "myInput" channel.

Start spring-server (port 8761), spring-producer (port 8081), spring-consumer (port 8080), visit: http://localhost:8081/send, and then open the messenger console to see the output of the "Hello World" string, proving that the consumer can get the message from the message broker.

Replace the binder

In the previous example, RabbitMQ is used as the message broker, and if you want to use Kafka, you can replace the Maven dependency to implement it. In the producer and consumer pom.xml, change the dependency of spring-cloud-starter-stream-rabbit to spring-cloud-starter-stream-kafka, as shown in the following configuration:

Org.springframework.cloud spring-cloud-starter-stream-kafka

Spring Cloud provides API for binders, and currently implements binders for RabbitMQ and Kafka. From the author's point of view, the binder is more like an adapter, and for our messaging program, we don't care which message broker is used, which is implemented by the binder.

Thank you for reading! This is the end of the article on "how Spring Cloud develops messaging microservices". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report