当前位置: 首页 > news >正文

Spring Boot集成RabbitMQ

目录

  • 1.RabbitMQ简介
  • 2.添加依赖
  • 3.配置RabbitMQ连接
  • 4.DirectExchange
    • 4.1 消费者
    • 4.2 生产者
    • 4.3 测试
    • 4.4 一个交换机对多个队列
    • 4.5 一个队列对多个消费者
  • 5.FanoutExchange
    • 5.1 消费者
    • 5.2 生产者
    • 5.3 测试
  • 6.TopicExchange
    • 6.1 消费者
    • 6.2 生产者

1.RabbitMQ简介

RabbitMQ是一个由Erlang语言编写的消息中间件,它遵循AMQP协议,提供了稳定可靠的消息传输服务。RabbitMQ通过其独特的架构和丰富的功能,帮助开发者解决分布式系统中的消息传递问题,提高系统的可扩展性、可靠性和响应速度。

2.添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

3.配置RabbitMQ连接

application.propertiesapplication.yml中配置RabbitMQ服务器的连接参数:

# 定义RabbitMQ的主机地址,这里使用的是局域网内的一个IP地址
spring.rabbitmq.host=192.168.131.130# 指定RabbitMQ的端口号,默认情况下RabbitMQ使用5672端口
spring.rabbitmq.port=5672# 设置RabbitMQ的用户名,这里使用的是默认的用户名guest
spring.rabbitmq.username=guest# 设置RabbitMQ的密码,这里使用的是默认的密码guest
spring.rabbitmq.password=guest# 配置RabbitMQ的虚拟主机,这里使用的是默认的虚拟主机"/"
spring.rabbitmq.virtual-host=/

4.DirectExchange

4.1 消费者

@Configuration
public class DirectConsumer {//注册一个队列@Bean  //启动多次为什么不报错?启动的时候,它会根据这个名称Direct_Q01先去查找有没有这个队列,如果有什么都不做,如果没有创建一个新的public Queue directQueue(){return   QueueBuilder.durable("Direct_Q01").maxLength(100).build();}//注册交换机@Beanpublic DirectExchange directExchange(){//1.启动的时候,它会根据这个名称Direct_E01先去查找有没有这个交换机,如果有什么都不做,如果没有创建一个新的return  ExchangeBuilder.directExchange("Direct_E01").build();}//绑定交换机与队列关系@Beanpublic Binding directBinding(Queue directQueue,DirectExchange directExchange){return BindingBuilder.bind(directQueue).to(directExchange).with("RK01");}//启动一个消费者@RabbitListener(queues = "Direct_Q01")public void receiveMessage(String msg){System.out.println("Direct_Q01收到消息:"+msg);}
}

4.2 生产者

//放入Ioc容器
@Service
public class DirectProvider {@Resource   private RabbitTemplate rabbitTemplate;//发送消息public void send(String message) {rabbitTemplate.convertAndSend("Direct_E01", "RK01", message);}
}

4.3 测试

@SpringBootTest(classes = App.class)
public class TestDirect {@Resourceprivate DirectProvider directProvider;@Testpublic void  directSendTest(){for (int i = 0; i < 10; i++) {directProvider.send("我嫩爹");}}
}

4.4 一个交换机对多个队列

多个队列

4.5 一个队列对多个消费者

多个消费者

5.FanoutExchange

5.1 消费者

