RabbitMQ 详解

宁可枝头抱香死,何曾吹落北风中。这篇文章主要讲述RabbitMQ 详解相关的知识,希望能为你提供帮助。
RabbitMQ 一、RabbitMQ 简介

  • RabbitMQ 采用Erlang语言开发,Erlang语言由Ericson设计
RabbitMQ 详解

文章图片

  • 【RabbitMQ 详解】名词解释
  • Broker
接收和分发消息的应用,RabbitMQ Server 就是Message Broker

  • Virtual host
出于多租户和安全因素设计的,把AMQP的基本组件划分到一个虚拟的分组中,类似于网络中的namespace概念。当多个不同的用户使用同一个RabbitMQ server提供的服务时,可以划分出多个vhost,每个用户在自己的vhost创建exchange/queue等。

  • Connection
publisher/consumer和broker之间的TCP连接。断开连接的操作只会在client端进行,Broker不会断开连接,除非出现网络故障或broker服务出现问题

  • Channel
如果每一次访问RabbitMQ都建立一个Connection,在消息量大的时候建立TCP Connection的开销将是巨大的,效率也较低。Channel是在connection内部建立的逻辑连接,如果应用程序支持多线程,通常每个thread创建单独的channel进行通讯,AMQP method包含了channel id帮助客户端和message broker识别channel,所以channel之间是完全隔离的。Channel作为轻量级的Connection极大减少了操作系统建立TCP connection的开销。

  • Exchange
message到达broker的第一站,根据分发规则,匹配查询表中的routing key,分发消息到queue中去。常用的类型有:direct (point-to-point), topic (publish-subscribe) and fanout (multicast)

  • Queue
消息最终被送到这里等待consumer取走。一个message可以被同时拷贝到多个queue中。

  • Binding
exchange和queue之间的虚拟连接,binding中可以包含routing key。Binding信息被保存到exchange中的查询表中,用于message的分发依据。

rabbitmq 优势:
基于erlang语言开发,具有高并发优点、支持分布式 具有消息确认机制、消息持久化机制,消息可靠性和集群可靠性高 简单易用,运行稳定,跨平台,多语言 开源

Queue 特性
消息基于先进先出的原则进行顺序消费 消息可以持久化到磁盘节点服务器 消息可以缓存到内存节点服务器提高性能

二、 RabbitMQ 生产者消费者实例
  • 原理图
RabbitMQ 详解

文章图片

生产者发送消息到broker server,在broker内部,用户创建Exchange/Queue,通过Binding规则将两者联系在一起,Exchange分发消息,根据类型/binding 的不同分发策略有区别,消息最后来到Queue中,等待消费者取走

三、RabbitMQ 部署
  • https://www.rabbitmq.com/download.html# 官网地址
  • https://github.com/rabbitmq/rabbitmq-server/releases # github 地址
3.1 RabbitMQ 单机部署
  • https://www.rabbitmq.com/install-rpm.html
3.2 Centos7 部署
3.2.1 服务器安装 RabbitMQ
  • yum源配置(centos7 自带rabbitmq ,如果配置其他版本需要配置yum源,网络不好域名可能不通)
# /etc/yum.repos.d/rabbitmq.repo 配置yum源# In /etc/yum.repos.d/rabbitmq.repo## ## Zero dependency Erlang ##[rabbitmq_erlang] name=rabbitmq_erlang baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/$basearch repo_gpgcheck=1 gpgcheck=1 enabled=1 # PackageCloud\'s repository key and RabbitMQ package signing key gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300[rabbitmq_erlang-source] name=rabbitmq_erlang-source baseurl=https://packagecloud.io/rabbitmq/erlang/el/7/SRPMS repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/rabbitmq/erlang/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300## ## RabbitMQ server ##[rabbitmq_server] name=rabbitmq_server baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/$basearch repo_gpgcheck=1 gpgcheck=1 enabled=1 # PackageCloud\'s repository key and RabbitMQ package signing key gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300[rabbitmq_server-source] name=rabbitmq_server-source baseurl=https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/SRPMS repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300

  • 更新yum源
