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):消费者,从队列中读取消息,一个队列允许同时存在多个消费者,默认将消息平均分配给所有消费者。

它们的关系可以用下图表示:
RabbitMQ核心概念

默认服务端口:5672
默认Web管理端口:15672

关于RabbitMQ的所有组件介绍详见Documentation: Table of Contents

安装部署

可以直接参考RabbitMQ Ubuntu18.0.4安装及简单使用安装更加直接快捷。
在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
初始用户名和密码:guest/guest

如果希望添加其他用户信息,可以登录之后切换到“Admin”标签页进行操作:
RabbitMQ用户管理

参考:rabbitmq用户设定

【参考】
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关系

posted @ 2022-03-11 19:38  nuccch  阅读(191)  评论(0编辑  收藏  举报