Springboot整合RabbitMq-用心看完这一篇就够了(最新)
SpringBoot-整合MQ---目录
- 第一步创建一个springboot的项目
- 1---HelloWorld模型
- 2---Work模型
- 3---Fanout模式
- 4---direct路由模式
- 5---主题模式topics
第一步创建一个springboot的项目
创建springboot项目 选好所需要的骨架
1—HelloWorld模型
springboot-整合mq-第一种模型 Hello World 简单模式
一个生产者丶默认交换机丶一个队列丶一个消费者。
第一步配置好自己的yml
server:port: 8001spring:rabbitmq:username: guest password: guest virtual-host: / port: 5672host: 192.168.192.168
创建生产者
第一个:HelloWorld模型
1、一个生产者
2、默认交换机
3、一个队列
4、一个消费者
@SpringBootTest
// 1--生产者
class SpringBootBuliangrenApplicationTests {@Autowired//RabbitTemplate是模板对象,用来简化mq操作private RabbitTemplate rabbitTemplate;
// 2--不用写就是默认交换机
// 3--队列名称是:hello
// 发送的消息是HelloWorld@Testvoid contextLoads() { // 3--队列名字是:hellorabbitTemplate.convertAndSend("hello", "HelloWorld");}
}
创建消费者
@Component//@RabbitListener 这个注解代表消费者的 一个监听//消费者监听hello队列
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {//消费者 - 消费 - 提供者 的消息@RabbitHandlerpublic void receival(String message){System.out.println("消费接收到了消息----->"+message);}
}
运行
此时hello队列也创建出了
2—Work模型
springboot-整合mq-第二种模型:work-工作模式
一个生产者丶默认交换机丶一个队列丶多个消费者。
第一步配置好自己的yml
server:port: 8001spring:rabbitmq:username: guest password: guest virtual-host: / port: 5672host: 192.168.192.168
创建生产者
第二个:Work模型
1、一个生产者
2、默认交换机
3、一个队列
4、多个消费者
@SpringBootTest //1 一个生产者
class SpringBootBuliangren2ApplicationTests {// 2--不用写就是默认交换机@Autowired //RabbitTemplate是模板对象,用来简化mq操作private RabbitTemplate rabbitTemplatel;
// 3--队列名称是:workTest
//发送的消息是hello,work模型 我们发送多条消息 应为有多个消费者@Testvoid contextLoads() {//消费者发送10 条消息for (int i = 0;i<10;i++){rabbitTemplatel.convertAndSend("workTest","hello,work模型");}}
}
创建消费者
@Component
public class HelloCustomer {//1号消费者 ---监听workTest队列@RabbitListener(queuesToDeclare = @Queue("workTest"))public void receive01(String message){//消费者 - 消费 - 提供者 的消息System.out.println("消费者1号 - 接收到了消息----->"+message);}//2号消费者 ---监听workTest队列@RabbitListener(queuesToDeclare = @Queue("workTest"))public void receive02(String message){//消费者 - 消费 - 提供者 的消息System.out.println("消费者2号 - 接收到了消息----->"+message);}//3号消费者 ---监听workTest队列@RabbitListener(queuesToDeclare = @Queue("workTest"))public void receive03(String message){//消费者 - 消费 - 提供者 的消息System.out.println("消费者3号 - 接收到了消息----->"+message);}
}
运行
此时workTest队列也创建出了
3—Fanout模式
springboot-整合mq-第三种模型:Fanout-广播模型-订阅模式
多个消费者,每一个消费这都有自己的队列,每个队列都绑定到交换机
生产者发送消息到交换机-交换机发送到哪个队列
第一步配置好自己的yml
server:port: 8003spring:rabbitmq:virtual-host: /host: 192.168.192.168port: 5672username: guestpassword: guest
创建生产者
第三个:Fanout模式
1、一个生产者
2、一个交换机
3、多个队列
4、多个消费者
@SpringBootTest//1 一个生产者
class SpringBootBuliangren3ApplicationTests {@Autowired//RabbitTemplate是模板对象,用来简化mq操作private RabbitTemplate rabbitTemplate;@Testvoid contextLoads() {//参数: 交换机名称, 路由, 生产者发送的消息//我们使用for循环 ---发送多条消息 ---应为有多个消费者for (int i = 0; i < 10; i++) {rabbitTemplate.convertAndSend("fanoutlogs", "", "Fanout模型发送的消息");}}
}
创建消费者
@Component//创建消费者
public class FanoutCustomer {@RabbitListener(bindings = {@QueueBinding(value = @Queue("FanoutTest"),//创建的队列名称exchange = @Exchange(value = "fanoutlogs",//绑定的交换机type = "fanout"))})//fanout类型public void receice01(String message) {System.out.println("消费者1号 - 接收到了消息----->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("FanoutTest"),//创建的队列名称exchange = @Exchange(value = "fanoutlogs",//绑定的交换机type = "fanout"))})//fanout类型public void receice02(String message) {System.out.println("消费者2号 - 接收到了消息----->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("FanoutTest"),//创建的队列名称exchange = @Exchange(value = "fanoutlogs",//绑定的交换机type = "fanout"))})//fanout类型public void receice03(String message) {System.out.println("消费者3号 - 接收到了消息----->" + message);}
}
运行
此时FanoutTest队列也创建出了
此时fanoutlogs交换机也创建出了
4—direct路由模式
springboot-整合mq-第四种模型:-路由模型-direct
根据指定路由的key转向不同的队列
第一步配置好自己的yml
server:port: 8004
spring:rabbitmq:port: 5672host: 192.168.192.168virtual-host: /username: guestpassword: guest
创建生产者
第四个:direct路由模式
1、一个生产者
2、一个交换机
3、多个队列
4、多个消费者
根据不同的key 接收不同的消息
创建生产者
@SpringBootTest
class SpringBootBuliangren4ApplicationTests {@Autowired //RabbitTemplate是模板对象,用来简化mq操作private RabbitTemplate rabbitTemplate;//路由模式@Testvoid contextLoads() { 参数一:是交换机参数二:队列参数三:消息rabbitTemplate.convertAndSend("directlogs", "info", "发送info的key的路由信息");rabbitTemplate.convertAndSend("directlogs", "warn", "发送warn的key的路由信息");}
}
创建消费者
@Component
public class RouteCusstomer {@RabbitListener(bindings = {@QueueBinding(value = @Queue("directTest01"),//创建队列名字exchange = @Exchange(value = "directlogs"),//绑定的交换机key = {"info" //指定的key})})public void recrvel01(String message) {System.out.println("消费者1号: 接收的消息是---->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("directTest02"),//创建队列的名字exchange = @Exchange(value = "directlogs"),//绑定的交换机key = {"error" //指定的key})})public void recrvel02(String message) {System.out.println("消费者2号: 接收的消息是---->" + message);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("directTest03"),//创建队列的名字exchange = @Exchange(value = "directlogs"),//绑定的交换机key = {"warn" //指定的key})})public void recrvel03(String message) {System.out.println("消费者3号: 接收的消息是---->" + message);}
}
运行
此时directTest队列也创建出了
此时交换机也创建出了
5—主题模式topics
springboot-整合mq-第五种模型:主题模型-Topics
第五个:动态路由模式Topics
#号 可以代替零个或多个单词.
*号 可以代替一个完整的单词
第一步配置好自己的yml
server:port: 8005spring:rabbitmq:host: 192.168.192.168port: 5672username: guestpassword: guestvirtual-host: /
创建生产者
@SpringBootTest
class SpringBootBuliangren5ApplicationTests {@Autowired //rabbitmqtemplate 是简化mq开发private RabbitTemplate rabbitTemplate;@Testvoid contextLoads() { //参数1:交换机 参数2:key 参数3:生产者的消息rabbitTemplate.convertAndSend("topicsTest","a.user.save","user.save-----消息");}
}
创建消费者
@Component//消费者
public class TopicCusstomer {@RabbitListener(bindings = {@QueueBinding(value = @Queue("topiceTest01"),//创建的队列exchange = @Exchange(type = "topic",name = "topicsTest"),//topic类型绑定的交换机topicsTestkey = {"#.user.#"})})public void recevicel01(String messge){System.out.println("消费者1号: 接收的消息是---->"+messge);}@RabbitListener(bindings = {@QueueBinding(value = @Queue("topiceTest02"),//创建的队列exchange = @Exchange(type = "topic",name = "topicsTest"),//topic类型绑定的交换机topicsTestkey = {"*.user","user.*"})})public void recevicel02(String messge){System.out.println("消费者2号: 接收的消息是---->"+messge);}
}
运行
学习是一步一步来的不要跳过
Springboot整合RabbitMq-用心看完这一篇就够了(最新)相关推荐
- Springboot 整合RabbitMq ,用心看完这一篇就够了
该篇文章内容较多,包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct.Topic.Fanout的使用,消息回调.手动确认等. (但是 ...
- RabbitMQ详解,用心看完这一篇就够了【重点】
1.1 消息中间件 消息中间件是基于队列与消息传递技术,在网络环境中为应用系统提供同步或异步.可靠的消息传输的支撑性软件系统--百度百科 1.1.1 应用场景 1.1.1.1 异步处理 场景说明: 用 ...
- 分享一波关于做Kaggle比赛,Jdata,天池的经验,看完我这篇就够了。
@Author : Jasperyang @School : BUPT 这篇文章同时在知乎里放着- 写在前面 Kaggle的数据挖掘比赛近年来很火,以至于中国兴起了很多很多类似的比赛,做了两个这种类型 ...
- 网络安全工程师入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
前言 想要成为网络安全工程师,却苦于没有方向,不知道从何学起的话,下面这篇 网络安全入门 教程可以帮你实现自己的网络安全工程师梦想,如果想学,可以继续看下去,文章有点长,希望你可以耐心看到最后! 1. ...
- 程序员入门教程【非常详细】从零基础入门到精通,看完这一篇就够了 !
零基础编程入门先学什么?编程语言有几百种,我们应该怎么选择.想学习编程,加入互联网行业,哪一个更有前途?在小白学习编程会有各种各样的问题,今天小编我就来为你解答. 一.怎么选择编程语言 编程语言有很多 ...
- 网络安全工程师需要学哪些内容?零基础如何从入门到精通,看完这一篇就够了
网络安全工程师需要学习哪些内容? 作为一名网络安全工程师,需要学习以下内容: 计算机网络和操作系统:网络安全工程师需要了解计算机网络和操作系统的基础知识,以便理解网络安全的基础原理. 网络协议和技术: ...
- 网络安全入门教程(非常详细)从零基础入门到精通,看完这一篇就够了!
想要成为黑客/红客,却苦于没有方向,不知道从何学起,下面这篇网络安全入门教程可以帮你实现自己的黑客梦想,如果想学,可以继续看下去,文章有点长,希望你可以耐心看到最后 网络安全入门路线 Web安全相关概 ...
- springboot整合webSocket(看完即入门)
webSocket 1.什么是webSocket? 2.webSocket可以用来做什么? 3.webSocket协议 4.服务端 WebSocket操作类 5.客户端 1.什么是webSocket? ...
- beeline执行sql文件_MyBatis的SQL执行流程不清楚?看完这一篇就够了
前言 MyBatis可能很多人都一直在用,但是MyBatis的SQL执行流程可能并不是所有人都清楚了,那么既然进来了,通读本文你将收获如下: 1.Mapper接口和映射文件是如何进行绑定的 2.MyB ...
最新文章
- canvas初体验之加载图片
- windows与linux下的\r\n
- C语言(CED)输出前k大的数(分治法/局部快速排序):给定一个数组,统计前k大的数并且把这k个数从大到小输出。
- 面试官: MySQL 数据库的优化,你知道有哪些?
- 【数据治理】数据元、元数据、主数据、参考数据概述
- 苹果mac能安装计算机题库吗,苹果电脑能装windows系统吗_苹果电脑安装windows系统的方法...
- python探究小市值因子的有效性
- 桌面时间的最佳管理者-软媒时间
- 第四讲 系统建模方法
- Git报错remote: error: hook declined to update refs/heads/feature/XXX
- Apache POI + Office Word 生成页码
- 跟我学Linux系统管理 第三讲 磁盘管理
- 遇见你是我最美的意外
- 传感器课程实验:传感器静态特性研究-应变式传感器
- 游戏输入控制利器:DirectInput
- aac蓝牙编解码协议_【科普】蓝牙耳机主流编码格式(SBC、AAC、aptX)
- 5G/NR 5G核心网(5GC)之网络功能服务通信
- mysql小练习--有题有数据
- 61、智能指针类模板
- BACKUP DATABASE
热门文章
- position: relative;导致页面卡顿
- .NET实现之(WebService数据提供程序)
- 【转】Cannot find -ltinfo when compiling android 4.0.3
- 关于编写性能高效的javascript事件的技术
- 编写一个判断素数的函数,在主函数输入一个整数时,输出是否素数的信息。...
- Android实现数据存储技术
- 数据库优化的几条基本策略
- python中frozenset( )和set()的用法区别
- iBATIS.net获取运行时sql语句
- windows2008下配置iis时出现错误“由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。”...