首页 » 架构配置 » 正文

CentOs7.3 搭建 RabbitMQ 3.6 单机多实例服务

RabbitMQ简介

RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。

AMQP,即Advanced message Queuing Protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。

AMQP的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全。

注意事项

centos 7.x 关闭firewall

$ systemctl stop firewalld.service # 停止firewall

不想关闭防火墙,就开放15672端口,设置之后可以通过网页方式管理MQ

安装安装iptables防火墙

yum install iptables-services

添加配置

iptables -I INPUT -p tcp --dport 5672 -j ACCEPT
iptables -I INPUT -p tcp --dport 15672 -j ACCEPT

保存配置

service iptables save

重启

systemctl restart iptables.service

设置开机自启动

systemctl enable iptables.service

安装

安装 Erlang

RabbitMQ 安装需要依赖 Erlang 环境

$ cd /usr/local/src
$ wget http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm

$ yum install erlang-19.0.4-1.el7.centos.x86_64.rpm -y
安装 RabbitMQ
$ cd /usr/local/src
$ wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.10/rabbitmq-server-3.6.10-1.el7.noarch.rpm
$ yum install rabbitmq-server-3.6.10-1.el7.noarch.rpm -y

启动服务

$ service rabbitmq-server start

服务状态

$ service rabbitmq-server status

Redirecting to /bin/systemctl status  rabbitmq-server.service
 rabbitmq-server.service - RabbitMQ broker
   Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)
   Active: active (running) since  2018-03-05 21:42:10 CST; 15s ago
 Main PID: 2493 (beam.smp)
   Status: "Initialized"
   CGroup: /system.slice/rabbitmq-server.service
           ├─2493 /usr/lib64/erlang/erts-8.0.3/bin/beam.smp -W w -A 64 -P 1048576 -t 5000000 -stbt db -zdbbl 32000 -K true -- -root /usr/lib64/erlang -progname erl -- -home /var/l...
           ├─2634 /usr/lib64/erlang/erts-8.0.3/bin/epmd -daemon
           ├─2750 erl_child_setup 1024
           ├─2760 inet_gethost 4
           └─2761 inet_gethost 4

3 05 21:42:07 master01 rabbitmq-server[2493]: RabbitMQ 3.6.10. Copyright (C) 2007-2017 Pivotal Software, Inc.
3 05 21:42:07 master01 rabbitmq-server[2493]: ##  ##      Licensed under the MPL.  See http://www.rabbitmq.com/
3 05 21:42:07 master01 rabbitmq-server[2493]: ##  ##
3 05 21:42:07 master01 rabbitmq-server[2493]: ##########  Logs: /var/log/rabbitmq/rabbit@master01.log
3 05 21:42:07 master01 rabbitmq-server[2493]: ######  ##        /var/log/rabbitmq/rabbit@master01-sasl.log
3 05 21:42:07 master01 rabbitmq-server[2493]: ##########
3 05 21:42:07 master01 rabbitmq-server[2493]: Starting broker...
3 05 21:42:10 master01 rabbitmq-server[2493]: systemd unit for activation check: "rabbitmq-server.service"
3 05 21:42:10 master01 systemd[1]: Started RabbitMQ broker.
3 05 21:42:10 master01 rabbitmq-server[2493]: completed with 0 plugins.

查看日志

$ less /var/log/rabbitmq/rabbit\@master01.log 

=INFO REPORT==== 5-Mar-2018::21:42:07 ===
Starting RabbitMQ 3.6.10 on Erlang 19.0.4
Copyright (C) 2007-2017 Pivotal Software, Inc.
Licensed under the MPL.  See http://www.rabbitmq.com/

=INFO REPORT==== 5-Mar-2018::21:42:07 ===
node           : rabbit@master01
home dir       : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.config (not found)
cookie hash    : +9loSJmR5x/9GEguoed28A==
log            : /var/log/rabbitmq/rabbit@master01.log
sasl log       : /var/log/rabbitmq/rabbit@master01-sasl.log
database dir   : /var/lib/rabbitmq/mnesia/rabbit@master01

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Memory limit set to 732MB of 1831MB total.

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Enabling free disk space monitoring

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Disk free limit set to 50MB

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Limiting to approx 924 file handles (829 sockets)

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
FHC read buffering:  OFF

