In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Spring Cloud Stream". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Spring Cloud Stream.
Spring Cloud Stream is a framework for building message-driven microservices. It is built on top of Spring Boot to create industrial applications and provides connectivity to message brokers through Spring Integration. Spring Cloud Stream provides personalized automated configuration for some vendors' messaging middleware products (currently only RabbitMQ and Kafka are supported), while introducing the semantic concepts of publish subscriptions, consumer groups and partitions.
Create a project
First, let's create a normal Spring Boot project named stream-hello, and then add the following dependencies:
Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-stream-rabbit
Spring-cloud-starter-stream-rabbit dependency is the encapsulation of RabbitMQ by Spring Cloud Stream, which also includes the automatic configuration of RabbitMQ. For example, the default address of the connected RabbitMQ is localhost, the default port is 5672, the default user name is guest, and the default password is guest. Since our RabbitMQ is configured by default, the configuration here can be run without modification.
Create a receiver
Then create a class called SinkReceiver to receive messages from RabbitMQ, as follows:
@ EnableBinding (Sink.class) public class SinkReceiver {private static Logger logger = LoggerFactory.getLogger (StreamHelloApplication.class); @ StreamListener (Sink.INPUT) public void receive (Object playload) {logger.info ("Received:" + playload);}}
Here we first use the @ EnableBinding annotation to bind the message channel, and we also pass in a parameter that Sink.class,Sink is an interface, which is the definition of the input message channel binding implemented by default in Spring Cloud Stream. Then we define the receive method in the SinkReceiver class and add a @ StreamListener annotation to the method, which indicates that the method is an event listener for the data flow on the message middleware, and the Sink.INPUT parameter indicates that this is the listener processor on the input message channel.
test
Ok, after doing the above, we can start our project. In the project startup log, we can see the following:
This indicates that our project has created a connection to RabbitMQ through guest users. At this point, open the management page of RabbitMQ in the browser, and you can also see the connection, as follows:
The red line in the figure is our newly created queue. Click on the queue to find the Publish message option. We can send a message to our project from here, as follows:
At this point, we can see that the message has been received in the console of the project, as follows:
It's just that the message is not serialized.
Thank you for your reading, the above is the content of "how to use Spring Cloud Stream", after the study of this article, I believe you have a deeper understanding of how to use Spring Cloud Stream, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.