RabbitMQ整合Spring AMQP实战涉及到如下几个部分
- 集成RabbitAdmin
- SpringAMQP 声明
- rabbitTemplate
- SimpleMessageListenerContainer
- MessageListenAdapter
- MessageConverter
(通过spring集成RabbitMQ)
搭建基本的项目运行框架
1.项目pom文件
<?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.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.star</groupId>
<artifactId>rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmq</name>
<description>rabbitmq整合spring AMQP</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建配置类
package com.star.rabbitmq.config;
/**
* @Author likai
* @Description rabbitmq配置
* @Date 2020/3/10 21:08
**/
public class Config {
/**
* rabbitmq ip
* */
public static String HOST = "127.0.0.1";
/**
* rabbitmq 端口
* */
public static int PORT = 5672;
/**
* rabbitmq 账号
* */
public static String USERNAME = "root";
/**
* rabbitmq 密码
* */
public static String USERPASS = "123456";
}
3.创建RabbitMQConfig配置类做基本配置
RabbitAdmin类可以很好的操作RabbitMQ,在Spring中直接进行注入即可
注意:autoStartup必须要设置未true,否则Spring容器不会加载RabbitAdmin类。
RabbitAdmin 底层实现就是从Spring容器中获取Exchange、Bindind、RoutingKey以及Queue的@Bean声明。然后使用RabbitTemplate的execute方法执行对应的声明、修改、删除等一些列RabbitMQ基础功能操作。例如:添加一个交换机、删除一个绑定、清空一个队列里的消息等等。
配置文件代码:
package com.star.rabbitmq.config;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
/**
* @Author likai
* @Description
* @Date 2020/3/10 21:19
**/
@Configuration
public class RabbitMQConfig {
/**
* @Description: 创建rabbitmq连接工厂
* @param
* @return ConnectionFactory
* @throws
* @author kaili
* @date 2020/3/10 21:21
*/
@Bean
public ConnectionFactory connectionFactory(){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(Config.HOST);
connectionFactory.setPort(Config.PORT);
connectionFactory.setUsername(Config.USERNAME);
connectionFactory.setPassword(Config.USERPASS);
return connectionFactory;
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
}
4.测试RabbitAdmin
通过以上的步骤基本上已经完成了spring集成RabbitAdmin,现在通过代码来测试是否够集成成功。在测试类中测试RabbitAdmin是否可用
4.1 测试创建交换机
package com.star.rabbitmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
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 RabbitmqSpringApplicationTests {
@Autowired
private RabbitAdmin rabbitAdmin;
/**
* @Description: 测试RabbitAdmin
* @author kaili
* @date 2020/3/10 21:32
*/
@Test
public void testRabbitAdmin(){
//测试1 创建Exchange
rabbitAdmin.declareExchange(new DirectExchange("test.direct",false,false));
rabbitAdmin.declareExchange(new TopicExchange("test.topic",false,false));
rabbitAdmin.declareExchange(new FanoutExchange("test.fanout",false,false));
}
}
启动测试代码,观察管控页面是否有对应交换机创建成功
测试窗口代码运行正常,观察管控页面
创建了三种类型的交换机,非持久类型
4.2测试创建队列
在testRabbitAdmin接口添加如下的测试代码
//测试2 创建队列
String queueDirect = "test.direct.queue";
String queueTopic = "test.topic.queue";
String queueFanout = "test.fanout.queue";
rabbitAdmin.declareQueue(new Queue(queueDirect,false));
rabbitAdmin.declareQueue(new Queue(queueTopic,false));
rabbitAdmin.declareQueue(new Queue(queueFanout,false));
启动testRabbitAdmin接口,观察管控台页面
观察交换机页面,由于之前的测试已经创建过哪三个交换机接口重复执行创建代码也不会创建新的交换机。
4.3 交换机和队列绑定
//3.测试绑定
rabbitAdmin.declareBinding(new Binding(queueDirect,
Binding.DestinationType.QUEUE,
"test.direct",
"direct",
new HashMap<>()));
rabbitAdmin.declareBinding(
BindingBuilder.bind(new Queue(queueTopic,false))
.to(new TopicExchange("test.topic",false,false))
.with("user.#")
);
rabbitAdmin.declareBinding(
BindingBuilder.bind(new Queue(queueTopic,false))
.to(new FanoutExchange("test.fanout",false,false))
);
启动代码观察三个交换机的绑定关系
test.direct
test.fanout
test.topic
在test.direct、test.topic、test.fanout交换机中发送消息,观察消息是否发送到对应的队列中
到对应的队列中观察消息是否存在
查看消息类容
与在交换机中发送的消息一致
4.4 清除队列中的消息
//清空队列数据
rabbitAdmin.purgeQueue(queueDirect,false);