=INFO REPORT==== 5-Mar-2018::21:42:07 ===
Starting RabbitMQ 3.6.10 on Erlang 19.0.4
Copyright (C) 2007-2017 Pivotal Software, Inc.
Licensed under the MPL.  See http://www.rabbitmq.com/

=INFO REPORT==== 5-Mar-2018::21:42:07 ===
node           : rabbit@master01
home dir       : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.config (not found)
cookie hash    : +9loSJmR5x/9GEguoed28A==
log            : /var/log/rabbitmq/rabbit@master01.log
sasl log       : /var/log/rabbitmq/rabbit@master01-sasl.log
database dir   : /var/lib/rabbitmq/mnesia/rabbit@master01

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Memory limit set to 732MB of 1831MB total.

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Enabling free disk space monitoring

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Disk free limit set to 50MB

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Limiting to approx 924 file handles (829 sockets)

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
FHC read buffering:  OFF
FHC write buffering: ON

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Database directory at /var/lib/rabbitmq/mnesia/rabbit@master01 is empty. Initialising from scratch...

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Waiting for Mnesia tables for 30000 ms, 9 retries left

=INFO REPORT==== 5-Mar-2018::21:42:09 ===
Waiting for Mnesia tables for 30000 ms, 9 retries left

这里显示的是没有找到配置文件,我们可以自己创建这个文件

config file(s) : /etc/rabbitmq/rabbitmq.config (not found)

创建rabbitmq.config

$ cd /etc/rabbitmq/
$ vim rabbitmq.config
编辑内容如下:
[{rabbit, [{loopback_users, []}]}].

这里的意思是开放使用,rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加上面的配置。

保存配置后重启服务
$ service rabbitmq-server restart

开启管理UI

$ /sbin/rabbitmq-plugins enable rabbitmq_management
重启服务
$ service rabbitmq-server restart

访问管理UI

通过 http://ip:15672 使用guest, guest 进行登陆了.

mark

授权操作

添加用户

处于安全的考虑,guest这个默认的用户只能通过http://localhost:15672 来登录,其他的IP无法直接使用这个账号。 这对于服务器上没有安装桌面的情况是无法管理维护的,除非通过在前面添加一层代理向外提供服务,这个又有些麻烦了,这里通过配置文件来实现这个功能

命令格式

rabbitmqctl add_user <username> <newpassword>
$ rabbitmqctl add_user zhdya 123456
Creating user "zhdya"

删除用户

rabbitmqctl delete_user <username>
$ rabbitmqctl  delete_user admin_test
Deleting user "admin_test"

修改密码

rabbitmqctl change_password <username> <newpassword>
$ rabbitmqctl  change_password  zhdya 123456
Changing password for user "zhdya"

用户授权

rabbitmqctl set_permissions [-pvhostpath] {user} {conf} {write} {read}

该命令使用户zhdya /(可以访问虚拟主机) 中所有资源的配置、写、读权限以便管理其中的资源

$ rabbitmqctl set_permissions -p "/" zhdya ".*" ".*" ".*"
Setting permissions for user "zhdya" in vhost "/"

查看用户授权

rabbitmqctl list_permissions [-p VHostPath]
$ rabbitmqctl list_permissions -p /
Listing permissions in vhost "/"
guest    .*    .*    .*
zhdya    .*    .*    .*

查看当前用户列表

可以看到添加用户成功了,但不是administrator角色

$ rabbitmqctl list_users
Listing users
guest    [administrator]
zhdya    []

添加角色

这里我们也将ymq用户设置为administrator角色

命令格式

rabbitmqctl set_user_tags <username> <tag>
$ rabbitmqctl set_user_tags zhdya administrator
Setting tags for user "zhdya" to [administrator]

再次查看权限

$ rabbitmqctl list_users
Listing users
guest    [administrator]
zhdya    [administrator]

清除权限信息

rabbitmqctl clear_permissions [-p VHostPath] ymq
rabbitmqctl  clear_permissions  -p /  zhdya
Clearing permissions for user "zhdya" in vhost "/"

