什么是mq:
mq就是消息队列,能帮助我们实现上下游的消息传递,是一种消息跨进程的通信机制
为什么要用mq:
流量消峰
假如订单系统最多只能处理一万次订单,但是在高峰期,超过了一万订单之后就会崩。这时候就要我们做消息队列缓冲,让访问的人员进行排队,来达到消峰的目的。访问的速度会有所下降,但是总比崩盘了好。
![](https://img-blog.csdnimg.cn/img_convert/1e4036a39980ba948a96b27365563108.png)
应用解耦
以电商系统为例,应用中订单系统需要调用库存系统、支付系统、物流系统,如果在调用过程中,任何一个系统调用失败,那么就会下单异常。
如果我们用消息队列,由消息队列发送消息给库存系统、支付系统、物流系统,如果有系统故障,那么消息队列会一直监督他完成。
![](https://img-blog.csdnimg.cn/img_convert/092c4847d07a8986931289182ca84c8f.png)
异步处理
![](https://img-blog.csdnimg.cn/img_convert/a9c09cbcce7526f68b78ee66499e9745.png)
RabbitMq的概念:
![](https://img-blog.csdnimg.cn/img_convert/78082cddca1c18f12e0ad93fc791c8bc.png)
四大核心:
![](https://img-blog.csdnimg.cn/img_convert/d981c0e712677853131bb28582a6e66d.png)
![](https://img-blog.csdnimg.cn/img_convert/5fc83e2a982d596f3239d5c13a24d00b.png)
可以用发快递来举例:
![](https://img-blog.csdnimg.cn/img_convert/1b83165a681a44e6ca680b1c8f77846e.png)
RabbitMQ工作原理
![](https://img-blog.csdnimg.cn/img_convert/fdc893e850c44d7cadf3079313a0a50a.png)
![](https://img-blog.csdnimg.cn/img_convert/d7df47d56681629d1eed19d8dacd2354.png)
![](https://img-blog.csdnimg.cn/img_convert/a5f8d5d89b8538f8c05dcdc521802d28.png)
生产者代码:
代码可以对应上图,都是根据图中的步骤来的。
首先创建工厂,设置完成地址,用户名和密码。第二步就是创建链接,然后创建信道,通过信道去链接交换机,交换机链接队列,也可以通过默认交换机去链接队列,最后再通过链接中的信道,发消息给消费者。
![](https://img-blog.csdnimg.cn/img_convert/fb12040e4ff5f0cec24336cba6b8c91c.png)
![](https://img-blog.csdnimg.cn/img_convert/d0574cc9fc76e0036ac5c3b0a4fef094.png)
通过信道发送消息
![](https://img-blog.csdnimg.cn/img_convert/b1df291ac081191ad3894f946291eb6d.png)
消息发送完毕之后,我们可以从Rabbit后台界面当中找到队列
![](https://img-blog.csdnimg.cn/img_convert/ed3dc870423194b10887a4aeb9ade4a0.png)
消费者代码
创建工厂
创建连接、信道
声明 接收消息
取消消息时的回调
![](https://img-blog.csdnimg.cn/img_convert/bc9eac2be5f5c81866fb4549441695e3.png)
![](https://img-blog.csdnimg.cn/img_convert/e209ff9bdce1cf680cc42f45d403b3dd.png)
Rabbit工作队列原理:
![](https://img-blog.csdnimg.cn/img_convert/a18f29ce1de7ff566f4c706889fd1d6e.png)
![](https://img-blog.csdnimg.cn/img_convert/ecd2743e3ae9fc261e6ae1c56a536189.png)
简单俩说就是生产者发送了大量的消息,有多个消费者接收处理消息,消费者之间是竞争关系,消息轮训接收。
消息应答概念
![](https://img-blog.csdnimg.cn/img_convert/22a0e4326f9519797df9d67f7cdcaf51.png)
消息应答分为自动应答和手动应答
自动应答接收到消息后,马上就会应答,不一定完没完成工作,所以并不靠谱,需要一个优良的环境下才能用自动应答。
手动应答的方法:
![](https://img-blog.csdnimg.cn/img_convert/56f8bf891c314ed1d6ed2c4fa2554daa.png)
手动应答的好处就是可以批量应答,减少网络堵塞
![](https://img-blog.csdnimg.cn/img_convert/4c572e77589f77e88ffab225cdcab71c.png)
建议不要批量应答,因为如果有A、B、C三个消息,如果当前执行完的是C那么就应答C,如果A、B没有执行完成,批量应答也会进行应答,也可能会出现消息丢失的情况,所以尽量不要批量应答。
消息重新入队
![](https://img-blog.csdnimg.cn/img_convert/68c5279ee8b98adc92a63fe0705139d2.png)
![](https://img-blog.csdnimg.cn/img_convert/dbb223488fbcc6a6d74656df3e112785.png)
向C1、C2中发送消息,如果C1宕机了,出现了故障,那么这个数据不会丢失,会重新返回队列,然后再交给C2进行处理。
Rabbit持久化
Rabbit是可以进行持久化的,可以将消息和队列进行持久化,持久化的目的就是为了保证数据不丢失。
队列持久化:
![](https://img-blog.csdnimg.cn/img_convert/d1f61c78abb9aae45e831227c284b4dc.png)
队列如果不做持久化,如果Rabbit重启,该队列就会被删除掉
![](https://img-blog.csdnimg.cn/img_convert/bc3630ff5bbdaf6d1d756102f2dcb79c.png)
![](https://img-blog.csdnimg.cn/img_convert/09a5dbcf1edcc74232e900115c58bf8c.png)
消息持久化:
如果队列做了持久化,消息没做持久化,当队列遇到故障重启了,消息就会丢失。
![](https://img-blog.csdnimg.cn/img_convert/3597bd5050e9020665fedbe406fe7bd0.png)
不公平分发:
![](https://img-blog.csdnimg.cn/img_convert/669395570c48bbb4b845e9af5c423e8e.png)
预取值分发:可以设定每个消费者接收多少条消息
![](https://img-blog.csdnimg.cn/img_convert/84dbf9557f50ae86f4009fa7e75833fd.png)
![](https://img-blog.csdnimg.cn/img_convert/5d936deb44003723f082b61627a63abd.png)
确认发布:
单个确认发布:
![](https://img-blog.csdnimg.cn/img_convert/bc93e91ff51ad11024337448d5538c0b.png)
批量确认发布:
![](https://img-blog.csdnimg.cn/img_convert/cbe92467da78ce9327a325c178d9024e.png)
异步确认发布:
![](https://img-blog.csdnimg.cn/img_convert/cb4ab873dc9130c4d7d19a50a0f8b890.png)
需要写一个监听器
![](https://img-blog.csdnimg.cn/img_convert/d06db48a9f8ad9ac14bf81650514ef1f.png)
![](https://img-blog.csdnimg.cn/img_convert/04aa6379e44a67ca8b21f847955a8f14.png)
如何处理异步为确认消息?
![](https://img-blog.csdnimg.cn/img_convert/20daad5347de07789570bdfa5038a9fa.png)
在开启消息发布确认下面,写一个线程安全有序的一个哈希表 适用于高并发的情况下
![](https://img-blog.csdnimg.cn/img_convert/c4aa1d5a5bc2da093faf04a07762200e.png)
之后在发消息的时候进行记录,每发一次消息记录一次
![](https://img-blog.csdnimg.cn/img_convert/544d14164e7cb3add98bb973c7b7862c.png)
删除已经确认的消息
![](https://img-blog.csdnimg.cn/img_convert/90bd6a8e21ba14d7a02be70e9de7e192.png)
RabbitMq交换机的作用
之前说的都是简单模式,不需要交换机
下面说的就是发布订阅模式,需要交换机
交换机的概念:
![](https://img-blog.csdnimg.cn/img_convert/09868e95e43dc5e1a9efeec934835ceb.png)
交换机类型:
![](https://img-blog.csdnimg.cn/img_convert/f3fae4cde466cc9e32173985def8e983.png)
![](https://img-blog.csdnimg.cn/img_convert/6703159da3cc4fd2543db0f9ba082245.png)
临时队列:就是不做持久化的,结束之后队列将被自动删除
![](https://img-blog.csdnimg.cn/img_convert/419b233f9866941dc192eb29042a7d68.png)
![](https://img-blog.csdnimg.cn/img_convert/1f820c6bd793de1765d8fbf0909aa0e1.png)
![](https://img-blog.csdnimg.cn/img_convert/cb01ed1256b0c7e6f57f210311f9a04b.png)