为什么搭建 rabbitmq 集群?rabbitmq 集群有那些模式?如何搭建 Rabbitmq 集群?rabbitmq 镜像高可用策略有那些?
首先这款产品本身的优点众多,大家最看好的便是他的异步化提高系统抗峰值能力,然后便是系统及功能结构解耦,那么照此两点来说,他的在整个系统中的作用还是至关重要的,那么如此重要,当然要考虑他的高可用性,那么便有第一个问题的解答。
rabbitmq 有 3 种模式,但集群模式是 2 种。详细如下:
3台centos7.4操作系统,ip分别为: 192.168.31.154 192.168.31.155 192.168.31.156
vim /etc/hosts
192.168.31.154 rabbitmq1
192.168.31.155 rabbitmq2
192.168.31.156 rabbitmq3
[root@rabbitmq1 ~]# ping 192.168.31.154
PING 192.168.31.154 (192.168.31.154) 56(84) bytes of data.
64 bytes from 192.168.31.154: icmp_seq=1 ttl=64 time=0.025 ms
64 bytes from 192.168.31.154: icmp_seq=2 ttl=64 time=0.043 ms
64 bytes from 192.168.31.154: icmp_seq=3 ttl=64 time=0.042 ms
^C
--- 192.168.31.154 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.025/0.036/0.043/0.010 ms
[root@rabbitmq1 ~]# ping 192.168.31.155
PING 192.168.31.155 (192.168.31.155) 56(84) bytes of data.
64 bytes from 192.168.31.155: icmp_seq=1 ttl=64 time=0.742 ms
64 bytes from 192.168.31.155: icmp_seq=2 ttl=64 time=0.343 ms
^C
--- 192.168.31.155 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.343/0.542/0.742/0.200 ms
[root@rabbitmq1 ~]# ping 192.168.31.156
PING 192.168.31.156 (192.168.31.156) 56(84) bytes of data.
64 bytes from 192.168.31.156: icmp_seq=1 ttl=64 time=1.15 ms
64 bytes from 192.168.31.156: icmp_seq=2 ttl=64 time=0.380 ms
^C
--- 192.168.31.156 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.380/0.768/1.157/0.389 ms
[root@rabbitmq1 ~]#
好啦,环境到此就就能满足啦,接下来我们把 rabbitmq 装在 3 台机器上。
安装 rabbitmq 依赖 erlang 环境,所以我们要先安装 erlang 环境。
yum install erlang
yum install -y socat
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el6.noarch.rpm
rpm -ivh rabbitmq-server-3.6.15-1.el6.noarch.rpm
service rabbitmq-server start 启动
service rabbitmq-server stop 停止
service rabbitmq-server restart 重启
安装启动后其实还不能在其它机器访问,rabbitmq 默认的 guest 账号只能在本地机器访问, 如果想在其它机器访问必须配置其它账号
配置管理员账号:
可以创建管理员用户,负责整个MQ的运维
rabbitmqctl add_user admin adminpasspord
赋予其administrator角色
rabbitmqctl set_user_tags admin administrator
创建RabbitMQ监控用户,负责整个MQ的监控
rabbitmqctl add_user user_monitoring passwd_monitor
rabbitmqctl set_user_tags user_monitoring monitoring
创建某个项目的专用用户,只能访问项目自己的virtual hosts
rabbitmqctl set_user_tags user_monitoring management
创建和赋角色完成后查看并确认
rabbitmqctl list_users
启动 rabbitmq 内置 Web 插件, 管理 rabbitmq 账号等信息
rabbitmq-plugins enable rabbitmq_management
访问 http://你的地址:15672/#/users
页面如下:
像 MySQL 有数据库的概念并且可以指定用户对库和表等操作的权限。那 RabbitMQ 呢?RabbitMQ 也有类似的权限管理。在 RabbitMQ 中可以虚拟消息服务器 VirtualHost,每个 VirtualHost 相当月一个相对独立的 RabbitMQ 服务器,每个 VirtualHost 之间是相互隔离的。exchange、queue、message 不能互通。
在 RabbitMQ 中无法通过 AMQP 创建 VirtualHost,可以通过以下命令来创建
rabbitmqctl add_vhost [vhostname]
通常在权限管理中主要包含三步:
rabbitmqctl add_user superrd superrd
set_permissions [-p ] rabbitmqctl set_permissions -p /suanyun admin '.*' '.*' '.*'
其中, 的位置分别用正则表达式来匹配特定的资源,如:
'^(amq.gen.*|amq.default)$'
可以匹配 server 生成的和默认的 exchange,’^$’不匹配任何资源
示例:我们赋予 superrd 在“/”下面的全部资源的配置和读写权限。
rabbitmqctl set_permissions -p / superrd ".*" ".*" ".*"
注意”/”代表 virtual host 为“/”这个“/”和 Linux 里的根目录是有区别的并不是 virtual host 为“/”可以访问所以的 virtual host,把这个“/”理解成字符串就行。
需要注意的是 RabbitMQ 会缓存每个 connection 或 channel 的权限验证结果、因此权限发生变化后需要重连才能生效。
rabbitmqctl list_user_permissions admin
rabbitmqctl list_permissions -p /
rabbitmqctl set_user_tags [user] [role]
RabbitMQ 中的角色分为如下五类:none、management、policymaker、monitoring、administrator
官方解释如下:
management
User can access the management plugin
policymaker
User can access the management plugin and manage policies and parameters for the vhosts they have access to.
monitoring
User can access the management plugin and see all connections and channels as well as node-related information.
administrator
User can do everything monitoring can do, manage users, vhosts and permissions, close other user’s connections, and manage policies and parameters for all vhosts.
不能访问 management plugin
用户可以通过 AMQP 做的任何事外加:
management 可以做的任何事外加:
management 可以做的任何事外加:
policymaker 和 monitoring 可以做的任何事外加:
如下示例将 superrd 设置成 administrator 角色。
rabbitmqctl set_user_tags superrd administrator
在上述的 3 台机器上安装 rabbitmq 完成之后,你可以看到你的机器中有如下 1 个文件。路径在 $HOME 中或者在/var/lib/rabbitmq 中,文件名称为。erlang.cookie,他是一个隐藏文件。那么这文件存储的内容是什么,是做什么用的呢?
这样说吧:RabbitMQ 的集群是依赖 erlang 集群,而 erlang 集群是通过这个 cookie 进行通信认证的,因此我们做集群的第一步就是干 cookie。怎么干?
chmod 600 /var/lib/rabbitmq/.erlang.cookie
[root@rabbitmq1 rabbitmq]# rabbitmqctl status
Status of node rabbit@rabbitmq1
[{pid,17572},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.6.15"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.15"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.15"},
{rabbit,"RabbitMQ","3.6.15"},
{amqp_client,"RabbitMQ AMQP Client","3.6.15"},
{rabbit_common,
"Modules shared by rabbitmq-server and rabbitmq-erlang-client",
"3.6.15"},
{recon,"Diagnostic tools for production use","2.3.2"},
{os_mon,"CPO CXC 138 46","2.2.14"},
{cowboy,"Small, fast, modular HTTP server.","1.0.4"},
{ranch,"Socket acceptor pool for TCP protocols.","1.3.2"},
{ssl,"Erlang/OTP SSL application","5.3.3"},
{public_key,"Public key infrastructure","0.21"},
{cowlib,"Support library for manipulating Web protocols.","1.0.2"},
{crypto,"CRYPTO version 2","3.2"},
{inets,"INETS CXC 138 49","5.9.8"},
{mnesia,"MNESIA CXC 138 12","4.11"},
{compiler,"ERTS CXC 138 10","4.9.4"},
{xmerl,"XML parser","1.3.6"},
{syntax_tools,"Syntax tools","1.6.13"},
{asn1,"The Erlang ASN1 compiler version 2.0.4","2.0.4"},
{sasl,"SASL CXC 138 11","2.3.4"},
{stdlib,"ERTS CXC 138 10","1.19.4"},
{kernel,"ERTS CXC 138 10","2.16.4"}]},
{os,{unix,linux}},
{erlang_version,
"Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:2:2] [async-threads:64] [hipe] [kernel-poll:true]\n"},
{memory,
[{connection_readers,0},
{connection_writers,0},
{connection_channels,0},
{connection_other,2800},
{queue_procs,2800},
{queue_slave_procs,0},
{plugins,1471200},
{other_proc,23282232},
{metrics,142320},
{mgmt_db,526352},
{mnesia,84160},
{other_ets,2372704},
{binary,890656},
{msg_index,40536},
{code,27114499},
{atom,992409},
{other_system,22458876},
{allocated_unused,9790936},
{reserved_unallocated,2389504},
{total,91561984}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"},{http,15672,"::"}]},
{vm_memory_calculation_strategy,rss},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,1589890252},
{disk_free_limit,50000000},
{disk_free,33908043776},
{file_descriptors,
[{total_limit,204700},
{total_used,2},
{sockets_limit,184228},
{sockets_used,0}]},
{processes,[{limit,1048576},{used,330}]},
{run_queue,0},
{uptime,2934},
{kernel,{net_ticktime,60}}]
[root@rabbitmq1 rabbitmq]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbitmq1
[{nodes,[{disc,[rabbit@rabbitmq1]},{ram,[rabbit@rabbitmq3,rabbit@rabbitmq2]}]},
{running_nodes,[rabbit@rabbitmq3,rabbit@rabbitmq2,rabbit@rabbitmq1]},
{cluster_name,<<"rabbit_cluster">>},
{partitions,[]},
{alarms,[{rabbit@rabbitmq3,[]},{rabbit@rabbitmq2,[]},{rabbit@rabbitmq1,[]}]}]
[root@rabbitmq1 rabbitmq]#
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@rabbitmq1
rabbitmqctl start_app
如此便可以啦,你可以做下测试,验证下我们序言中说的普通模式的说明,那必须是杠杠对的。
在我们使用 rabbitmq 作为消息服务时,在服务负载不是很大的情况下,一般我们只需要一个 rabbitmq 节点便能为我们提供服务,可这难免会发生单点故障,要解决这个问题,我们便需要配置 rabbitmq 的集群和镜像。
首先镜像模式要依赖 policy 模块,这个模块是做什么用的呢?
policy 中文来说是政策,策略的意思,那么他就是要设置,那些 Exchanges 或者 queue 的数据需要复制,同步,如何复制同步?对就是做这些的。
这里有点内容的,我先上例子慢慢说:
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
参数意思为:
接下来就可以测试 rabbitmq 高可用性集群了。
Rabbitmq集群高可用部署详细
rabbitmq下载地址
RabbitMQ消息队列-VirtualHost与权限管理
RabbitMQ安装详解
RabbitMQ高可用性集群镜像实施方案