官方文档

安装:https://www.rabbitmq.com/install-debian.html

访问控制:https://www.rabbitmq.com/access-control.html

网络:https://www.rabbitmq.com/networking.html

配置:https://www.rabbitmq.com/configure.html

集群:https://www.rabbitmq.com/clustering.html

命令:https://www.rabbitmq.com/man/rabbitmqctl.1.man.html#set_user_tags

web 界面

mark

添加用户

鼠标点击,划红线的角色,选择一种

mark

RabbitMQ的用户角色分类:

nonemanagementpolicymakermonitoringadministrator

RabbitMQ各类角色描述:

none

不能访问 management plugin

management

用户可以通过AMQP做的任何事外加:
列出自己可以通过AMQP登入的virtual hosts  
查看自己的virtual hosts中的queues, exchanges  bindings
查看和关闭自己的channels  connections
查看有关自己的virtual hosts的“全局”的统计信息,包含其他用户在这些virtual hosts中的活动。

policymaker

management可以做的任何事外加:
查看、创建和删除自己的virtual hosts所属的policiesparameters

monitoring

management可以做的任何事外加:
列出所有virtual hosts,包括他们不能登录的virtual hosts
查看其他用户的connectionschannels
查看节点级别的数据如clusteringmemory使用情况
查看真正的关于所有virtual hosts的全局的统计信息

administrator

policymakermonitoring可以做的任何事外加:
创建和删除virtual hosts
查看、创建和删除users
查看创建和删除permissions
关闭其他用户的connections

设置权限

该用户无权访问任何虚拟主机

mark

点击 用户名 Set permission

设置可以访问虚拟主机 中所有资源的配置、写、读权限以便管理其中的资源

mark

至此rabbitMQ单机服务已经完全搭建完毕,下面来操作单机多实例:

rabbitMQ 单机多实例

其实在操作前我在网上看到了很多这种方案,多数为如下:

RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit1 rabbitmq-server -detached
RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit2 rabbitmq-server -detached
RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit3 rabbitmq-server -detached

不过我觉得对于新人来说过于简陋和不负责任!!!

第一个节点

下面指定了特定hostname启动的,当然你也可以指定 为localhost。

[root@master01 sbin]# vim /etc/hosts

#127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 master01

启动第一个节点

RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit@master01 ./rabbitmq-server

第二个节点

[root@master01 sbin]#  RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit1@master01 rabbitmq-server -detached

Warning: PID file not written; -detached was passed.

你可能发现不了什么,假如你去掉-detached 你会发现,最后你得到了一个这样的错误信息:崩溃了的信息。。。

Crash dump is being written to: erl_crash.dump...done

往上翻信息或者查看日志,你会看到这样的提示:

BOOT FAILED
===========

Error description:
   {could_not_start,rabbitmq_mqtt,
       {{shutdown,
            {failed_to_start_child,'rabbit_mqtt_listener_sup_:::1883',
                {shutdown,
                    {failed_to_start_child,
                        {ranch_listener_sup,{acceptor,{0,0,0,0,0,0,0,0},1883}},
                        {shutdown,
                            {failed_to_start_child,ranch_acceptors_sup,
                                {listen_error,
                                    {acceptor,{0,0,0,0,0,0,0,0},1883},
                                    eaddrinuse}}}}}}},
        {rabbit_mqtt,start,[normal,[]]}}}

启动第二个节点

[root@master01 sbin]# RABBITMQ_NODE_PORT=5673 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15673}] -rabbitmq_stomp tcp_listeners [61614] -rabbitmq_mqtt  tcp_listeners [1884]" RABBITMQ_NODENAME=rabbit1 ./rabbitmq-server -detached

Warning: PID file not written; -detached was passed.

查看端口:

