> 技术文档 > RabbitMQ 详解

RabbitMQ 详解


RabbitMQ 详解

一、RabbitMQ 基本概念

RabbitMQ 是一个开源的消息代理和队列服务器,实现了高级消息队列协议(AMQP),用于在分布式系统中存储和转发消息。

核心组件

组件 说明 Producer 消息生产者,发送消息到Exchange Consumer 消息消费者,从队列接收消息 Exchange 接收生产者消息并根据规则路由到队列 Queue 存储消息的缓冲区 Binding Exchange和Queue之间的连接规则 Channel TCP连接中的虚拟连接,多路复用 Virtual Host 虚拟隔离环境,包含独立的Exchange和Queue

二、RabbitMQ 核心工作模式

1. 简单模式 (Simple)

#mermaid-svg-8hopRIsJUdTIcfEx {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-8hopRIsJUdTIcfEx .error-icon{fill:#552222;}#mermaid-svg-8hopRIsJUdTIcfEx .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8hopRIsJUdTIcfEx .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-8hopRIsJUdTIcfEx .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8hopRIsJUdTIcfEx .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8hopRIsJUdTIcfEx .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8hopRIsJUdTIcfEx .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8hopRIsJUdTIcfEx .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8hopRIsJUdTIcfEx .marker.cross{stroke:#333333;}#mermaid-svg-8hopRIsJUdTIcfEx svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8hopRIsJUdTIcfEx .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-8hopRIsJUdTIcfEx .cluster-label text{fill:#333;}#mermaid-svg-8hopRIsJUdTIcfEx .cluster-label span{color:#333;}#mermaid-svg-8hopRIsJUdTIcfEx .label text,#mermaid-svg-8hopRIsJUdTIcfEx span{fill:#333;color:#333;}#mermaid-svg-8hopRIsJUdTIcfEx .node rect,#mermaid-svg-8hopRIsJUdTIcfEx .node circle,#mermaid-svg-8hopRIsJUdTIcfEx .node ellipse,#mermaid-svg-8hopRIsJUdTIcfEx .node polygon,#mermaid-svg-8hopRIsJUdTIcfEx .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8hopRIsJUdTIcfEx .node .label{text-align:center;}#mermaid-svg-8hopRIsJUdTIcfEx .node.clickable{cursor:pointer;}#mermaid-svg-8hopRIsJUdTIcfEx .arrowheadPath{fill:#333333;}#mermaid-svg-8hopRIsJUdTIcfEx .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8hopRIsJUdTIcfEx .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8hopRIsJUdTIcfEx .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-8hopRIsJUdTIcfEx .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-8hopRIsJUdTIcfEx .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8hopRIsJUdTIcfEx .cluster text{fill:#333;}#mermaid-svg-8hopRIsJUdTIcfEx .cluster span{color:#333;}#mermaid-svg-8hopRIsJUdTIcfEx div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-8hopRIsJUdTIcfEx :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}ProducerQueueConsumer

  • 单一生产者、单一队列、单一消费者
  • 不需要定义Exchange

2. 工作队列模式 (Work Queue)

#mermaid-svg-vpjI9LWjIYWKRIJm {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm .error-icon{fill:#552222;}#mermaid-svg-vpjI9LWjIYWKRIJm .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vpjI9LWjIYWKRIJm .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-vpjI9LWjIYWKRIJm .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vpjI9LWjIYWKRIJm .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vpjI9LWjIYWKRIJm .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vpjI9LWjIYWKRIJm .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vpjI9LWjIYWKRIJm .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vpjI9LWjIYWKRIJm .marker.cross{stroke:#333333;}#mermaid-svg-vpjI9LWjIYWKRIJm svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vpjI9LWjIYWKRIJm .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm .cluster-label text{fill:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm .cluster-label span{color:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm .label text,#mermaid-svg-vpjI9LWjIYWKRIJm span{fill:#333;color:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm .node rect,#mermaid-svg-vpjI9LWjIYWKRIJm .node circle,#mermaid-svg-vpjI9LWjIYWKRIJm .node ellipse,#mermaid-svg-vpjI9LWjIYWKRIJm .node polygon,#mermaid-svg-vpjI9LWjIYWKRIJm .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vpjI9LWjIYWKRIJm .node .label{text-align:center;}#mermaid-svg-vpjI9LWjIYWKRIJm .node.clickable{cursor:pointer;}#mermaid-svg-vpjI9LWjIYWKRIJm .arrowheadPath{fill:#333333;}#mermaid-svg-vpjI9LWjIYWKRIJm .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vpjI9LWjIYWKRIJm .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vpjI9LWjIYWKRIJm .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-vpjI9LWjIYWKRIJm .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-vpjI9LWjIYWKRIJm .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vpjI9LWjIYWKRIJm .cluster text{fill:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm .cluster span{color:#333;}#mermaid-svg-vpjI9LWjIYWKRIJm div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vpjI9LWjIYWKRIJm :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}ProducerQueueConsumer1Consumer2

  • 单一队列,多个消费者竞争消费
  • 默认轮询分发(Round-robin)

