版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
开发环境:
- 开发工具:IntelliJ IDEA
- Java版本:1.8
- Spring Boot版本:2.1.6.RELEASE
- RabbitMQ版本:3.6.9
1、开启RabbitMQ
RabbitMQ安装在本地虚拟机CentOS7上,开启RabbitMQ操作如下:
#进入到系统管理目录
[root@rabbitmq ~]# cd /etc
#查看当前防火墙状态
[root@rabbitmq etc]# systemctl status firewalld.service
#关闭防火墙
[root@rabbitmq etc]# systemctl stop firewalld.service
#查看rabbitmq的安装目录
[root@rabbitmq ~]# whereis rabbitmq
#进入到rabbitmq指令目录
[root@rabbitmq ~]# cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.9/sbin
#查看指令列表
[root@rabbitmq sbin]# ll
#后台开启rabbitmq
[root@rabbitmq sbin]# rabbitmq-server -detached
#查看rabbit进程,获取进程号
[root@rabbitmq sbin]# ps -ef | grep rabbitmq
#关闭rabbit进程,6712为rabbit进程号
[root@rabbitmq sbin]# kill -9 6712
#查看用户列表
[root@rabbitmq sbin]# rabbitmqctl list_users
#添加用户
[root@rabbitmq sbin]# rabbitmqctl add_user root 1314
#设置管理员角色
[root@rabbitmq sbin]# rabbitmqctl set_user_tags root administrator
#设置无限制权限
[root@rabbitmq sbin]# rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
#删除用户
[root@rabbitmq sbin]# rabbitmqctl delete_user root 1314
2、创建Spring Boot工程
(1)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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.ming</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rabbitmq</name>
<description>RabbitMQ project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--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>
(2)工程结构
![](https://img-blog.csdnimg.cn/20190629212227408.jpg)
application.yml
spring:
rabbitmq:
#服务器地址
host: 192.168.230.128
#RabbitMQ端口
port: 5672
#RabbitMQ用户
username: root
#RabbitMQ密码
password: 1314
#是否确认生产的消息已被消费
publisher-confirms: true
rabbitmq:
queue:
#消息队列名称
msg: queue-msg
RabbitMQConfig
package com.ming.rabbitmq.config;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
@Value("${rabbitmq.queue.msg}")
private String msgQueueName;
@Bean
public Queue createQueueMsg() {
//创建字符串消费队列,durable代表是否持久化消息
return new Queue(msgQueueName, true);
}
}
RabbitMQProducer
package com.ming.rabbitmq.producer;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
//实现RabbitTemplate类的内部接口ConfirmCallback,重写confirm回调确认方法
public class RabbitMQProducer implements RabbitTemplate.ConfirmCallback {
@Value("${rabbitmq.queue.msg}")
private String msgRouting;
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMsg(String msg) {
System.out.println("发送的字符串消息:" + msg);
//设置回调
rabbitTemplate.setConfirmCallback(this);
//将消息发送到指定队列(msgRouting)
//RabbitTemplate中的convertAndSend方法表示转换和发送消息,默认使用SimpleMessageConverter对象转换
rabbitTemplate.convertAndSend(msgRouting, msg);
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
//ack表示是否消费成功,cause表示异常原因
if (ack) {
System.out.println("已成功消费!");
} else {
System.out.println("消费失败:" + cause);
}
}
}
RabbitMQConsumer
package com.ming.rabbitmq.consumer;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQConsumer {
/**
* 使用注解@RabbitListener定义监听器,用于监听rabbitmq.queue.msgs所指向的队列
* @param msg 队列中的字符串消息
*/
@RabbitListener(queues = {"${rabbitmq.queue.msg}"})
public void receiveMsg(String msg) {
System.out.println("消费的字符串信息:" + msg);
}
}
RabbitmqApplicationTests
package com.ming.rabbitmq;
import com.ming.rabbitmq.producer.RabbitMQProducer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqApplicationTests {
@Autowired
private RabbitMQProducer rabbitMQProducer;
@Test
public void contextLoads() {
rabbitMQProducer.sendMsg("It's good");
}
}
3、单元测试
运行测试类RabbitmqApplicationTests,结果如下:
![](https://img-blog.csdnimg.cn/20190629214111660.jpg)