[root@master01 sbin]# !net
netstat -lntp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      3779/epmd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1348/sshd           
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      3598/beam.smp       
tcp        0      0 0.0.0.0:15673           0.0.0.0:*               LISTEN      5679/beam.smp       
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2065/master         
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      3598/beam.smp       
tcp        0      0 0.0.0.0:25673           0.0.0.0:*               LISTEN      5679/beam.smp       
tcp6       0      0 :::3306                 :::*                    LISTEN      1418/mysqld         
tcp6       0      0 :::4369                 :::*                    LISTEN      3779/epmd           
tcp6       0      0 :::22                   :::*                    LISTEN      1348/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2065/master         
tcp6       0      0 :::5672                 :::*                    LISTEN      3598/beam.smp       
tcp6       0      0 :::5673                 :::*                    LISTEN      5679/beam.smp 

查看rabbit1的状态:

cd /sbin/

./rabbitmqctl status -n rabbit1

第三个节点

[root@master01 sbin]# RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit2@master01 rabbitmq-server -detached

Warning: PID file not written; -detached was passed.

启动第三个节点

[root@master01 sbin]# RABBITMQ_NODE_PORT=5674 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15674}] -rabbitmq_stomp tcp_listeners [61615] -rabbitmq_mqtt tcp_listeners [1885]" RABBITMQ_NODENAME=rabbit2 ./rabbitmq-server -detached

Warning: PID file not written; -detached was passed.

查看rabbit2的状态:

cd /sbin/

./rabbitmqctl status -n rabbit2

查看启动状态:

[root@master01 sbin]# !net
netstat -lntp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:25674           0.0.0.0:*               LISTEN      6759/beam.smp       
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      3779/epmd           
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1348/sshd           
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      3598/beam.smp       
tcp        0      0 0.0.0.0:15673           0.0.0.0:*               LISTEN      5679/beam.smp       
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2065/master         
tcp        0      0 0.0.0.0:15674           0.0.0.0:*               LISTEN      6759/beam.smp       
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      3598/beam.smp       
tcp        0      0 0.0.0.0:25673           0.0.0.0:*               LISTEN      5679/beam.smp       
tcp6       0      0 :::5674                 :::*                    LISTEN      6759/beam.smp       
tcp6       0      0 :::3306                 :::*                    LISTEN      1418/mysqld         
tcp6       0      0 :::4369                 :::*                    LISTEN      3779/epmd           
tcp6       0      0 :::22                   :::*                    LISTEN      1348/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2065/master         
tcp6       0      0 :::5672                 :::*                    LISTEN      3598/beam.smp       
tcp6       0      0 :::5673                 :::*                    LISTEN      5679/beam.smp 

这样的话你可以通过:http://192.168.161.161:15672/#/,http://192.168.161.161:15673/#/http://192.168.161.161:15674/#/ 访问web查看相关节点是否真的存在。下面就是如何实现集群操作了。

集群操作

把节点rabbit1 加入 rabbit中

1.停止第二个节点的应用程序

./rabbitmqctl -n rabbit1@master01 stop_app

2.重新设置第二个节点的元数据和状态为清空状态。

./rabbitmqctl -n rabbit1@master01 reset

3.加入第一节点

./rabbitmqctl -n rabbit1@master01 join_cluster rabbit@localhost

4.重新启动第二节点

./rabbitmqctl -n rabbit1@master01 start_app

现在再次登陆web界面你可以到web端看到集群已经存在了!!!

把节点rabbit2 加入 rabbit中

同如上步骤的1234:其中注意点是,如果你需要设置第三个集群节点为内存模式,而非磁盘模式,可以参考当前版本的命令提示,在最后加上 –ram 参数

添加第三节点的完整命令

./rabbitmqctl -n rabbit2@master01 stop_app

./rabbitmqctl -n rabbit2@master01 reset

./rabbitmqctl -n rabbit2@master01 join_cluster rabbit@master01

./rabbitmqctl -n rabbit2@master01 start_app


注意:如上第三步,如果你要设置第三个集群节点为内存模式,而非磁盘模式,那就需要 --ram

./rabbitmqctl -n rabbit2@master01 join_cluster rabbit@master01 --ram

查看集群状态

./rabbitmqctl cluster_status -n rabbit@localhost