yum update -y

  • RabbitMQ 安装
[root@localhost yum.repos.d]# yum install socat logrotate -y [root@localhost yum.repos.d]# yum install erlang rabbitmq-server -y

  • 启动RabbitMQ 服务
[root@localhost yum.repos.d]# systemctl start rabbitmq-server [root@localhost yum.repos.d]# systemctl enable rabbitmq-server Created symlink from /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service to /usr/lib/systemd/system/rabbitmq-server.service. [root@localhost yum.repos.d]# systemctl status rabbitmq-server ● rabbitmq-server.service - RabbitMQ broker Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: disabled) Active: active (running) since Thu 2021-09-09 06:39:46 CST; 22s ago Main PID: 7776 (beam.smp) CGroup: /system.slice/rabbitmq-server.service ├─7776 /usr/lib64/erlang/erts-5.10.4/bin/beam.smp -W w -K true -A30 -P 1048576 -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.3.5/sbin/../ebin -noshell -noinput -s rabb... ├─7791 /usr/lib64/erlang/erts-5.10.4/bin/epmd -daemon ├─7860 inet_gethost 4 └─7861 inet_gethost 4Sep 09 06:39:45 localhost.localdomain systemd[1]: rabbitmq-server.service: Got notification message from PID 7815, but reception only permitted for main PID 7776 Sep 09 06:39:45 localhost.localdomain systemd[1]: rabbitmq-server.service: Got notification message from PID 7817, but reception only permitted for main PID 7776 Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: RabbitMQ 3.3.5. Copyright (C) 2007-2014 GoPivotal, Inc. Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: ####Licensed under the MPL.See http://www.rabbitmq.com/ Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: #### Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: ##########Logs: /var/log/rabbitmq/rabbit@localhost.log Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: ########/var/log/rabbitmq/rabbit@localhost-sasl.log Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: ########## Sep 09 06:39:46 localhost.localdomain rabbitmq-server[7776]: Starting broker... completed with 0 plugins. Sep 09 06:39:46 localhost.localdomain systemd[1]: Started RabbitMQ broker.

3.2.2 RabbitMQ 插件管理
  • https://www.rabbitmq.com/management.html
开启 web 界面管理插件:
[root@localhost yum.repos.d]# rabbitmq-plugins enable rabbitmq_management The following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_management Plugin configuration has changed. Restart RabbitMQ for changes to take effect.5672 : 消费者访问端口 15672: web 管理端口 25672: 集群状态通信端口

3.2.3 创建账号
[root@localhost ~]# rabbitmqctl add_user admin admin #添加账号和密码 Creating user "admin" ... ...done. [root@localhost ~]# rabbitmqctl set_user_tags admin administrator #设置账号相关权限 Setting tags for user "admin" to [administrator] ... ...done. [root@localhost ~]# rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"#配置权限 Setting permissions for user "admin" in vhost "/" ... ...done. [root@localhost ~]# rabbitmqctl list_users # 查看用户列表 Listing users ... admin[administrator] guest[administrator] ...done.

3.2.4 登陆 web界面
  • RabbitMQ 高版本 开始禁用guest/guest 权限,需要重新创建账号密码进行登录
RabbitMQ 详解

文章图片

3.3 集群部署
  • rabbitmq 的集群是依赖于erlang的集群来工作的,所以必须先构建起erlang的集群环境,而erlang的集群中各个节点是通过一个magic cookie来实现的,这个cookie存放在 /var/lib/rabbitmq/.erlang.cookie 中,文件是400的权限,所以必须保证各个节点cookie保持一致否则节点之间无法通信。
3.3.1各服务器安装rabbitmq
  • 安装rabbitmq 版本需要一致
  • 配置.erlang.cookie 文件 (内容要求一致)