@Configuration
public class FanoutConsumer {//注册一个队列@Bean  public Queue fanoutQueue(){return   QueueBuilder.durable("Fanout_Q01").maxLength(100).build();}@Bean  public Queue fanoutQueue2(){return   QueueBuilder.durable("Fanout_Q02").maxLength(100).build();}//注册交换机@Beanpublic FanoutExchange fanoutExchange(){return  ExchangeBuilder.fanoutExchange("Fanout_E01").build();}//绑定交换机与队列关系@Beanpublic Binding fanoutBinding(Queue fanoutQueue,FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);}@Beanpublic Binding fanoutBinding2(Queue fanoutQueue2,FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);}//启动一个消费者@RabbitListener(queues = "Fanout_Q01")public void receiveMessage(String msg){System.out.println("Fanout_Q01收到消息:"+msg);}//启动一个消费者@RabbitListener(queues = "Fanout_Q02")public void receiveMessage2(String msg){System.out.println("Fanout_Q02收到消息:"+msg);}}

5.2 生产者

@Service
public class FanoutProvider {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(JSONObject message) {rabbitTemplate.convertAndSend("Fanout_E01","",message.get("msg"));}
}

5.3 测试

发送请求进行测试

@RestController
@RequestMapping("/fanout")
public class FanoutController {@Resourceprivate FanoutProvider fanoutProvider;@PostMapping("/send")public void send(@RequestBody JSONObject message) {fanoutProvider.send(message);}
}

额外涉及到的一些依赖:

<!-- 封装了一些工具类  --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId>
</dependency>
<!--   之前web请求相关注解   -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

6.TopicExchange

6.1 消费者

@Configuration
public class TopicConsumer {//注册一个队列@Bean  public Queue topicQueue(){return   QueueBuilder.durable("Topic_Q01").maxLength(100).build();}@Bean  public Queue topicQueue2(){return   QueueBuilder.durable("Topic_Q02").maxLength(100).build();}//注册交换机@Beanpublic TopicExchange topicExchange(){return  ExchangeBuilder.topicExchange("Topic_E01").build();}//绑定交换机与队列关系@Beanpublic Binding topicBinding(Queue topicQueue,TopicExchange topicExchange){return BindingBuilder.bind(topicQueue).to(topicExchange).with("#");}@Beanpublic Binding topicBinding2(Queue topicQueue2,TopicExchange topicExchange){return BindingBuilder.bind(topicQueue2).to(topicExchange).with("1.8.*");}//启动一个消费者@RabbitListener(queues = "Topic_Q01")public void receiveMessage(String msg){System.out.println("Topic_Q01收到消息:"+msg);}//启动一个消费者@RabbitListener(queues = "Topic_Q02")public void receiveMessage2(String msg){System.out.println("Topic_Q02收到消息:"+msg);}}

6.2 生产者

@Service
public class TopicProvider {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(JSONObject message) {rabbitTemplate.convertAndSend("Topic_E01",message.get("routingKey").toString(),message.get("msg"));}
}
http://www.lryc.cn/news/426094.html

相关文章:

  • OLED屏幕制造工艺流程
  • knowLedge-VueCLI项目中环境变量的定义与使用
  • 【C#】 接口 继承
  • Self-Supervised Learning(李宏毅老师系列)
  • 8月16日笔记
  • 苹果Mac电脑——装macOS和Windows双系统的方法
  • 【C++ 面试 - 基础题】每日 3 题(十五)
  • 数学建模学习笔记
  • 个人可识别信息(PII) AI 去除 API 数据接口
  • 【Python-办公自动化】1秒提取PPT文本内容形成目录保存至WORD
  • maven介绍与安装
  • 瑞友科技项目经理认证负责人杨文娟受邀为第四届中国项目经理大会演讲嘉宾︱PMO评论
  • Ubuntu基础使用
  • 知识图谱结构的提示
  • (计算机网络)网络层
  • [upload]-[GXYCTF2019]BabyUpload1-笔记
  • 2023卫星视频综述论文Recent Advances in Intelligent Processing of Satellite Video
  • Mysql的Binlog的数据样例
  • 基于VS2022+Qt5+C++的串口助手开发
  • Mysql之视图
  • 【开端】Java 分页工具类运用
  • leetcode每日一题48
  • 源码工具文档手册
  • hive之greatest和least函数
  • C:数组传参的本质
  • excel 2019版本的index match搜索功能
  • 【问题解决】apache.poi 3.1.4版本升级到 5.2.3,导出文件报错版本无法解析
  • (亲测有效)SpringBoot项目集成腾讯云COS对象存储(2)
  • 界面优化 - QSS
  • 实现基于TCP协议的服务器与客户机间简单通信