[root@master01 sbin]# ./rabbitmqctl cluster_status -n rabbit@localhost
Cluster status of node rabbit@localhost
[{nodes,[{disc,['rabbit1@master01','rabbit2@master01',
                rabbit@localhost]}]},
 {running_nodes,['rabbit2@master01','rabbit1@master01',
                 rabbit@localhost]},
 {cluster_name,<<"rabbit@master01">>},
 {partitions,[]},
 {alarms,[{'rabbit2@master01',[]},
          {'rabbit1@master01',[]},
          {rabbit@localhost,[]}]}]

到web端看下效果吧!!!

mark

至此,集群搭建完毕。


遇到的问题:

RabbitMQ在安装后可能会出现无法启动,如:

Applying plugin configuration to rabbit@192... failed.
* Could not contact node rabbit@192.
Changes will take effect at broker restart.
* Options: --online - fail if broker cannot be contacted.
--offline - do not try to contact broker.
Error: unable to connect to node rabbit@192: nodedown

DIAGNOSTICS
===========

attempted to contact: [rabbit@192]

rabbit@192:
* unable to connect to epmd (port 4369) on 192: badarg (unknown POSIX error)


current node details:
- node name: 'rabbitmq-cli-97@192'
- home dir: /var/lib/rabbitmq
- cookie hash: rb2CNGgDqm+k5+jq1wj6vg==

Error: unable to connect to node rabbit@192: nodedown
解决办法
vi /etc/rabbitmq/rabbitmq-env.conf

在文件中写入“NODENAME=rabbit@localhost”,保存。

20180515 再次增加3个实例:

---------------------------------------------------------------------------

第四个节点

RABBITMQ_NODE_PORT=5675 RABBITMQ_NODENAME=rabbit3@jike-test-mid rabbitmq-server -detached

启动第四个节点
RABBITMQ_NODE_PORT=5675 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15675}] -rabbitmq_stomp tcp_listeners [61616] -rabbitmq_mqtt tcp_listeners [1886]" RABBITMQ_NODENAME=rabbit3 ./rabbitmq-server -detached


第五个节点

RABBITMQ_NODE_PORT=5676 RABBITMQ_NODENAME=rabbit4@jike-test-mid rabbitmq-server -detached

启动第五个节点
RABBITMQ_NODE_PORT=5676 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15676}] -rabbitmq_stomp tcp_listeners [61617] -rabbitmq_mqtt tcp_listeners [1887]" RABBITMQ_NODENAME=rabbit4 ./rabbitmq-server -detached

第六个节点

RABBITMQ_NODE_PORT=5677 RABBITMQ_NODENAME=rabbit5@jike-test-mid rabbitmq-server -detached

启动第六个节点
RABBITMQ_NODE_PORT=5677 RABBITMQ_SERVER_START_ARGS="-rabbitmq_management listener [{port,15677}] -rabbitmq_stomp tcp_listeners [61618] -rabbitmq_mqtt tcp_listeners [1888]" RABBITMQ_NODENAME=rabbit5 ./rabbitmq-server -detached

--------------------------
加入集群

添加第四节点的完整命令

./rabbitmqctl -n rabbit3@jike-test-mid stop_app

./rabbitmqctl -n rabbit3@jike-test-mid reset

./rabbitmqctl -n rabbit3@jike-test-mid join_cluster rabbit@localhost

./rabbitmqctl -n rabbit3@jike-test-mid start_app


添加第五节点

./rabbitmqctl -n rabbit4@jike-test-mid stop_app

./rabbitmqctl -n rabbit4@jike-test-mid reset

./rabbitmqctl -n rabbit4@jike-test-mid join_cluster rabbit@localhost

./rabbitmqctl -n rabbit4@jike-test-mid start_app

添加第六节点

./rabbitmqctl -n rabbit5@jike-test-mid stop_app

./rabbitmqctl -n rabbit5@jike-test-mid reset

./rabbitmqctl -n rabbit5@jike-test-mid join_cluster rabbit@localhost

./rabbitmqctl -n rabbit5@jike-test-mid start_app

-----------------------------------------

结束命令如下:

rabbitmqctl -n rabbit1 stop
rabbitmqctl -n rabbit2 stop
rabbitmqctl -n rabbit3 stop
赞 (2)

发表评论