rabbitmq简介
rabbitmq是一个开源的amqp实现,服务器端用erlang语言编写,支持多种客户端,如:python、ruby、.net、java、jms、c、php、actionscript、xmpp、stomp等,支持ajax。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。
amqp,即advanced message queuing protocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件主要用于组件之间的解耦,消息的发送者无需知道消息使用者的存在,反之亦然。
amqp的主要特征是面向消息、队列、路由(包括点对点和发布/订阅)、可靠性、安全。
1
|
$ systemctl stop firewalld.service # 停止firewall |
不想关闭防火墙,就开放15672端口,设置之后可以通过网页方式管理mq
安装安装iptables防火墙
1
|
yum install iptables-services |
添加配置
保存配置
1
|
service iptables save |
重启
1
|
systemctl restart iptables.service |
设置开机自启动
1
|
systemctl enable iptables.service |
安装
安装 erlang
rabbitmq 安装需要依赖 erlang 环境
安装 rabbitmq
1
2
3
|
$ 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 |
启动服务
1
|
$ service rabbitmq-server start |
服务状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
$ 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. |
查看日志
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
62
63
64
65
66
67
68
69
|
$ 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 |
这里显示的是没有找到配置文件,我们可以自己创建这个文件
1
|
config file (s) : /etc/rabbitmq/rabbitmq .config (not found) |
创建rabbitmq.config
1
2
|
$ cd /etc/rabbitmq/ $ vim rabbitmq.config |
编辑内容如下:
1
|
[{rabbit, [{loopback_users, []}]}]. |
这里的意思是开放使用,rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加上面的配置。
保存配置后重启服务
1
|
$ service rabbitmq-server restart |
开启管理ui
1
|
$ /sbin/rabbitmq-plugins enable rabbitmq_management |
重启服务
1
|
$ service rabbitmq-server restart |
访问管理ui
通过 http://ip:15672 使用guest, guest 进行登陆了.
爱主机评测网,最优惠主机信息推荐,便宜VPS分享,香港CN2
添加用户
处于安全的考虑,guest这个默认的用户只能通过http://localhost:15672 来登录,其他的ip无法直接使用这个账号。 这对于服务器上没有安装桌面的情况是无法管理维护的,除非通过在前面添加一层代理向外提供服务,这个又有些麻烦了,这里通过配置文件来实现这个功能
命令格式
1
2
3
|
rabbitmqctl add_user <username> <newpassword> $ rabbitmqctl add_user zhdya 123456 creating user "zhdya" |
删除用户
1
2
3
|
rabbitmqctl delete_user <username> $ rabbitmqctl delete_user admin_test deleting user "admin_test" |
修改密码
1
2
3
|
rabbitmqctl change_password <username> <newpassword> $ rabbitmqctl change_password zhdya 123456 changing password for user "zhdya" |
用户授权
1
|
rabbitmqctl set_permissions [-pvhostpath] {user} {conf} {write} { read } |
该命令使用户zhdya /(可以访问虚拟主机) 中所有资源的配置、写、读权限以便管理其中的资源
1
2
|
$ rabbitmqctl set_permissions -p "/" zhdya ".*" ".*" ".*" setting permissions for user "zhdya" in vhost "/" |
查看用户授权
1
2
3
4
5
|
rabbitmqctl list_permissions [-p vhostpath] $ rabbitmqctl list_permissions -p / listing permissions in vhost "/" guest .* .* .* zhdya .* .* .* |
查看当前用户列表
可以看到添加用户成功了,但不是administrator角色
1
2
3
4
|
$ rabbitmqctl list_users listing users guest [administrator] zhdya [] |
添加角色
这里我们也将ymq用户设置为administrator角色
命令格式
1
2
3
|
rabbitmqctl set_user_tags <username> <tag> $ rabbitmqctl set_user_tags zhdya administrator setting tags for user "zhdya" to [administrator] |
再次查看权限
1
2
3
4
|
$ rabbitmqctl list_users listing users guest [administrator] zhdya [administrator] |
清除权限信息
1
2
3
|
rabbitmqctl clear_permissions [-p vhostpath] ymq rabbitmqctl clear_permissions -p / zhdya clearing permissions for user "zhdya" in vhost "/" |
官方文档
安装:
访问控制:
网络:
配置:
集群:
命令:
web 界面
添加用户
鼠标点击,划红线的角色,选择一种
rabbitmq的用户角色分类:
none、management、policymaker、monitoring、administrator
rabbitmq各类角色描述:
none
不能访问 management plugin
management
用户可以通过amqp做的任何事外加:
列出自己可以通过amqp登入的virtual hosts
查看自己的virtual hosts中的queues, exchanges 和 bindings
查看和关闭自己的channels 和 connections
查看有关自己的virtual hosts的“全局”的统计信息,包含其他用户在这些virtual hosts中的活动。
policymaker
management可以做的任何事外加:
查看、创建和删除自己的virtual hosts所属的policies和parameters
monitoring
management可以做的任何事外加:
列出所有virtual hosts,包括他们不能登录的virtual hosts
查看其他用户的connections和channels
查看节点级别的数据如clustering和memory使用情况
查看真正的关于所有virtual hosts的全局的统计信息
administrator
policymaker和monitoring可以做的任何事外加:
创建和删除virtual hosts
查看、创建和删除users
查看创建和删除permissions
关闭其他用户的connections
设置权限
该用户无权访问任何虚拟主机
点击 用户名 set permission
设置可以访问虚拟主机 中所有资源的配置、写、读权限以便管理其中的资源
至此rabbitmq单机服务已经完全搭建完毕,下面来操作单机多实例:
rabbitmq 单机多实例
其实在操作前我在网上看到了很多这种方案,多数为如下:
1
2
3
|
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。
1
2
3
4
|
[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 |
启动第一个节点
1
|
rabbitmq_node_port=5672 rabbitmq_nodename=rabbit@master01 . /rabbitmq-server |
第二个节点
1
2
|
[root@master01 sbin] # rabbitmq_node_port=5673 rabbitmq_nodename=rabbit1@master01 rabbitmq-server -detached warning: pid file not written; -detached was passed. |
你可能发现不了什么,假如你去掉-detached 你会发现,最后你得到了一个这样的错误信息:崩溃了的信息。。。
1
|
crash dump is being written to: erl_crash.dump... done |
往上翻信息或者查看日志,你会看到这样的提示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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,[]]}}} |
启动第二个节点
1
2
|
[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. |
查看端口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[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的状态:
1
2
|
cd /sbin/ . /rabbitmqctl status -n rabbit1 |
第三个节点
1
2
|
[root@master01 sbin] # rabbitmq_node_port=5674 rabbitmq_nodename=rabbit2@master01 rabbitmq-server -detached warning: pid file not written; -detached was passed. |
启动第三个节点
1
2
|
[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的状态:
1
2
|
cd /sbin/ . /rabbitmqctl status -n rabbit2 |
查看启动状态:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[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.停止第二个节点的应用程序
1
|
|
2.重新设置第二个节点的元数据和状态为清空状态。
1
|
. /rabbitmqctl -n rabbit1@master01 reset |
3.加入第一节点
1
|
. /rabbitmqctl -n rabbit1@master01 join_cluster rabbit@localhost |
4.重新启动第二节点
1
|
. /rabbitmqctl -n rabbit1@master01 start_app |
现在再次登陆web界面你可以到web端看到集群已经存在了!!!
把节点rabbit2 加入 rabbit中
同如上步骤的1234:其中注意点是,如果你需要设置第三个集群节点为内存模式,而非磁盘模式,可以参考当前版本的命令提示,在最后加上 –ram 参数
添加第三节点的完整命令
1
2
3
4
|
. /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
1
|
. /rabbitmqctl -n rabbit2@master01 join_cluster rabbit@master01 -- ram |
查看集群状态
1
2
3
4
5
6
7
8
9
10
11
12
|
. /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端看下效果吧!!!
至此,集群搭建完毕。
遇到的问题:
rabbitmq在安装后可能会出现无法启动,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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 |
解决办法
1
|
vi /etc/rabbitmq/rabbitmq-env .conf |
在文件中写入“nodename=rabbit@localhost”
,保存。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对爱主机评测网的支持。