3. 发布/订阅模式 (Publish/Subscribe)

#mermaid-svg-1gQj0BJHQKxWQk0y {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y .error-icon{fill:#552222;}#mermaid-svg-1gQj0BJHQKxWQk0y .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1gQj0BJHQKxWQk0y .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-1gQj0BJHQKxWQk0y .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1gQj0BJHQKxWQk0y .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1gQj0BJHQKxWQk0y .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1gQj0BJHQKxWQk0y .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1gQj0BJHQKxWQk0y .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1gQj0BJHQKxWQk0y .marker.cross{stroke:#333333;}#mermaid-svg-1gQj0BJHQKxWQk0y svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1gQj0BJHQKxWQk0y .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y .cluster-label text{fill:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y .cluster-label span{color:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y .label text,#mermaid-svg-1gQj0BJHQKxWQk0y span{fill:#333;color:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y .node rect,#mermaid-svg-1gQj0BJHQKxWQk0y .node circle,#mermaid-svg-1gQj0BJHQKxWQk0y .node ellipse,#mermaid-svg-1gQj0BJHQKxWQk0y .node polygon,#mermaid-svg-1gQj0BJHQKxWQk0y .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1gQj0BJHQKxWQk0y .node .label{text-align:center;}#mermaid-svg-1gQj0BJHQKxWQk0y .node.clickable{cursor:pointer;}#mermaid-svg-1gQj0BJHQKxWQk0y .arrowheadPath{fill:#333333;}#mermaid-svg-1gQj0BJHQKxWQk0y .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-1gQj0BJHQKxWQk0y .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-1gQj0BJHQKxWQk0y .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-1gQj0BJHQKxWQk0y .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-1gQj0BJHQKxWQk0y .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-1gQj0BJHQKxWQk0y .cluster text{fill:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y .cluster span{color:#333;}#mermaid-svg-1gQj0BJHQKxWQk0y div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-1gQj0BJHQKxWQk0y :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}ProducerExchangeQueue1Queue2Consumer1Consumer2

  • 使用Fanout类型Exchange
  • 消息广播到所有绑定队列

4. 路由模式 (Routing)

#mermaid-svg-pnoBoAhyh64ZjO5g {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g .error-icon{fill:#552222;}#mermaid-svg-pnoBoAhyh64ZjO5g .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-pnoBoAhyh64ZjO5g .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-pnoBoAhyh64ZjO5g .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-pnoBoAhyh64ZjO5g .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-pnoBoAhyh64ZjO5g .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-pnoBoAhyh64ZjO5g .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-pnoBoAhyh64ZjO5g .marker{fill:#333333;stroke:#333333;}#mermaid-svg-pnoBoAhyh64ZjO5g .marker.cross{stroke:#333333;}#mermaid-svg-pnoBoAhyh64ZjO5g svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-pnoBoAhyh64ZjO5g .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g .cluster-label text{fill:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g .cluster-label span{color:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g .label text,#mermaid-svg-pnoBoAhyh64ZjO5g span{fill:#333;color:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g .node rect,#mermaid-svg-pnoBoAhyh64ZjO5g .node circle,#mermaid-svg-pnoBoAhyh64ZjO5g .node ellipse,#mermaid-svg-pnoBoAhyh64ZjO5g .node polygon,#mermaid-svg-pnoBoAhyh64ZjO5g .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-pnoBoAhyh64ZjO5g .node .label{text-align:center;}#mermaid-svg-pnoBoAhyh64ZjO5g .node.clickable{cursor:pointer;}#mermaid-svg-pnoBoAhyh64ZjO5g .arrowheadPath{fill:#333333;}#mermaid-svg-pnoBoAhyh64ZjO5g .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-pnoBoAhyh64ZjO5g .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-pnoBoAhyh64ZjO5g .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-pnoBoAhyh64ZjO5g .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-pnoBoAhyh64ZjO5g .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-pnoBoAhyh64ZjO5g .cluster text{fill:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g .cluster span{color:#333;}#mermaid-svg-pnoBoAhyh64ZjO5g div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-pnoBoAhyh64ZjO5g :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}errorinfoProducerExchangeQueue1Queue2Consumer1Consumer2

  • 使用Direct类型Exchange
  • 基于Routing Key精确匹配

5. 主题模式 (Topics)

