rabbitmq使用springboot实现direct模式(最新推荐)
一、 Direct模式
- 类型:direct
- 特点:Direct模式是fanout模式上的一种叠加,增加了路由RoutingKey的模式。

二、coding
Ⅰ 生产者
1、引入相应的pom文件 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.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xpf</groupId>
<artifactId>rabbitmq-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rabbitmq-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>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-web</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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>2、配置文件 application.properties
server.port=8080 spring.rabbitmq.username=admin spring.rabbitmq.password=admin spring.rabbitmq.virtual-host=/ spring.rabbitmq.host=192.168.199.20 spring.rabbitmq.port=5672
3、写一个生产者 DirectOrderService.java
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class DirectOrderService {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 模拟用户下单,发送消息给下游系统
* @param user
* @param num
*/
public void makerOrder(String user, int num){
//1、查询库存是否有剩余
//2、保存订单
String orderId = UUID.randomUUID().toString();
System.out.println("订单生产成功:" + orderId);
//3、通过mq给下游系统发送消息
String exchangeName = "direct_order_exchange";
rabbitTemplate.convertAndSend(exchangeName, "sms", orderId);
rabbitTemplate.convertAndSend(exchangeName, "email", orderId);
System.out.println("完成");
}
}(从代码中可以看到,direct_order_exchange交换机分别给绑定的路由key为sms和email的消息队列发送了消息)
4、写一个测试类,发送消息
import com.xpf.rabbitmqspringboot.service.DirectOrderService;
import com.xpf.rabbitmqspringboot.service.FanoutOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitmqSpringbootApplicationTests {
@Autowired
private FanoutOrderService fanoutOrderService;
@Autowired
private DirectOrderService directOrderService;
/**
* Direct模式生产者发送消息
*/
@Test
public void setDirectOrderService(){
directOrderService.makerOrder("用户2", 10);
}
}(先别启动测试类,因为交换机和队列的声明放在下面的消费者中。)
Ⅱ 消费者
1、新建一个springboot项目,其中pom.xml 和 application.properties和上述生产者文件相同,但是如果在一个电脑模拟同启动两个项目时,记得把application.properties中的端口换成不同的
2、使用springboot写一个配置文件 RabbitMqConfiguration.java
关于为啥在消费者中建配置文件而不是在生产者,请看rabbitmq使用springboot实现fanout模式
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfiguration {
//1、声明注册Direct模式交换机
@Bean
public DirectExchange DirectExchange(){
return new DirectExchange("direct_order_exchange", true, false);
}
//2、声明队列 sms.Direct.queue、email.Direct.queue、duanxin.Direct.queue
@Bean
public Queue smsQueue(){
return new Queue("sms.direct.queue", true);
}
@Bean
public Queue emailQueue(){
return new Queue("email.direct.queue", true);
}
@Bean
public Queue duanxinQueue(){
return new Queue("duanxin.direct.queue", true);
}
//3、完成绑定关系(队列绑定交换机)
@Bean
public Binding smsBinding(){
return BindingBuilder.bind(smsQueue()).to(DirectExchange()).with("sms");
}
@Bean
public Binding emailBinding(){
return BindingBuilder.bind(emailQueue()).to(DirectExchange()).with("email");
}
@Bean
public Binding duanxinBinding(){
return BindingBuilder.bind(duanxinQueue()).to(DirectExchange()).with("duanxin");
}
}3、写三个消费者分别监听路由key为sms、email、duanxin的消息队列
(这里举例两个 SmsDirectConsumer.java 和 EmailDirectConsumer.java)
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Author xpf
* @Date 2023/7/9 1:27
* @Version 1.0
*/
@Component
@RabbitListener(queues = "sms.direct.queue")
public class SmsDirectConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("接收到来自队列sms.direct.queue消息订单的message是:" + message);
}
}import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Author xpf
* @Date 2023/7/9 1:27
* @Version 1.0
*/
@Component
@RabbitListener(queues = "email.direct.queue")
public class EmailDirectConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("接收到来自队列email.direct.queue消息订单的message是:" + message);
}
}三、测试
1、先启动消费者,因为本项目配置类在消费者
2、启动生产者测试类
结果发现路由key为sms、email的消息队列接收到了生产者发送的消息,而duanxin没有收到,结果符合预期
到此这篇关于rabbitmq使用springboot实现direct模式的文章就介绍到这了,更多相关springboot实现direct模式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
SpringBoot项目中HTTP请求对响应体进行压缩的方法
本文我们将梳理一下 Spring Boot 项目中是如何对 HTTP 请求的响应体 (Response Body) 进行压缩,请注意:压缩是针对服务器返回给客户端的响应体,而不是客户端发给服务器的请求 URL 或请求体,需要的朋友可以参考下2025-06-06
解决java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条问题
文章解释了一个系统问题,尽管SQL写了limit 1且MyBatis的debug日志显示total为1,但仍返回了1805条数据,原因是selectOne方法只查询一条记录,若有多条则报错,而非智能筛选,文章还分析了MyBatisPlus和MyBatis中selectOne方法的不同实现2026-04-04
IDEA下SpringBoot指定环境、配置文件启动操作过程
这篇文章主要介绍了IDEA下SpringBoot指定环境、配置文件启动过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-08-08
idea左下角的Git(Version Control)中显示Local Changes窗口方式
在IDEA中,通过使用快捷键Alt+9(Windows)或Cmd+9(Mac)可以快速打开LocalChanges窗口,查看当前Git仓库的本地变更,若此方法不可用,可尝试进入settings,点击VersionControl,选择Commit,并取消Use interface的勾选2024-10-10
Java分布式锁理论(redis、zookeeper))案例详解
zookeeper有个节点路径的概念,节点路径不能重复,保证了唯一性,这篇文章给大家介绍Java分布式锁理论(redis、zookeeper) 案例详解,感兴趣的朋友跟随小编一起看看吧2024-01-01


最新评论