In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to integrate RabbitMQ in SpringBoot. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Install RabbitMQ
Due to the change of Mac, some environments are directly done in Mac, but installing RabbitMQ will make your computer system too messy, so you can install it in Docker if you can install it in Docker. This time I also installed RabbitMQ directly in Docker.
Start Docker for Mac. If you haven't installed it, please see my previous article: http://www.54tianzhisheng.cn/2018/01/25/Docker-install/
Of course, you can also start and install RabbitMQ in your own Linux server or virtual machine.
Docker installation is simple, because RabbitMQ has officially provided its own Docker container, which requires only one command: (you can move to the right to see the complete code)
Docker run-d-p 15672 RABBITMQ_DEFAULT_PASS=admin-- name rabbitmq rabbitmq:3-management
The image has a web-based console and Http API. Http API can see how to use it at the address: http://localhost:15672/api/
Explain the above command line:
15672: represents the port number of the RabbitMQ console, and you can perform RabbitMQ-related operations through the console in the browser.
5672: indicates the TCP port number that RabbitMQ listens to, through which the application can establish a TCP connection with RabbitMQ and complete subsequent asynchronous message communication
RABBITMQDEFAULTUSER: used to set the user name of the login console. Here I set admin.
RABBITMQDEFAULTPASS: used to set the password for logging in to the console. Here I set admin
After the container starts successfully, you can enter the address in the browser: http://localhost:15672/ to access the console
After landing:
Briefly describe the role of the console list in the above figure:
Overview: used to view some basic information about RabbitMQ (message queue, message delivery rate, node, port, context information, etc.)
Connections: used to view connection information for RabbitMQ clients
Channels: users view the channel information of RabbitMQ
Exchange: used to view RabbitMQ switch
Queues: the queue used to view RabbitMQ
Admin: used to manage users, but can increase users
Create a project
Create a SpringBoot project structure in IDEA:
Support for RabbitMQ is already built into the SpringBoot framework, as you can see if you have read the official documentation, we just need to introduce the dependency spring-boot-starter-amqp.
1. After pom.xml introduces dependency, it is as follows:
4.0.0 com.zhisheng rabbitmq 0.0.1-SNAPSHOT jar rabbitmq Demo project for Spring Boot RabbitMQ org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web Org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-amqp org.springframework.boot spring-boot-maven-plugin
2. The application.properties configuration is modified as follows:
Spring.rabbitmq.addresses=localhost:5672spring.rabbitmq.username=adminspring.rabbitmq.password=admin
3. Message sending class RabbitMQClient.java
Package com.zhisheng.rabbitmq.client;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * Created by zhisheng_tian on 2018-1-23 * / @ Componentpublicclass RabbitMQClient {@ Autowired private RabbitTemplate rabbitTemplate; public void send (String message) {rabbitTemplate.convertAndSend ("zhisheng", message);}}
In this way, the sending message code is implemented.
The key code here is the rabbitTemplate.convertAndSend () method, and zhisheng this is the routing rule (routingKey). Its value indicates that the message is sent to the specified queue zhisheng. Following the source code here, it is found that the last method called by the convertAndSend () method is actually a doSend () method.
4. Message receiving class
Package com.zhisheng.rabbitmq.server;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;/** * Created by zhisheng_tian on 2018-1-23 * / @ Componentpublicclass RabbitMQServer {@ RabbitListener (queues = "zhisheng") public void receive (String message) {System.out.println ("the message received is:" + message);}}
You see, there is a RabbitListener listening to the queue zhisheng all the time.
Of course, this queue must be created by ourselves in the application, unlike the Kafka in my previous article "SpringBoot Kafka Integrated use", Kafka will be created dynamically when using the queue, and there is no need for us to create it in advance.
So how do you create queues in RabbitMQ?
As shown in the figure above: so we have created a queue for zhisheng, and when the program starts running, the message receiving class will continue to listen for incoming messages in queue zhisheng.
5. Run the project
You need to inject the class that sends the message into the startup class, and provide the init method, and call the send () method of the sending message class in the init method
@ PostConstructpublicvoid init () {rabbitMQClient.send ("send message-zhisheng-");}
It is important to note that the init () method is annotated with @ PostConstruct, and the method modified by @ PostConstruct is executed after the constructor.
Start the project to find that the console has received the message.
6. Single-thread test performance
See the code commented out in the picture above? That's used to test the performance of message sending, and I send 10000 messages to see how much time it takes.
10000 messages took time to send: 215ms. This is in a single-threaded environment, which can be compared with other MQ tests next time, and the performance can also be tested in a multithreaded environment.
At the same time, you can see the rate of transmission from the console:
7. Multithreading testing performance
Ten threads were opened, each sending 10000 messages.
The init method code is as follows:
@ PostConstructpublic void init () {StopWatch stopWatch = new StopWatch (); stopWatch.start (); int threads = 10; ExecutorService executorService = Executors.newFixedThreadPool (threads); final CountDownLatch start = new CountDownLatch (1); final CountDownLatch end = new CountDownLatch (threads); for (int I = 0; I
< threads; i++) { executorService.execute(( ) ->{try {start.await (); for (int i1 = 0; i1 < 10000; i1cm +) {rabbitMQClient.send ("send message-zhisheng-");}} catch (InterruptedException e) {e.printStackTrace ();} finally {end.countDown ();}}) } start.countDown (); try {end.await ();} catch (InterruptedException e) {e.printStackTrace ();} finally {executorService.shutdown ();} stopWatch.stop (); System.out.println ("time to send message:" + stopWatch.getTotalTimeMillis ());}
Time consuming: 4063ms
The console is displayed as follows:
After reading the above, do you have any further understanding of how to integrate RabbitMQ in SpringBoot? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.