#mermaid-svg-9yuvWRaNewThpg0R {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-9yuvWRaNewThpg0R .error-icon{fill:#552222;}#mermaid-svg-9yuvWRaNewThpg0R .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9yuvWRaNewThpg0R .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-9yuvWRaNewThpg0R .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9yuvWRaNewThpg0R .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9yuvWRaNewThpg0R .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9yuvWRaNewThpg0R .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9yuvWRaNewThpg0R .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9yuvWRaNewThpg0R .marker.cross{stroke:#333333;}#mermaid-svg-9yuvWRaNewThpg0R svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9yuvWRaNewThpg0R .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-9yuvWRaNewThpg0R .cluster-label text{fill:#333;}#mermaid-svg-9yuvWRaNewThpg0R .cluster-label span{color:#333;}#mermaid-svg-9yuvWRaNewThpg0R .label text,#mermaid-svg-9yuvWRaNewThpg0R span{fill:#333;color:#333;}#mermaid-svg-9yuvWRaNewThpg0R .node rect,#mermaid-svg-9yuvWRaNewThpg0R .node circle,#mermaid-svg-9yuvWRaNewThpg0R .node ellipse,#mermaid-svg-9yuvWRaNewThpg0R .node polygon,#mermaid-svg-9yuvWRaNewThpg0R .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9yuvWRaNewThpg0R .node .label{text-align:center;}#mermaid-svg-9yuvWRaNewThpg0R .node.clickable{cursor:pointer;}#mermaid-svg-9yuvWRaNewThpg0R .arrowheadPath{fill:#333333;}#mermaid-svg-9yuvWRaNewThpg0R .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9yuvWRaNewThpg0R .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9yuvWRaNewThpg0R .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-9yuvWRaNewThpg0R .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-9yuvWRaNewThpg0R .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9yuvWRaNewThpg0R .cluster text{fill:#333;}#mermaid-svg-9yuvWRaNewThpg0R .cluster span{color:#333;}#mermaid-svg-9yuvWRaNewThpg0R div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9yuvWRaNewThpg0R :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}*.errorsystem.*ProducerExchangeQueue1Queue2Consumer1Consumer2

  • 使用Topic类型Exchange
  • 支持通配符匹配(*匹配一个词,#匹配零或多个词)

6. RPC模式 (Remote Procedure Call)

#mermaid-svg-VxTQY4SYE0HTWmVj {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj .error-icon{fill:#552222;}#mermaid-svg-VxTQY4SYE0HTWmVj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VxTQY4SYE0HTWmVj .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-VxTQY4SYE0HTWmVj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VxTQY4SYE0HTWmVj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VxTQY4SYE0HTWmVj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VxTQY4SYE0HTWmVj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VxTQY4SYE0HTWmVj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VxTQY4SYE0HTWmVj .marker.cross{stroke:#333333;}#mermaid-svg-VxTQY4SYE0HTWmVj svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VxTQY4SYE0HTWmVj .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-VxTQY4SYE0HTWmVj text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-VxTQY4SYE0HTWmVj .actor-line{stroke:grey;}#mermaid-svg-VxTQY4SYE0HTWmVj .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj .sequenceNumber{fill:white;}#mermaid-svg-VxTQY4SYE0HTWmVj #sequencenumber{fill:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj .messageText{fill:#333;stroke:#333;}#mermaid-svg-VxTQY4SYE0HTWmVj .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-VxTQY4SYE0HTWmVj .labelText,#mermaid-svg-VxTQY4SYE0HTWmVj .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-VxTQY4SYE0HTWmVj .loopText,#mermaid-svg-VxTQY4SYE0HTWmVj .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-VxTQY4SYE0HTWmVj .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-VxTQY4SYE0HTWmVj .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-VxTQY4SYE0HTWmVj .noteText,#mermaid-svg-VxTQY4SYE0HTWmVj .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-VxTQY4SYE0HTWmVj .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-VxTQY4SYE0HTWmVj .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-VxTQY4SYE0HTWmVj .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-VxTQY4SYE0HTWmVj .actorPopupMenu{position:absolute;}#mermaid-svg-VxTQY4SYE0HTWmVj .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-VxTQY4SYE0HTWmVj .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-VxTQY4SYE0HTWmVj .actor-man circle,#mermaid-svg-VxTQY4SYE0HTWmVj line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-VxTQY4SYE0HTWmVj :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;}ClientServer请求消息(包含ReplyTo队列和CorrelationId)响应消息(相同CorrelationId)ClientServer

三、Exchange 类型详解

类型 描述 路由行为 Direct 直接交换器 精确匹配Routing Key Fanout 广播交换器 忽略Routing Key,广播到所有绑定队列 Topic 主题交换器 支持通配符匹配Routing Key Headers 头交换器 基于消息头属性匹配,忽略Routing Key

四、消息确认机制

1. 生产者确认 (Publisher Confirm)

  • 事务机制:性能较差(同步阻塞)
    channel.txSelect();channel.basicPublish(...);channel.txCommit();
  • Confirm模式:异步确认(推荐)
    channel.confirmSelect();channel.addConfirmListener((sequenceNumber, multiple) -> { // 处理确认}, (sequenceNumber, multiple) -> { // 处理NACK});

2. 消费者确认 (Consumer Ack)

  • 自动确认autoAck=true(消息发送后立即确认,可能丢失)
  • 手动确认autoAck=false(处理完成后手动确认)
    channel.basicConsume(queue, false, (consumerTag, delivery) -> { // 处理消息 channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);}, consumerTag -> {});

五、高级特性

1. 消息持久化

// 队列持久化boolean durable = true;channel.queueDeclare(\"task_queue\", durable, false, false, null);// 消息持久化channel.basicPublish(\"\", \"task_queue\", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());

2. 消息TTL (Time-To-Live)

// 队列级别TTLMap<String, Object> args = new HashMap<>();args.put(\"x-message-ttl\", 60000); // 60秒channel.queueDeclare(\"myqueue\", false, false, false, args);// 消息级别TTLAMQP.BasicProperties properties = new AMQP.BasicProperties.Builder() .expiration(\"60000\") // 60秒 .build();channel.basicPublish(\"\", \"myqueue\", properties, message.getBytes());

3. 死信队列 (DLX)

Map<String, Object> args = new HashMap<>();args.put(\"x-dead-letter-exchange\", \"dlx.exchange\");args.put(\"x-dead-letter-routing-key\", \"dlx.routingkey\");channel.queueDeclare(\"normal.queue\", true, false, false, args);

4. 延迟队列

Map<String, Object> args = new HashMap<>();args.put(\"x-dead-letter-exchange\", \"target.exchange\");args.put(\"x-dead-letter-routing-key\", \"target.routingkey\");args.put(\"x-message-ttl\", 60000); // 60秒延迟channel.queueDeclare(\"delay.queue\", true, false, false, args);

六、集群与高可用

1. 集群模式

  • 普通集群:元数据共享,队列内容不复制
  • 镜像队列:队列内容跨节点复制(高可用)

2. 镜像队列配置

# 设置镜像策略rabbitmqctl set_policy ha-all \"^ha.\" \'{\"ha-mode\":\"all\"}\'

3. 负载均衡

  • 使用HAProxy或Nginx进行负载均衡
  • 客户端连接多个节点实现故障转移

七、管理监控

1. 管理插件

rabbitmq-plugins enable rabbitmq_management

访问:http://server:15672 (默认guest/guest)

2. 关键监控指标

  • 消息速率(发布/消费)
  • 队列长度
  • 连接数/通道数
  • 内存/磁盘使用情况

3. 常用命令

# 查看队列rabbitmqctl list_queues# 查看交换机rabbitmqctl list_exchanges# 查看绑定rabbitmqctl list_bindings

八、代码示例

1. Java生产者

ConnectionFactory factory = new ConnectionFactory();factory.setHost(\"localhost\");try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.queueDeclare(\"hello\", false, false, false, null); String message = \"Hello World!\"; channel.basicPublish(\"\", \"hello\", null, message.getBytes()); System.out.println(\" [x] Sent \'\" + message + \"\'\");}

2. Java消费者

ConnectionFactory factory = new ConnectionFactory();factory.setHost(\"localhost\");Connection connection = factory.newConnection();Channel channel = connection.createChannel();channel.queueDeclare(\"hello\", false, false, false, null);System.out.println(\" [*] Waiting for messages...\");DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), \"UTF-8\"); System.out.println(\" [x] Received \'\" + message + \"\'\");};channel.basicConsume(\"hello\", true, deliverCallback, consumerTag -> { });

3. Spring AMQP配置

@Configurationpublic class RabbitConfig { @Bean public Queue helloQueue() { return new Queue(\"hello\"); } @Bean public TopicExchange exchange() { return new TopicExchange(\"spring-boot-exchange\"); } @Bean public Binding binding(Queue queue, TopicExchange exchange) { return BindingBuilder.bind(queue).to(exchange).with(\"foo.bar.#\"); }}

九、最佳实践

  1. 连接管理

    • 保持长连接,不要频繁创建/关闭
    • 使用连接池(如Spring的CachingConnectionFactory)
  2. 通道管理

    • 每个线程使用独立Channel
    • Channel不是线程安全的
  3. 错误处理

    • 实现ConnectionListener和ChannelListener
    • 处理网络中断和自动恢复
  4. 性能优化

    • 批量发布消息
    • 合理设置QoS(prefetch count)
    • 避免大消息(建议<1MB)
  5. 安全建议

    • 修改默认guest账户
    • 启用TLS加密
    • 使用Vhost隔离环境

RabbitMQ作为成熟的消息中间件,在异步处理、应用解耦、流量削峰等场景发挥着重要作用。合理使用其特性和模式,可以构建出高效可靠的分布式消息系统。