概述
Springboot作为一个流行的轻量级框架,对RabbitMQ提供了很好的支持。本篇我们就介绍如何在Spring Boot中集成RabbitMQ。
项目搭建
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 https://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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javamike</groupId>
<artifactId>springboot-rabbitmq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-rabbitmq</name>
<description>Demo project for Rabbitmq</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、MyAMQPConfig
package com.javamike.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.support.converter.MessageConverter;
/**
* 自定义消息转换器,默认是jdk的序列化转换器,我们自定义为json的
*/
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
}
3、新建一个Springboot测试类,并注入RabbitTemplate和AmqpAdmin,用来操作交换器,队列和绑定等逻辑
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
4、创建rabbitmq发送消息需要的交换器,队列和绑定
@Test
public void create() {
//创建direct类型Exchange,名称为exchange.direct
amqpAdmin.declareExchange(new DirectExchange("exchange.direct"));
//创建fanout类型Exchange,名称为exchange.fanout
amqpAdmin.declareExchange(new FanoutExchange("exchange.fanout"));
//创建topic类型Exchange,名称为exchange.topic
amqpAdmin.declareExchange(new TopicExchange("exchange.topic"));
//创建名称为direct Queue的队列,durable为true表示持久化
amqpAdmin.declareQueue(new Queue("direct.queue", true));
//创建名称为fanout Queue的队列,durable为true表示持久化
amqpAdmin.declareQueue(new Queue("fanout.queue", true));
//绑定Queue
amqpAdmin.declareBinding(new Binding("direct.queue", Binding.DestinationType.QUEUE, "exchange.direct", "direct.queue", null));
amqpAdmin.declareBinding(new Binding("fanout.queue", Binding.DestinationType.QUEUE, "exchange.direct", "fanout.queue", null));
amqpAdmin.declareBinding(new Binding("direct.queue", Binding.DestinationType.QUEUE, "exchange.fanout", "", null));
amqpAdmin.declareBinding(new Binding("fanout.queue", Binding.DestinationType.QUEUE, "exchange.fanout", "", null));
amqpAdmin.declareBinding(new Binding("direct.queue", Binding.DestinationType.QUEUE, "exchange.topic", "direct.#", null));
amqpAdmin.declareBinding(new Binding("fanout.queue", Binding.DestinationType.QUEUE, "exchange.topic", "direct.*", null));
}
5、发送direct类型消息
@Test
public void send2Direct() {
Map<String, Object> map = new HashMap<>();
map.put("msg", "这是一条direct消息");
map.put("data", Arrays.asList("Hello World", 123456, true));
//对象被序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct", "direct.queue", map);
}
6、发送fanout类型消息
@Test
public void send2Fanout() {
Map<String, Object> map = new HashMap<>();
map.put("msg", "这是一条fanout消息");
map.put("data", Arrays.asList("Hello World", 123456, true));
//对象被序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.fanout", "", map);
}
7、发送topic类型消息
@Test
public void send2Topic() {
Map<String, Object> map = new HashMap<>();
map.put("msg", "这是一条topic消息");
map.put("data", Arrays.asList("Hello World", 123456, true));
//对象被序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.topic", "direct.test", map);
}
8、接收队列的消息
@Test
public void receive() {
//根据队列名称获取消息
Object o = rabbitTemplate.receiveAndConvert("direct.queue");
o.getClass();
System.out.println(o.getClass());
System.out.println(o);
}
9、也可以通过监听消息队列来接收某个队列的消息,queuesToDeclare=@Queue(“fanout.queue”)表示自动创建队列
@RabbitListener(queuesToDeclare=@Queue("fanout.queue"))
public void receive(Message message) {
System.out.println("收到fanout.queue消息 : " + new String(message.getBody()));
}