Getting started with RabbitMQ (Installation of RabbitMQ + Solve issues such as version during RabbitMQ installation process + Solve issues such as failure to start RabbitMQ service + Use of Java client)
- 1. Foreword - refer to the official website
- 2. Download and install
- 3. Start the service
- 4. Access the management interface
- 5. Start, stop services and other related commands
- 6. Quick Start
1. Foreword - refer to the official website
1.1 Official website address
1.2 Pay attention to version comparison
- Note the RabbitMQ versions supported by the Linux version
- Note the Erlang versions supported by Linux
- Note the Erlang versions supported by RabbitMQ
- For more version correspondence, see the link below
https://www.rabbitmq.com/which-erlang.html .
- For more version correspondence, see the link below
2. Download and install
2.1 Download and install erlang
2.1.1 Check Linux version
- My side is centos, the command is as follows:
cat /etc/centos-release
2.1.2 Check the erlang version supported by the current system
- The command is as follows:
uname -a
2.1.3 Download erlang
- Download address:
https://github.com/rabbitmq/erlang-rpm . - According to the Linux version and the erlang version supported by the system, download the corresponding erlang, as follows:
- So, what I downloaded here is
Erlang 23.3.4.18
(note: if you are learning by yourself, it is recommended to change to CenOS8, it is also best to change to other distributions directly, such as Ubuntu, etc.), as follows:
2.1.4 Install erlang
- The installation command is as follows:
rpm -ivh erlang-23.3.4.18-1.el7.x86_64.rpm
2.1.5 Problems encountered
- Explanation: This problem is caused by the installation
erlang-23.3.4.18-1.el8.x86_64.rpm
( ), but there is no problem with the installation ( ). My Linux system supports it according to the above check , so I don’t know if it is a version problem!el8
erlang-23.3.4.18-1.el7.x86_64.rpm
el7
el7
- as follows:
error: Failed dependencies: libtinfo.so.6()(64bit) is needed by erlang-23.3.4.18-1.el8.x86_64 libz.so.1(ZLIB_1.2.7.1)(64bit) is needed by erlang-23.3.4.18-1.el8.x86_64
- Solve the problem:
In addition--nodeps --force
, the function of this command: No longer analyze the dependencies between packages and install them directlyrpm -ivh erlang-23.3.4.18-1.el8.x86_64.rpm --nodeps --force
2.2 Install socat
- The installation command is as follows:
yum install socat -y
2.3 Download and install rabbitmq-server
2.3.1 Download rabbitmq-server
- When downloading RabbitMQ, pay attention to the Erlang version downloaded above (
Erlang 23.3.4.18
), and then find the corresponding RabbitMQ version- Download address 1: It is easy to find the new version, but it is difficult to find the old version, as follows:
https://github.com/rabbitmq/rabbitmq-server/releases . - Download address 2:rpm warehouse
https://packagecloud.io/rabbitmq.
- Download address 1: It is easy to find the new version, but it is difficult to find the old version, as follows:
- According to the Linux system and Erlang version, find the corresponding rabbitmq version as follows:
- The download address of this version is as follows:
https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/7/rabbitmq-server-3.9.13-1.el7.noarch.rpm?distro_version_id=140 .
- The download address of this version is as follows:
2.3.2 Install rabbitmq-server
- The installation command is as follows:
rpm -ivh rabbitmq-server-3.9.13-1.el7.noarch.rpm
3. Start the service
3.1 Start the service
- After installation, find the installation directory:
whereis rabbitmq
- Enter the installation
sbin
directory:
- Start service:
./rabbitmq-server start
- Check whether the service is started:
ps -ef | grep rabbitmq
3.2 Failure to start the service
3.2.1 Version problem
- If the startup fails, check whether erlang is installed or whether the erlang version is correct. The check command is as follows:
rpm -qa|grep erlang
- If
erlang
the wrong version is installed, you can uninstall and reinstall (uninstall rabbitmq before uninstalling, because rabbitmq depends on erlang). The uninstall command is as follows:# 1. 卸载rabbitmq-server rpm -e rabbitmq-server rm -rf /usr/lib/rabbitmq # 2. 卸载erlang rpm -e erlang rm -rf /usr/lib64/erlang
- Then reinstall the correct version.
4. Access the management interface
4.1 Open the graphical management interface
- If rabbitmq is starting, stop the service first, and then install the web interface plug-in. The command is as follows:
rabbitmq-plugins enable rabbitmq_management
4.2 Check the occupied ports
-
as follows:
ps -ef | grep rabbit lsof -i | grep pid
4.3 Open port 15672
- Refer to the following articles:
Check the firewall status, turn off the firewall, open and close ports, etc. under Linux .
4.4 Access the management interface
- address:
http://服务器IP:15672/
4.5 Login issues
4.5.1 Problem
- Log in, the username and password are both: guest. After logging in, the prompt is as follows
4.5.2 Troubleshooting - Create new user
- Enter the installation directory and view users and roles:
cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.9.13/sbin ./rabbitmqctl list_users
- Create new user
- Step 1: Add mqAdmin user and set password 123456
rabbitmqctl add_user mqAdmin 123456
- Step 2: Set user role (set mqAdmin user to administrator role)
rabbitmqctl set_user_tags mqAdmin administrator
- Step 3: Set user permissions (specify vhost and write/read allowed access)
rabbitmqctl set_permissions -p "/" mqAdmin ".*" ".*" ".*"
- Step 4, check:
- Step 1: Add mqAdmin user and set password 123456
4.6 Login - Using a new user
mqAdmin
Log in using the newly created user above , as follows:
5. Start, stop services and other related commands
5.1 Start command
5.1.1 Startup method
- Way 1: is what is used above, enter the sbin directory of the installation directory and start:
cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.9.13/sbin/ ./rabbitmq-server start
- Way 2:Set up startup, as follows:
chkconfig rabbitmq-server on
- Way 3: Service command starts (executable in any directory)
service rabbitmq-server start
5.1.2 Startup problem——Job for rabbitmq-server.service failed……
5.1.2.1 Questions
- When using
service rabbitmq-server start
the command to start the error reporting problem, the problem description is as follows:Job for rabbitmq-server.service failed because the control process exited with error code. See "systemctl status rabbitmq-server.service" and "journalctl -xe" for details.
5.1.2.2 Solve the problem
5.1.2.2.1 Attempt 1:
- Execute
journalctl -xe
the command to view detailed problems:
- Solve the problem (modify the group and owner of the two log files in the /var/log/rabbitmq/ directory to rabbitmq):
cd /var/log/rabbitmq/ chown -R rabbitmq:rabbitmq rabbit@hello_TQ1*
5.1.2.2.2 Attempt 2:
- After the above problem is solved, if the error still occurs after restarting, execute
journalctl -xe
the command again to view the detailed problem, as follows:
- The solution is the same as above, as follows:
executeservice rabbitmq-server start
the command again without reporting an error, as follows:
5.2 Summary of commonly used commands
- Start service
service rabbitmq-server start
- Out of service
service rabbitmq-server stop
- Restart service
service rabbitmq-server restart
6. Quick Start
6.1 Simulate sending messages on the console
-
To create a switch, of course you can use the default switch. I will use the default one to save trouble here.amq.fanout。
-
Create queue
-
The switch binds the queue as follows:
-
send message
-
Check
6.2 Java client - basic code
6.2.1 pom file
- as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.liu.susu</groupId> <artifactId>rabbitmq-demo1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>rabbitmq-demo1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- rabbitmq依赖客户端--> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.8.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>
6.2.2 Producer
- test:
- Check the effect
- Attached code
package com.liu.susu.example; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; /** * @Description 生产者 * @Author susu */ public class Producer { // 队列名称 public static final String QUEUE_NAME = "queue-hello"; public static void main(String[] args) throws Exception{ //1. 创建连接工厂 ConnectionFactory factory = new ConnectionFactory(); //2. 工厂IP连接rabbitmq的队列 factory.setHost("服务IP地址"); //3. 连接用户名和密码 factory.setUsername("mqAdmin"); factory.setPassword("123456"); //4. 创建连接 Connection connection = factory.newConnection(); //5. 获取信道 Channel channel = connection.createChannel(); /** * 6. 创建一个队列 */ channel.queueDeclare(QUEUE_NAME,false,false,false,null); //7. 定义发送消息的内容 String msg = "hello world!!!,我来之于Java程序"; /** * 8. 发送消息 * 1. 参数1:交换机(发送到哪个交换机上) * 2. 参数2:路由key的值(本次发送用的队列名称) * 3. 参数3:其他参数信息 (本次没有,直接null) * 4. 参数4:发送消息的消息体 */ channel.basicPublish("",QUEUE_NAME,null,msg.getBytes()); System.out.println("=====消息发送成功====="); } }
6.2.3 Consumers
-
Before consuming, first read the messages in the queue
-
Test consumer consumption messages
-
After consumption, look at the messages in the queue
-
Attached code:
package com.liu.susu.example; import com.rabbitmq.client.*; /** * @Description 消费者 接收消息 * @Author susu */ public class Consumer { // 队列名称 public static final String QUEUE_NAME = "queue-hello"; public static void main(String[] args) throws Exception{ //1. 创建连接工厂 ConnectionFactory factory = new ConnectionFactory(); //1.1. 工厂IP连接rabbitmq的队列 factory.setHost("服务IP地址"); //1.2. 连接用户名和密码 factory.setUsername("mqAdmin"); factory.setPassword("123456"); //2. 创建连接 Connection connection = factory.newConnection(); //3. 获取信道 Channel channel = connection.createChannel(); // 接收消息后的回调 DeliverCallback deliverCallback = (consumerTag,msg)->{ byte[] msgBody = msg.getBody(); System.out.println("消费的消息是:" + new String(msgBody)); }; // 取消消息时的回调 CancelCallback cancelCallback = (consumerTag)->{ System.out.println("===消息消费被取消===="); }; /** * 4. 消费者消费消息 * 1. 参数1:消费哪个队列 * 2. 参数2:消费成功之后是否要自定应答 * true——自动应答 * false——手动应答 * 3. 参数3:消费者接收消息后的回调方法 * 4. 参数4:消费者取消消费的回调(正常接收不调用) */ channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback); } }
6.2.4 Problems encountered
6.2.4.1 Problem 1——Connection timed out
- Problem description: Operation timed out (Connection timed out)
- Solving the problem
For cloud services, opening ports in the security group5672
can solve the problem.
6.2.4.2 Question 2——Connection refused
- Questions are as follows:
Connection refused (Connection refused)
- Solve the problem
If the firewall is not closed,5672
just open the port on the firewall.sudo firewall-cmd --add-port=5672/tcp --permanent firewall-cmd --reload
6.3 Java Client——Spring AMQP
6.3.1 Introduction
- AMQP (Advanced Message Queuing Protocol) is an open standard for delivering business messages between applications. This protocol has nothing to do with language platforms and is more in line with the requirements for independence in microservices.
- Spring AMQP is a set of API specifications based on the AMQP protocol, which provides templates to send and receive messages.
- Spring AMQP consists of
spring-amqp
twospring-rabbit
parts: spring-amqp is the basic abstraction and spring-rabbit is the underlying default implementation. - Refer to the official website:
https://spring.io/projects/spring-amqp .
- Spring AMQP consists of
6.3.2 Project structure
-
Directory Structure
-
parentpom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.liu.susu</groupId> <artifactId>rabbitmq-demo2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>rabbitmq-demo2</name> <url>http://maven.apache.org</url> <modules> <module>producer</module> <module>consumer</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>3.8.1</junit.version> <lombok.version>1.16.8</lombok.version> <spring-boot.version>2.7.16</spring-boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- AMQP依赖,包含RabbitMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> <version>2.7.14</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!-- <dependency>--> <!-- <groupId>junit</groupId>--> <!-- <artifactId>junit</artifactId>--> <!-- <version>${junit.version}</version>--> <!-- <scope>test</scope>--> <!-- </dependency>--> </dependencies> </dependencyManagement> </project>
6.3.3 Producer services
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.liu.susu</groupId> <artifactId>rabbitmq-demo2</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>producer</artifactId> <packaging>jar</packaging> <name>producer</name> <description>producer</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- AMQP依赖,包含RabbitMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!--单元测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> </project>
-
application.yml
spring: rabbitmq: host: 服务IP地址 # 服务IP port: 5672 # 端口(发送消息的端口5672 rabbitmq管理界面的端口15672) # virtual-host: /mqAdmin # 虚拟主机 username: mqAdmin # 用户名 password: 123456 # 密码
-
Test class for sending messages
package com.liu.susu.producer; import org.junit.jupiter.api.Test; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; /** * @Description * @Author susu */ @SpringBootTest public class ProducerTest { @Autowired private RabbitTemplate rabbitTemplate; @Test public void publishMsg(){ System.out.println("---------------------"); String queueName = "queue-hello"; String msg = "hello,I am from amqp!!!"; //发送消息 rabbitTemplate.convertAndSend(queueName,msg); System.out.println("amqp——消息发送成功!!!"); } }
-
Test to see the effect
6.3.4 Consumer Services
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.liu.susu</groupId> <artifactId>rabbitmq-demo2</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>consumer</artifactId> <packaging>jar</packaging> <version>0.0.1-SNAPSHOT</version> <name>consumer</name> <description>consumer</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- AMQP依赖,包含RabbitMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
application.yml
logging: pattern: dateformat: HH:mm:ss:SSSS spring: rabbitmq: host: 服务IP地址 # 服务IP port: 5672 # 端口(发送消息的端口5672 rabbitmq管理界面的端口15672) # virtual-host: /mqAdmin # 虚拟主机 username: mqAdmin # 用户名 password: 123456 # 密码
-
Component that receives messages
package com.liu.susu.consumer.listeners; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; /** * @Description * @Author susu */ @Component public class MqListener { @RabbitListener(queues = "queue-hello") //监听的队列 public void listenQueues(String msg){ System.out.println("消费者收到消息===>"+msg); } }
-
Start the service and test to see the effect
6.4 Additional items
- Download project:
rabbitmq-demo (Java client-basic code + Java client-Spring AMQP)
.