In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about what is the message-driven micro-service in Spring Cloud building micro-service architecture. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Let's take a look at Spring Cloud Stream through this article.
Spring Cloud Stream is a framework for building message-driven capabilities for microservice applications. It can create stand-alone Spring applications that can be used in production based on Spring Boot. It uses Spring Integration to connect message broker middleware to implement message event-driven micro-service applications. Spring Cloud Stream provides personalized automated configuration for some vendors' message middleware products, and introduces three core concepts: publish-subscribe, consumer group and message partition. To put it simply, Spring Cloud Stream essentially integrates Spring Boot and Spring Integration to implement a lightweight message-driven micro-service framework. Through the use of Spring Cloud Stream, developers can effectively simplify the complexity of using message middleware, so that system developers can have more energy to focus on the core business logic processing. Because Spring Cloud Stream is implemented based on Spring Boot, it inherits the advantages of Spring Boot and realizes the function of automatic configuration to help us get started with it quickly. But so far, Spring Cloud Stream only supports the automatic configuration of the following two famous messaging middleware:
RabbitMQ
Kafka
Getting started
Let's get a preliminary understanding of Spring Cloud Stream by building a simple example. The main goal of this example is to build a Spring Boot-based micro-service application that will receive messages and print them to a log by using message middleware RabbitMQ. Therefore, please make sure that RabbitMQ has been installed locally before proceeding to the following steps. For specific installation steps, please refer to this article.
Build a Spring Cloud Stream consumer
Create a basic Spring Boot project, named stream-hello
Edit the dependencies in pom.xml and introduce Spring Cloud Stream's support for RabbitMQ, as follows:
Org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-stream-rabbit org.springframework.cloud spring-cloud-dependencies Dalston.SR4 pom import
Create a consumer SinkReceiver to receive messages from RabbitMQ, as follows
@ EnableBinding (Sink.class) public class SinkReceiver {private static Logger logger = LoggerFactory.getLogger (SinkReceiver.class); @ StreamListener (Sink.INPUT) public void receive (Object payload) {logger.info ("Received:" + payload);}}
Create the application main class. Like other Spring Boot, there is nothing special here, as shown below:
@ SpringBootApplicationpublic class SinkApplication {public static void main (String [] args) {SpringApplication.run (SinkApplication.class, args);}}
At this point, the coding task of our quick start example is complete. Let's launch RabbitMQ and the Spring Boot application respectively, and then do the following experiments to see how they work.
Manual test verification
Let's first take a look at the startup log of the Spring Boot application.
INFO 16272-[main] o.s.c.s.b.r.RabbitMessageChannelBinder: declaring queue for inbound: input.anonymous.Y8VsFILmSC27eS5StsXp6A Bound to: inputINFO 16272-[main] o.s.a.r.c.CachingConnectionFactory: Created new connection: SimpleConnection@3c78e551 [delegate=amqp://guest@127.0.0.1:5672/] INFO 16272-[main] o.s.integration.channel.DirectChannel: Channel 'input.anonymous.Y8VsFILmSC27eS5StsXp6A.bridge' has 1 subscriber (s). INFO 16272-[main] o.s.i.a.i.AmqpInboundChannelAdapter: started inbound.input.anonymous.Y8VsFILmSC27eS5StsXp6A
From the log above, we can get the following information:
Using the guest user, we created a RabbitMQ connection to the 127.0.0.1 RabbitMQ 5672 location, which we can also find in the console.
Declares a queue named input.anonymous.Y8VsFILmSC27eS5StsXp6A and binds itself as its consumer through RabbitMessageChannelBinder. We can also find this information in the RabbitMQ console.
Next, we can go to the input.anonymous.Y8VsFILmSC27eS5StsXp6A queue management page in the RabbitMQ console and send a message to the queue through the Publish Message function.
At this point, we can see the following in the console of the currently launched Spring Boot application:
INFO 16272-[C27eS5StsXp6A-1] com.didispace.HelloApplication: Received: [B@7cba610e
We can find that the output in the application console is defined by the receive method in SinkReceiver, and the specific content of the output is from the object fetched in the message queue. Here, since we do not serialize the message, what is output is only a reference to the object, and we will describe the processing after receiving the message in detail in the following section.
After successfully completing the quick start example above, let's briefly explain how the above steps connect our Spring Boot application to RabbitMQ to consume messages to implement message-driven business logic.
First of all, what we do for Spring Boot applications is to introduce spring-cloud-starter-stream-rabbit dependency, which is the encapsulation of RabbitMQ support by Spring Cloud Stream, which includes the automatic configuration of RabbitMQ and so on. From the dependencies it defines below, we can also know that it is equivalent to spring-cloud-stream-binder-rabbit dependencies.
Org.springframework.cloud spring-cloud-stream-binder-rabbit
Next, let's take a look at several core annotations of Spring Cloud Stream used here, all of which are defined in SinkReceiver:
@ EnableBinding, which is used to bind to a message channel (Channel) by specifying one or more interfaces that define @ Input or @ Output annotations. In the above example, we bound the Sink interface through @ EnableBinding (Sink.class), which is the definition of input message channel binding implemented by default in Spring Cloud Stream. Its source code is as follows:
Public interface Sink {String INPUT = "input"; @ Input (Sink.INPUT) SubscribableChannel input ();}
It binds a channel called input through the @ Input annotation. In addition to Sink, Spring Cloud Stream also implements Source interface for binding output channel by default, and Processor interface that combines Sink and Source. In practice, we can also define the interface for binding message channel through @ Input and @ Output annotations. When we need to specify multiple interfaces for @ EnableBinding to bind the message channel, we can define it as @ EnableBinding (value = {Sink.class, Source.class}).
@ StreamListener: this annotation is mainly defined on the method, and its function is to register the modified method as the event listener of the data flow on the message middleware. The attribute value in the annotation corresponds to the name of the listening message channel. In the above example, we registered the receive method as a listener for the input message channel through the @ StreamListener (Sink.INPUT) annotation, so when we post a message in the RabbitMQ control page, the receive method responds accordingly.
Write unit test cases for consuming messages
Above, we have completed sending messages to verify the function of the message consumer program through the console of RabbitMQ. Although this method compares low, through the above steps, I believe you have some basic understanding of the message consumption of RabbitMQ and Spring Cloud Stream. Let's improve our introductory content by writing unit test cases for production messages.
Create a unit test class in the project created above:
@ RunWith (SpringRunner.class) @ EnableBinding (value = {SinkApplicationTests.SinkSender.class}) public class SinkApplicationTests {@ Autowired private SinkSender sinkSender; @ Test public void sinkSenderTester () {sinkSender.output () .send (MessageBuilder.withPayload ("produce a message: http://blog.didispace.com").build());} public interface SinkSender {String OUTPUT =" input "; @ Output (SinkSender.OUTPUT) MessageChannel output ();}}
After applying the above message consumer program, run the unit test program defined here, and we will immediately receive the following in the message consumer's console:
INFO 50947-[L2W-c2AcChb2Q-1] com.didispace.stream.SinkReceiver: Received: produce a message: http://blog.didispace.com
In the above unit test, we defined an output pass through @ Output (SinkSender.OUTPUT), and the name of the output channel is input, which has the same name as the consumption channel defined in the previous Sink, so the unit test here and the previous consumer program form a pair of producers and consumers. This is the end of this article, and if you can complete the above example on your own, then you are getting started with the basics of Spring Cloud Stream. However, the use of Spring Cloud Stream is much more than that, and in a recent blog post, I will continue to update this section to help them understand and use Spring Cloud Stream to build message-driven microservices!
This is what the message-driven micro-service is in the Spring Cloud micro-service architecture shared by the editor. If you happen to have similar doubts, please refer to the above analysis. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.