目录
第1章 RabbitMQ简介
1.1 什么是消息中间件
1.2 消息中间件的作用
1.3 RabbitMQ起源
1.4 RabbitMQ的安装及简单实用
第2章 RabbitMQ入门
2.1 概念介绍
2.2 AMQP协议介绍
第3章 客户端开发向导(php-amqplib)
3.1 连接RabbitMQ
3.2 使用交换器和队列
3.3 发送消息
3.4 消费消息
3.5 消费端的确认与拒绝
3.6 关闭连接
第4章 RabbitMQ进阶
4.1 消息何去何从
4.2 过期时间(TTL)
4.3 死信队列
4.4 延迟队列
4.5 优先级队列
4.6 RPC实现
4.7 持久化
4.8 生产者确认
4.9 消费端要点介绍
4.10 消息传输保障
第5章 RabbitMQ管理
5.1 多租户与权限
5.2 用户管理
5.3 Web端管理
5.4 应用与集群管理
5.5 服务端状态
5.6 HTTP API接口管理
第6章 RabbitMQ配置
6.1 环境变量
6.2 配置文件
6.3 参数及策略
第7章 RabbitMQ运维
7.1 集群搭建
7.1.1 多机多节点配置
7.1.2 集群节点类型
7.1.3 剔除单个节点
7.1.4 集群节点升级
7.1.5 单机多节点配置
7.2 查看服务日志
7.3 单节点故障恢复
7.4 集群迁移
7.5 集群监控
第8章 跨越集群的界限
8.1 Federation
8.2 Shovel
第9章 RabbitMQ高阶
9.1 存储机制
9.2 内存及磁盘警告
9.3 流控(Flow Control)
9.4 镜像队列
第10章 网络分区
10.1 网络分区的意义
10.2 网络分区的判定
10.3 网络分区的模拟
10.4 网络分区的影响
10.5 手动处理网络分区
10.6 自动处理网络分区
第11章 RabbitMQ扩展
11.1 消息追踪
11.2 负载均衡
yum安装:
[root@liuns ~]# yum install erlang -y
[root@liuns ~]# erl
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1>
source安装:
[root@liuns ~]# wget https://erlang.org/download/otp_src_22.1.tar.gz
[root@liuns ~]# tar zxvf otp_src_22.1.tar.gz
[root@liuns ~]# cd otp_src_22.1
[root@liuns ~]# ./configure --prefix=/opt/erlang
[root@liuns ~]# make && make install
[root@liuns ~]# ERLANG_HOME=/opt/erlang
[root@liuns ~]# export PATH=$PATH:$ERLANG_HOME/bin
[root@liuns ~]# export ERLANG_HOME
[root@liuns ~]# erl //验证
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1>
//直接下载安装包解压到对应的目录即可
[root@liuns ~]# wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-generic-unix-3.6.10.tar.xz
[root@liuns ~]# xz -d rabbitmq-server-generic-unix-3.6.10.tar.xz
[root@liuns ~]# tar xvf rabbitmq-server-generic-unix-3.6.10.tar
[root@liuns ~]# mv rabbitmq_server-3.6.10 /opt/rabbitmq
[root@liuns ~]# echo RABBITMQ_HOME=/opt/rabbitmq >> ~/.bash_profile
[root@liuns ~]# echo export PATH=$PATH:$RABBITMQ_HOME/sbin >> ~/.bash_profile
[root@liuns ~]# echo export RABBITMQ_HOME >> ~/.bash_profile
[root@liuns ~]# source ~/.bash_profile //环境变量生效
//以守护进程方式启动rabbitmq服务
[root@liuns ~]# rabbitmq-server -detached
//查看RabbitMQ运行状态
[root@liuns ~]# rabbitmqctl status
//查看RabbitMQ运行状态
[root@liuns ~]# rabbitmqctl cluster_status
//停止rabbitmq服务
[root@liuns ~]# rabbitmqctl stop
//默认访问用户名和密码都是“guest”,只能通过localhost本地网络访问。
//添加新用户,用户名root,密码123456
[root@liuns ~]# rabbitmqctl add_user root 123456
Creating user "root"
//为root用户设置所有权限
[root@liuns ~]# rabbitmqctl set_permissions -p / root ".*" ".*" ".*"
Setting permissions for user "root" in vhost "/"
//设置root为管理员角色
[root@liuns ~]# rabbitmqctl set_user_tags root administrator
Setting tags for user "root" to [administrator]
[root@liuns ~]# rabbitmqctl list_users
Listing users
guest [administrator]
root [administrator]
//设置root为管理员角色
[root@liuns ~]# rabbitmqctl [Enter]
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;
$connection = new AMQPStreamConnection('localhost', 9876, 'guest', 'guest');
$channel = $connection->channel();
$exchange = 'router';
$queue = 'msgs';
//队列声明
/*
name: $queue
passive: false
durable: true // the queue will survive server restarts
exclusive: false // the queue can be accessed in other channels
auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queue_declare($queue, false, true, false, false);
//交换器声明
/*
name: $exchange
type: direct
passive: false
durable: true // the exchange will survive server restarts
auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);
//交换器和队列绑定
$channel->queue_bind($queue, $exchange);
https://github.com/php-amqplib/php-amqplib/blob/master/demo/amqp_publisher.php
客户端支持列表 https://www.rabbitmq.com/devtools.html
客户端使用教程 https://www.rabbitmq.com/getstarted.html
//新建虚拟机vhost1
[root@liuns ~]# rabbitmqctl add_vhost vhost1
Creating vhost "vhost1"
//虚拟机列表
[root@liuns ~]# rabbitmqctl list_vhosts name tracing
Listing vhosts
vhost1 false
/ false
//删除虚拟机vhost1
[root@liuns ~]# rabbitmqctl delete_vhost vhost1
Deleting vhost "vhost1"
//授予用户root可访问虚拟主机vhost1,并在所有资源上都具备可配置、可写,可读权限
[root@liuns ~]# rabbitmqctl set_permissions -p vhost1 root ".*" ".*" ".*"
Setting permissions for user "root" in vhost "vhost1"
//列举虚拟机vhost1权限信息
[root@liuns ~]# rabbitmqctl list_permissions -p vhost1
Listing permissions in vhost "vhost1"
root .* .* .*
//列举用户root的权限信息
[root@liuns ~]# rabbitmqctl list_user_permissions root
Listing permissions for user "root"
vhost1 .* .* .*
/ .* .* .*
//清除root用户在vhost1上的权限
[root@liuns ~]# rabbitmqctl clear_permissions -p vhost1 root
Clearing permissions for user "root" in vhost "vhost1"
//创建一个新用户root,密码123456
[root@liuns ~]# rabbitmqctl add_user root 123456
Creating user “root”
//列出所有用户列表
[root@liuns ~]# rabbitmqctl list_users
Listing users
guest [administrator]
root [administrator]
//修改用户密码
[root@liuns ~]# rabbitmqctl change_password root 654321
Changing password for user "root"
//清除用户密码
[root@liuns ~]# rabbitmqctl clear_password root
Clearing password for user "root"
//验证用户名和密码
[root@liuns ~]# rabbitmqctl authenticate_user root 123456
Authenticating user "root"
Success 或 Error: failed to authenticate user "root"
//删除用户
[root@liuns ~]# rabbitmqctl delete_user root
Deleting user "root"
//设置用户一个或多个角色
[root@liuns ~]# rabbitmqctl set_user_tags root policymaker,management
Setting tags for user "root" to ['policymaker,management']
[root@liuns ~]# rabbitmqctl list_users
Listing users
guest [administrator]
root [policymaker,management]
//启用rabbitmq-management插件
[root@liuns ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
amqp_client
cowlib
cowboy
rabbitmq_web_dispatch
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@liuns... started 6 plugins.
//查看当前插件使用情况
//e*为隐士启用
//E*为显式启用
[root@liuns ~]# rabbitmq-plugins list
Configured: E = explicitly enabled; e = implicitly enabled
| Status: * = running on rabbit@liuns
|/
[e*] amqp_client 3.6.10
[e*] cowboy 1.0.4
[e*] cowlib 1.0.2
[ ] rabbitmq_amqp1_0 3.6.10
[ ] rabbitmq_auth_backend_ldap 3.6.10
[ ] rabbitmq_auth_mechanism_ssl 3.6.10
[ ] rabbitmq_consistent_hash_exchange 3.6.10
[ ] rabbitmq_event_exchange 3.6.10
[ ] rabbitmq_federation 3.6.10
[ ] rabbitmq_federation_management 3.6.10
[ ] rabbitmq_jms_topic_exchange 3.6.10
[E*] rabbitmq_management 3.6.10
[e*] rabbitmq_management_agent 3.6.10
[ ] rabbitmq_management_visualiser 3.6.10
[ ] rabbitmq_mqtt 3.6.10
[ ] rabbitmq_recent_history_exchange 3.6.10
[ ] rabbitmq_sharding 3.6.10
[ ] rabbitmq_shovel 3.6.10
[ ] rabbitmq_shovel_management 3.6.10
[ ] rabbitmq_stomp 3.6.10
[ ] rabbitmq_top 3.6.10
[ ] rabbitmq_tracing 3.6.10
[ ] rabbitmq_trust_store 3.6.10
[e*] rabbitmq_web_dispatch 3.6.10
[ ] rabbitmq_web_mqtt 3.6.10
[ ] rabbitmq_web_mqtt_examples 3.6.10
[ ] rabbitmq_web_stomp 3.6.10
[ ] rabbitmq_web_stomp_examples 3.6.10
[ ] sockjs 0.3.4
#/etc/hosts
192.168.0.2 node1
192.168.0.3 node2
192.168.0.4 node3
//node1服务器
[root@node1 ~]# cat ~/.erlang.cookie
ZKPLEQVNHWEWGBSEWUFL
//node2服务器
[root@node2 ~]# echo ZKPLEQVNHWEWGBSEWUFL > ~/.erlang.cookie
//node3服务器
[root@node3 ~]# echo ZKPLEQVNHWEWGBSEWUFL > ~/.erlang.cookie
[root@node1 ~]# rabbitmq-server -detached
[root@node2 ~]# rabbitmq-server -detached
[root@node3 ~]# rabbitmq-server -detached
//三个节点目前都是以独立的节点存在的单个集群。
//通过rabbitmqctl cluster_status查看各个节点状态。
[root@node1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node1
[{nodes,[{disc,[rabbit@node1]}]},
{running_nodes,[rabbit@node1]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node1,[]}]}]
[root@node2 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node1
[{nodes,[{disc,[rabbit@node2]}]},
{running_nodes,[rabbit@node2]},
{cluster_name,<<"rabbit@node2">>},
{partitions,[]},
{alarms,[{rabbit@node2,[]}]}]
[root@node2 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node1
[{nodes,[{disc,[rabbit@node2]}]},
{running_nodes,[rabbit@node2]},
{cluster_name,<<"rabbit@node2">>},
{partitions,[]},
{alarms,[{rabbit@node2,[]}]}]
//将node2节点加入到node1集群中(node3节点操作一样)
[root@node2 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@node2
[root@node2 ~]# rabbitmqctl reset
Resetting node rabbit@node2
[root@node2 ~]# rabbitmqctl join_cluster rabbit@node1
Clustering node rabbit@node2 with rabbit@node1
[root@node2 ~]# rabbitmqctl start_app
Starting node rabbit@node2
//任意一个节点查看集群状态,便可以看到node1、node2和node3已处于一个集群中。
[root@node1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@liuns
[{nodes,[{disc,[rabbit@node1,rabbit@node2,rabbit@node3]}]},
{running_nodes,[rabbit@node1,rabbit@node2,rabbit@node3]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node1,[]},{rabbit@node2,[]},{rabbit@node3,[]}]}]
至此集群已搭建完成。
其他注意项
如果集群中某个节点关闭了,那么关闭的节点会从集群集running_nodes选项中删除。
//关闭node2节点
[root@node2 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@node2
//查看集群状态
[root@liuns ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@liuns
[{nodes,[{disc,[rabbit@node1,rabbit@node2,rabbit@node3]}]},
{running_nodes,[rabbit@node1,rabbit@node3]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node1,[]},{rabbit@node3,[]}]}]
如果关闭了集群中的所有节点,则需要确保最先启动的是最后一个关闭的节点。如果不是,那么当前节点会等待最后一个关闭的节点启动后启动(默认等待时间是30秒/10次),如果没有等到,那么当前节点会启动失败。
Error: timeout_waiting_for_tables
如果最后一个关闭的节点最终由于异常无法启动,则可以通过rabbitmqctl forget_cluster_node命令剔除出当前集群。
[root@node1 ~]# rabbitmqctl forget_cluster_node node1
Removing node node1 from cluster
如果由于某些非正常原因关闭了所有节点,此时需要调用rabbitmqctl force_boot命令来启动一个节点,之后集群才能正常启动。
//假设最后关闭的node1节点异常无法启动,强制启动node2节点
[root@node2 ~]# rabbitmqctl force_boot
Forcing boot for Mnesia dir /..../rabbit@node2
[root@node2 ~]# rabbitmq-server -detached
[root@node1 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@liuns
[{nodes,[{disc,[rabbit@node1,rabbit@node2,rabbit@node3]}]},//其中disc标记了节点的类型
{running_nodes,[rabbit@node1,rabbit@node2,rabbit@node3]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]},
{alarms,[{rabbit@node1,[]},{rabbit@node2,[]},{rabbit@node3,[]}]}]
//--ram参数表示内存节点,不加表示磁盘节点
[root@node2 ~]# rabbitmqctl join_cluster node2 --ram
//修改集群中节点类型
[root@node2 ~]# rabbitmqctl stop_app
[root@node2 ~]# rabbitmqctl change_cluster_node_type disc
[root@node2 ~]# rabbitmqctl start_app
[root@node2 ~]# rabbitmqctl cluster_status
//假设node2节点异常终止,在node1上从集群中剔除node2
[root@node1 ~]# rabbitmqctl forget_cluster_node node2
//在node2节点服务器上,剔除node2节点
[root@node2 ~]# rabbitmqctl stop_app
[root@node2 ~]# rabbitmqctl reset //清空节点状态,并恢复到空白状态
[root@node2 ~]# rabbitmqctl start_app
//再查看集群状态时,node2节点已不再集群中
[root@node2 ~]# rabbitmqctl cluster_status
//节点1
[root@liuns rabbitmq]# RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1
RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15672}]"
rabbitmq-server -detached
//节点2
[root@liuns rabbitmq]# RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit2
RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}]"
rabbitmq-server -detached
//节点3
[root@liuns rabbitmq]# RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3
RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}]"
rabbitmq-server -detached
服务节点rabbit2、rabbit3加入到节点rabbit1的集群中。
[root@liuns rabbitmq]# rabbitmqctl -n rabbit2@liuns stop_app
[root@liuns rabbitmq]# rabbitmqctl -n rabbit2@liuns reset
[root@liuns rabbitmq]# rabbitmqctl -n rabbit2@liuns join_cluster rabbit1@liuns
[root@liuns rabbitmq]# rabbitmqctl -n rabbit2@liuns start_app
[root@liuns rabbitmq]# rabbitmqctl cluster_status
配置完成。
1
磁盘告警
//配置镜像队列命令
//rabbitmqctl set_policy [-p ] [--priority ] [--apply-to ]
definition中需要包含3部分:
1、ha-mode:指明镜像队列模式,有效值[all|exactly|nodes],默认all。
all:表示在所有节点上镜像;
exactly:表示在指定节点数上镜像,节点个数由ha-params指定;
nodes:表示在指定的节点上镜像,节点名称由ha-params指定、
2、ha-params:不同的ha-mode配置中需要用到的参数。
3、ha-sync-mode:队列消息的同步方式,有效值[automatic|manual]。
automatic时,新加入的slave会默认同步已知的镜像队列。
manual时,需要手动同步将享队列。
//举例,对队列名称以“queue_”开头的所有队列进行镜像,并在集群的2个节点上完成镜像
[root@node1 ~]#rabbitmqctl set_policy --prioriry 0 --apply-to queues mirror_queue "^queue_"
'{"ha-mode":"all","ha-params":2,"ha-sync-mode":"automatic"}'
//查看哪些slave已经完成同步命令
//rabbitmqctl list_queues {name} slave_pids synchronised_slave_pids
[root@node1 ~]#rabbitmqctl list_queues mirror_queue slave_pids synchronised_slave_pids
//手动方式同步slave命令
//rabbitmqctl sync_queue {name}
[root@node1 ~]#rabbitmqctl sync_queue mirror_queue