[root@localhost ~]# systemctl stop rabbitmq-server [root@localhost ~]# echo "TUZAORXCNIBYDKWOTJBZ" > /var/lib/rabbitmq/.erlang.cookie [root@localhost ~]# chmod 400 /var/lib/rabbitmq/.erlang.cookie [root@localhost ~]# systemctl start rabbitmq-server

3.3.2 查看集群状态
[root@rabbitmq01 ~]# rabbitmqctl cluster_status Cluster status of node rabbit@rabbitmq01 ... [{nodes,[{disc,[rabbit@rabbitmq01]}]}, {running_nodes,[rabbit@rabbitmq01]}, {cluster_name,< < "rabbit@rabbitmq01"> > }, {partitions,[]}] ...done.

3.3.3 创建RabbitMQ 集群在rabbitmq03 作为内存节点添加到rabbitmq01,并作为内存节点,在rabbitmq03执行命令:
[root@rabbitmq03 ~]# rabbitmqctl stop_app #停止app服务 [root@rabbitmq03 ~]# rabbitmqctl reset #清空元数据 [root@rabbitmq03 ~]# rabbitmqctl join_cluster rabbit@rabbitmq01 --ram #将03加入添加到集群中,--ram 为内存节点,不加默认是磁盘节点 [root@rabbitmq03 ~]# rabbitmqctl start_app #启动app服务

在rabbitmq02 节点相同操作加入到 01 节点
3.3.4 将集群设置为镜像模式
[root@rabbitmq03 ~]# rabbitmqctl set_policy all_policy_name "^" \'{"ha-mode":"all"}\' Setting policy "all_policy_name" for pattern "^" to "{\\"ha-mode\\":\\"all\\"}" with priority "0" ... ...done.

3.3.5 验证集群状态
[root@rabbitmq01 ~]# rabbitmqctl cluster_status Cluster status of node rabbit@rabbitmq01 ... [{nodes,[{disc,[rabbit@rabbitmq01]}, {ram,[rabbit@rabbitmq03,rabbit@rabbitmq02]}]}, {running_nodes,[rabbit@rabbitmq03,rabbit@rabbitmq02,rabbit@rabbitmq01]}, {cluster_name,< < "rabbit@rabbitmq01"> > }, {partitions,[]}] ...done.

3.3.6 web界面验证集群状态
  • 不启用web插件的rabbitmq服务器,会在web节点提示节点统计信息不可用(Node statistics not available )
RabbitMQ 详解

文章图片

3.3.7 各服务器启动web插件
[root@rabbitmq03 ~]#rabbitmq-plugins enable rabbitmq_management The following plugins have been enabled: mochiweb webmachine rabbitmq_web_dispatch amqp_client rabbitmq_management_agent rabbitmq_management Plugin configuration has changed. Restart RabbitMQ for changes to take effect.

3.3.8 查看web集群状态
RabbitMQ 详解

文章图片

四、RabbitMQ 常用命令
  • 创建vhost
[root@rabbitmq01 ~]# rabbitmqctl add_vhost test Creating vhost "test" ... ...done.

  • 列出所有 vhost
[root@rabbitmq01 ~]# rabbitmqctl list_vhosts Listing vhosts ... / test ...done.

  • 列出所有队列
[root@rabbitmq01 ~]# rabbitmqctl list_queues Listing queues ... ...done.

  • 删除制定vhost
[root@rabbitmq01 ~]# rabbitmqctl delete_vhost test Deleting vhost "test" ... ...done.

  • 添加账户
[root@rabbitmq01 ~]# rabbitmqctl add_user test 123456 Creating user "test" ... ...done.

  • 更改账户密码
[root@rabbitmq01 ~]# rabbitmqctl change_password test test Changing password for user "test" ... ...done.

  • 设置权限
[root@localhost ~]# rabbitmqctl set_permissions -p "/" test ".*" ".*" ".*" Setting permissions for user "admin" in vhost "/" ... ...done.

五、 RabbitMQ APIhttps://rawcdn.githack.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_9/priv/www/api/index.html

    推荐阅读