消息中间件-RabbitMQ

本文详细介绍了RabbitMQ消息中间件,包括其概念、安装、运行机制、消息确认机制、延时队列的实现以及如何确保消息的可靠性。通过SpringBoot整合RabbitMQ,展示了如何使用不同类型的exchange,并提供了代码示例。同时,文章讨论了如何处理消息丢失、重复和积压问题,为实际应用提供了指导。
摘要由CSDN通过智能技术生成

消息队列应用场景

image-20200716220330316

image-20200716220349030

消息中间件概述

image-20200716220452119

image-20200716220508446

image-20200716220524410

image-20200716220538166

RabbitMQ 概念

image-20200716220548181

image-20200716220600626

image-20200716220610376

image-20200716220646276

一个客户端只与消息中间件建立一条链接(长连接),一条链接里面有多个channel(虚拟链接,复用一条TCP连接)

虚拟主机是rabbitmq为了隔离不同类型客户端出现的概念。我们在开发过程中可以一个客户端一个虚拟主机。即一个rabbitmq里面可以有多个虚拟主机

虚拟主机:多个交换机构成虚拟主机,主要目的是隔离客户端的消息。比如当前消息队列的生产者有java端和php端,为了隔离开我们可以划分出两个虚拟主机。虚拟主机互相隔离,一台虚拟主机出现问题,不会影响到别的虚拟主机

虚拟主机是按照路径来划分的。

流程:无论是生产者往mq发送消息,还是消费者接受mq的消息,都会建立一条连接,所有的数据都会在连接里面开辟信道来进行收发。收发的消息分成两部分,消息头和消息体。消息头就是消息的属性信息,消息体就是消息的真正内容,消息里面还需要定义发给那个交换机,那个虚拟主机,消息头里面最重要就是route-key。发消息时,消息会来到rabbitmq里面指定的虚拟主机中,然后到达指定的交换机。交换机通过消息里面的route-key和绑定的队列的路由键,决定将消息发送给与交换机绑定的哪些队列中。如果队列放入的消息,监听队列的消费者就会得到队列里面的数据。一个客户端建立一条长连接的好处有,mq能实时的知道那个消费端断线了,从而及时的将队列里面的数据保存起来(停止出队),防止消息丢失。

docker 安装rabbitmq

docker run -d \
-p 5671:5671 \
-p 5672:5672 \
-p 4369:4369 \
-p 25672:25672 \
-p 15671:15671 \
-p 15672:15672 \
--restart=always --name rabbitmq rabbitmq:3-management

默认账户密码是guest
4369, 25672 (Erlang发现&集群端口)
5672, 5671 (AMQP端口)
15672 (web管理后台端口)
61613, 61614 (STOMP协议端口)
1883, 8883 (MQTT协议端口)
https://www.rabbitmq.com/networking.html

RabbitMQ的运行机制

image-20200716220703348 image-20200716220722776 image-20200716220746057

一个交换机可以绑定多个队列,一个队列可以绑定多个交换机。交换机决定要按照什么绑定关系路由给哪个消息队列。

交换机的类型:direct、fanout(扇出)、topic、headers。direct和headers 是点对点的实现、fanout和topic是发布订阅模式的实现。而header性能低下,现在都只用direct、fanout、topic这三种不同类型的交换机。

发消息是将消息发送给虚拟主机里面的交换机,消费消息是监听队列。

direct:直接类型交换机。按照路由键精确匹配到一个队列。

fanout:广播类型交换机。消息会发送给交换机下面绑定的所有队列,不管路由键。

topic:发布订阅模式交换机。可根据路由键进行模糊匹配。#匹配0个或多个单词,* 匹配一个单词。

注意:路由键是有单词组成的,单词之间使用 . 分割,这一点是理解topic模糊匹配的关键。

测试一下三种不同类型的exchange

image-20200716162659015

导出的配置(是json文件,xxx.json 导入即可)

{
   "rabbit_version":"3.8.5","rabbitmq_version":"3.8.5","product_name":"RabbitMQ","product_version":"3.8.5","users":[{
   "name":"guest","password_hash":"VLq+EgZQzd1z32LOZ83Onxc/MFCJcoMPf4jynGCdC9Aqvc6B","hashing_algorithm":"rabbit_password_hashing_sha256","tags":"administrator"}],"vhosts":[{
   "name":"/"}],"permissions":[{
   "user":"guest","vhost":"/","configure":".*","write":".*","read":".*"}],"topic_permissions":[],"parameters":[],"global_parameters":[{
   "name":"cluster_name","value":"rabbit@7072bb66c978"},{
   "name":"internal_cluster_id","value":"rabbitmq-cluster-id-9wl1WHjPlMGESsZsgSTH_g"}],"policies":[],"queues":[{
   "name":"atguigu.news","vhost":"/","durable":true,"auto_delete":false,"arguments":{
   "x-queue-type":"classic"}},{
   "name":"atguigu","vhost":"/","durable":true,"auto_delete":false,"arguments":{
   "x-queue-type":"classic"}},{
   "name":"gulixueyuan.news","vhost":"/","durable":true,"auto_delete":false,"arguments":{
   "x-queue-type":"classic"}},{
   "name":"atguigu.emps","vhost":"/","durable":true,"auto_delete":false,"arguments":{
   "x-queue-type":"classic"}}],"exchanges":[{
   "name":"exchange.fanout","vhost":"/","type":"fanout","durable":true,"auto_delete":false,"internal":false,"arguments":{
   }},{
   "name":"exchange.topic","vhost":"/","type":"topic","durable":true,"auto_delete":false,"internal":false,"arguments":{
   }},{
   "name":"my.exchange.direct","vhost":"/","type":"direct","durable":true,"auto_delete":false,"internal":false,"arguments":{
   }},{
   "name":"exchange.direct","vhost":"/","type":"direct","durable":true,"auto_delete":false,"internal":false,"arguments":{
   }}],"bindings":[{
   "source":"exchange.direct","vhost":"/","destination":"atguigu","destination_type":"queue","routing_key":"atguigu","arguments":{
   }},{
   "source":"exchange.direct","vhost":"/","destination":"atguigu.emps","destination_type":"queue","routing_key":"atguigu.emps","arguments":{
   }},{
   "source":"exchange.direct","vhost":"/","destination":"atguigu.news","destination_type":"queue","routing_key":"atguigu.news","arguments":{
   }},{
   "source":"exchange.direct","vhost":"/","destination":"gulixueyuan.news","destination_type":"queue","routing_key":"gulixueyuan.news","arguments":{
   }},{
   "source":"exchange.fanout","vhost":"/","destination":"atguigu","destination_type":"queue","routing_key":"atguigu","arguments":{
   }},{
   "source":"exchange.fanout","vhost":"/","destination":"atguigu.emps","destination_type":"queue","routing_key":"atguigu.emps","arguments":{
   }},{
   "source":"exchange.fanout","vhost":"/","destination":"atguigu.news","destination_type":"queue","routing_key":"atguigu.news","arguments":{
   }},{
   "source":"exchange.fanout","vhost":
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值