02-RabbitMQ整合springboot简单使用
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2. 配置yml文件
spring:
rabbitmq:
host: ip
port: 5672
password: *****
username: *****
virtual-host: /dev_virtual
3.引入springboot中的注解和类
- 启动类增加 @EnableRabbit注解
- 引入rabbitmq管理类
@Autowired
private RabbitTemplate rabbitTemplate;//用于消息处理
@Autowired
private RabbitAdmin rabbitAdmin; //用于管理组件
4.测试DIRECT模式消息队列
- 代码创建exchange和queue
@Test
public void buildRabbit(){
//交换机
Exchange exchange = new ExchangeBuilder("exchange_direct", ExchangeTypes.DIRECT).durable(true).build();
rabbitAdmin.declareExchange(exchange);
//队列
Queue queue = QueueBuilder.durable("queue_lpf").build();
rabbitAdmin.declareQueue(queue);
//绑定
Binding binding = BindingBuilder.bind(queue).to(exchange).with("mq_key").noargs();
rabbitAdmin.declareBinding(binding);
String beanName = rabbitAdmin.getBeanName();
System.out.println("buildRabbit"+beanName);
}
- provider发送消息
@Test
public void sendRabbitInfo() {
CorrelationData data = new CorrelationData(UUID.randomUUID().toString().replace("-", ""));
Book book = new Book("红楼梦","曹雪芹");
rabbitTemplate.convertAndSend("exchange_direct", "mq_key", sendMessage,data);
}
- consumer接收发送的消息
@RabbitListener(queues="queue_lpf")
public void getMessage(Map message){
System.out.println("********************************************************");
System.out.println(message);
System.out.println("********************************************************");
}
5.测试TOPIC模式发送数据
- 代码创建EXCHANGE 和 QUEUE
@Test
public void buildTopicRabbit(){
Exchange topicExchange = new ExchangeBuilder("topic_exchange", ExchangeTypes.TOPIC).durable(true).build();
rabbitAdmin.declareExchange(topicExchange);
Queue queue1 = QueueBuilder.durable("queue_topic").build();
rabbitAdmin.declareQueue(queue1);
Queue queue2 = QueueBuilder.durable("queue_lpf").build();
rabbitAdmin.declareQueue(queue2);
Queue queue3 = QueueBuilder.durable("topic_lpf").build();
rabbitAdmin.declareQueue(queue3);
//队列和交换器绑定
Binding binding = BindingBuilder.bind(queue1).to(topicExchange).with("#.topic").noargs();
Binding binding1 = BindingBuilder.bind(queue2).to(topicExchange).with("queue.#").noargs();
Binding binding2 = BindingBuilder.bind(queue3).to(topicExchange).with("#.lpf").noargs();
rabbitAdmin.declareBinding(binding);
rabbitAdmin.declareBinding(binding1);
rabbitAdmin.declareBinding(binding2);
}
- privider 发送消息
@Test
public void sendMsg(){
CorrelationData data = new CorrelationData(UUID.randomUUID().toString().replace("-", ""));
String message="this is a topic message(queue.lpf is the routingkey)";
rabbitTemplate.convertAndSend("topic_exchange","queue.lpf", (Object) message,data);
}
- 在management管理页面查看收到的信息
6.遇到的问题
问题1:org.springframework.amqp.AmqpIllegalStateException: Fatal exception on listener startup
看到很多人说是rabbitmq账号的权限问题,经过多番测试无果。
根据 报错详细,发现是有一个@RabbitListener 监听队列 ,这个队列没有创建。所以造成了这个问题。
注释掉久可以了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