环境:jdk11,springboot2.5.5
以下代码以路由模式(Routing)为示例。
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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-rabbitmq-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-rabbitmq-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-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. application.yml
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: admin
password: 123456
virtual-host: /
3. 配置队列,交换机,以及绑定关系
@Configuration
public class RabbitMQConfiguration {
// 创建队列
@Bean
public Queue orderQueue() {
/*
* durable:是否持久化,默认是false
* exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
* autoDelete:是否自动删除
* arguments 附加参数
*/
return new Queue("order-queue", true, false, false, null);
}
@Bean
public Queue courseQueue() {
/*
* durable:是否持久化,默认是false
* exclusive:是否独占,默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
* autoDelete:是否自动删除
* arguments 附加参数
*/
return new Queue("course-queue", true, false, false, null);
}
// 创建交换机
@Bean
public DirectExchange directExchange() {
// 交换机名称,是否持久化,是否自动删除,附加参数
return new DirectExchange("direct-exchange", true, false, null);
}
// 绑定交换机和队列
@Bean
public Binding bindingDirect1() {
return BindingBuilder.bind(orderQueue()).to(directExchange()).with("order");
}
@Bean
public Binding bindingDirect2() {
return BindingBuilder.bind(courseQueue()).to(directExchange()).with("course");
}
}
4. 配置消费者监听
@Service
public class ConsumerService {
@RabbitListener(queues = "order-queue")
public void orderConsumer(Message message) {
System.out.println("orderConsumer收到消息: " + new String(message.getBody(), StandardCharsets.UTF_8));
}
@RabbitListener(queues = "course-queue")
public void courseConsumer(Message message) {
System.out.println("courseConsumer收到消息: " + new String(message.getBody(), StandardCharsets.UTF_8));
}
}
5. 编写测试用例发送消息
@SpringBootTest
class SpringRabbitmqDemoApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void sendMessage1() {
rabbitTemplate.convertAndSend("direct-exchange","order","hello order");
}
@Test
void sendMessage2() {
rabbitTemplate.convertAndSend("direct-exchange","course","hello course");
}
}
6. SpringBoot启动类,启动项目
@SpringBootApplication
public class SpringRabbitmqDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringRabbitmqDemoApplication.class, args);
}
}
7. 运行测试类发送消息,进行测试