RabbitMQ入門介紹
基本概念
RabbitMQ是一個實現了AMQP協議的訊息佇列中介軟體,詳見RabbitMQ官網 。
核心概念:
1.訊息(Message):訊息由標籤(Label)和訊息體(Body)組成。
-標籤(Label):由一系列可選屬性組成,如:路由鍵(routing-key),優先順序(priority),持久化模式(delivery-mode)
-訊息體(Body):帶有業務邏輯結構的資料,如:json格式
2.生產者(Producer):建立訊息的一方,生產者將訊息交給RabbitMQ Broker,RabbitMQ Broker會根據標籤把訊息投遞給感興趣的消費者。
3.交換器(Exchange):用來接收生產者傳送的訊息,並負責將這些訊息路由給佇列,如果路由不到,則返回給生產者,或者直接丟棄(根據配置決定),有多種型別的交換器。
4.佇列(Queue):真正存放訊息的地方,消費者從這裡獲取訊息。
5.繫結(Binding):佇列與交換器的繫結關係,訊息到達交換器之後將根據這個繫結關係決定將將訊息路由到哪個佇列,交換器型別和Binding決定了訊息的路由規則。
6.消費者(Consumer):消費者,從佇列中讀取訊息,一個佇列允許同時存在多個消費者,預設將訊息平均分配給所有消費者。
它們的關係可以用下圖表示:
預設服務埠:5672
預設Web管理埠:15672
關於RabbitMQ的所有元件介紹詳見Documentation: Table of Contents 。
安裝部署
在Ubuntu 18.04上安裝RabbitMQ步驟:
第一步: 安裝Erlang/OTP
RabbitMQ是使用Erlang語言開發的,所以需要先安裝執行時環境。
獲取軟體包金鑰:
curl -fsSL https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add - sudo apt-key adv --keyserver "hkps://keys.openpgp.org" --recv-keys "0x0A9AF2115F4687BD29803A206B73A36E6026DFCA" sudo apt-get install apt-transport-https
新增映象源:
sudo vim /etc/apt/sources.list.d/bintray.erlang.list
新增如下內容:
# Use this line to install the latest Erlang 22.3.x package available deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang-22.x # Or use this line to install the latest Erlang 23.x package available # deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang # This repository provides RabbitMQ packages deb https://dl.bintray.com/rabbitmq/debian bionic main
安裝Erlang:
sudo apt-get update -y
# This is recommended. Metapackages such as erlang and erlang-nox must only be used
# with apt version pinning. They do not pin their dependency versions.
sudo apt-get install -y erlang-base \
erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
erlang-runtime-tools erlang-snmp erlang-ssl \
erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl
第二步: 安裝RabbitMQ
官網提供了2種apt倉庫:PackageCloud,Bintray,在實際使用時通過PackageCloud倉庫失敗。
使用Bintray服務安裝:
## If sudo is not available on the system,
## uncomment the line below to install it
# apt-get install -y sudo
sudo apt-get update -y
## Install prerequisites
sudo apt-get install curl gnupg -y
## Install RabbitMQ signing key
curl -fsSL https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add -
## Install apt HTTPS transport
sudo apt-get install apt-transport-https
## Add Bintray repositories that provision latest RabbitMQ and Erlang 23.x releases
sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list <<EOF
## Installs the latest Erlang 23.x release.
## Change component to "erlang-22.x" to install the latest 22.x version.
## "bionic" as distribution name should work for any later Ubuntu or Debian release.
## See the release to distribution mapping table in RabbitMQ doc guides to learn more.
deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang
## Installs latest RabbitMQ release
deb https://dl.bintray.com/rabbitmq/debian bionic main
EOF
## Update package indices
sudo apt-get update -y
## Install rabbitmq-server and its dependencies
sudo apt-get install rabbitmq-server -y --fix-missing
檢查rabbitmq-server是否安裝完畢:
service rabbitmq-server status
# 啟動/停止 RabbitMQ Server:
sudo service rabbitmq-server stop | start
# 檢查如下埠是否執行:
4369: epmd, a peer discovery service used by RabbitMQ nodes and CLI tools
5672, 5671: used by AMQP 0-9-1 and 1.0 clients without and with TLS
25672: used for inter-node and CLI tools communication (Erlang distribution server port) and is allocated from a dynamic range (limited to a single port by default, computed as AMQP port + 20000). Unless external connections on these ports are really necessary (e.g. the cluster uses federation or CLI tools are used on machines outside the subnet), these ports should not be publicly exposed. See networking guide for details.
監控管理
命令列工具
rabbitmqctl
管理RabbitMQ節點的工具,詳見:https://www.rabbitmq.com/rabbitmqctl.8.html 。
檢視佇列資訊:sudo rabbitmqctl list_queues
檢視佇列中未確認的訊息:sudo rabbitmqctl list_queues name messages_ready messages_unacknowledged
檢視exchange列表:sudo rabbitmqctl list_exchanges
檢視exchange與queue的繫結列表:sudo rabbitmqctl list_bindings
檢視虛擬主機列表:sudo rabbitmqctl list_vhosts
檢視消費者列表:sudo rabbitmqctl list_consumers
rabbitmq-diagnostics
RabbitMQ診斷,監控和健康檢查工具,詳見:https://www.rabbitmq.com/rabbitmq-diagnostics.8.html 。
檢視叢集狀態:sudo rabbitmq-diagnostics cluster_status
檢視使用者列表;sudo rabbitmq-diagnostics list_users
檢視許可權列表:sudo rabbitmq-diagnostics list_permissions
rabbitmq-plugins
外掛管理,詳見:https://www.rabbitmq.com/rabbitmq-plugins.8.html 。
檢視全部外掛列表:sudo rabbitmq-plugins list
啟用外掛:sudo rabbitmq-plugins enable 外掛名稱
,如:sudo rabbitmq-plugins enable rabbitmq_federation
禁用外掛:sudo rabbitmq-plugins disable 外掛名稱
,如:sudo rabbitmq-plugins disable rabbitmq_federation
rabbitmq-queues
佇列管理工具,詳見:https://www.rabbitmq.com/rabbitmq-queues.8.html 。
rabbitmq-upgrade
升級管理工具,詳見:https://www.rabbitmq.com/rabbitmq-upgrade.8.html 。
Web介面
RabbitMQ提供的命令列工具雖然也能完成監控,運維管理工作,但是不直觀,常規的一些操作還是通過WEB介面來完成。
RabbitMQ的Web管理功能是通過外掛方式實現的,需要啟動對應的外掛才能訪問:rabbitmq-plugins enable rabbitmq_management
。
預設的管理埠為:15672
訪問管理頁面:http://host:15672
【參考】
https://www.jianshu.com/p/377a68ce6744 RabbitMQ系列-1.初識RabbitMQ
https://www.jianshu.com/p/964218a8e37c RabbitMQ系列(一):rabbitMQ介紹
https://xie.infoq.cn/article/bb47281b6c41ac8420d50c605 RabbitMQ 實踐
https://juejin.cn/post/6844903773547003918 rabbitmq 死信佇列
https://blog.csdn.net/u014308482/article/details/53036770 rabbitmq 實現延遲佇列的兩種方式
https://www.cnblogs.com/mfrank/p/11260355.html 【RabbitMQ】一文帶你搞定RabbitMQ延遲佇列
https://blog.csdn.net/samxx8/article/details/47417133 RabbitMq的整理 exchange、route、queue關係