您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“CentOs 7.3中如何搭建RabbitMQ 3.6單機(jī)多實(shí)例服務(wù)”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“CentOs 7.3中如何搭建RabbitMQ 3.6單機(jī)多實(shí)例服務(wù)”這篇文章吧。
RabbitMQ簡介
RabbitMQ是一個(gè)開源的AMQP實(shí)現(xiàn),服務(wù)器端用Erlang語言編寫,支持多種客戶端,如:Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP等,支持AJAX。用于在分布式系統(tǒng)中存儲(chǔ)轉(zhuǎn)發(fā)消息,在易用性、擴(kuò)展性、高可用性等方面表現(xiàn)不俗。
AMQP,即Advanced message Queuing Protocol,高級(jí)消息隊(duì)列協(xié)議,是應(yīng)用層協(xié)議的一個(gè)開放標(biāo)準(zhǔn),為面向消息的中間件設(shè)計(jì)。消息中間件主要用于組件之間的解耦,消息的發(fā)送者無需知道消息使用者的存在,反之亦然。
AMQP的主要特征是面向消息、隊(duì)列、路由(包括點(diǎn)對(duì)點(diǎn)和發(fā)布/訂閱)、可靠性、安全。
注意事項(xiàng)
centos 7.x 關(guān)閉firewall
$ systemctl stop firewalld.service # 停止firewall
不想關(guān)閉防火墻,就開放15672端口,設(shè)置之后可以通過網(wǎng)頁方式管理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
設(shè)置開機(jī)自啟動(dòng)
systemctl enable iptables.service
安裝
安裝 Erlang
RabbitMQ 安裝需要依賴 Erlang 環(huán)境
$ 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
安裝 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
啟動(dòng)服務(wù)
$ service rabbitmq-server start
服務(wù)狀態(tài)
$ 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
這里顯示的是沒有找到配置文件,我們可以自己創(chuàng)建這個(gè)文件
config file(s) : /etc/rabbitmq/rabbitmq.config (not found)
創(chuàng)建rabbitmq.config
$ cd /etc/rabbitmq/ $ vim rabbitmq.config
編輯內(nèi)容如下:
[{rabbit, [{loopback_users, []}]}].
這里的意思是開放使用,rabbitmq默認(rèn)創(chuàng)建的用戶guest,密碼也是guest,這個(gè)用戶默認(rèn)只能是本機(jī)訪問,localhost或者127.0.0.1,從外部訪問需要添加上面的配置。
保存配置后重啟服務(wù)
$ service rabbitmq-server restart
開啟管理UI
$ /sbin/rabbitmq-plugins enable rabbitmq_management
重啟服務(wù)
$ service rabbitmq-server restart
訪問管理UI
通過 http://ip:15672 使用guest, guest 進(jìn)行登陸了.
授權(quán)操作
添加用戶
處于安全的考慮,guest這個(gè)默認(rèn)的用戶只能通過http://localhost:15672 來登錄,其他的IP無法直接使用這個(gè)賬號(hào)。 這對(duì)于服務(wù)器上沒有安裝桌面的情況是無法管理維護(hù)的,除非通過在前面添加一層代理向外提供服務(wù),這個(gè)又有些麻煩了,這里通過配置文件來實(shí)現(xiàn)這個(gè)功能
命令格式
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"
用戶授權(quán)
rabbitmqctl set_permissions [-pvhostpath] {user} {conf} {write} {read}
該命令使用戶zhdya /(可以訪問虛擬主機(jī)) 中所有資源的配置、寫、讀權(quán)限以便管理其中的資源
$ rabbitmqctl set_permissions -p "/" zhdya ".*" ".*" ".*" Setting permissions for user "zhdya" in vhost "/"
查看用戶授權(quán)
rabbitmqctl list_permissions [-p VHostPath] $ rabbitmqctl list_permissions -p / Listing permissions in vhost "/" guest .* .* .* zhdya .* .* .*
查看當(dāng)前用戶列表
可以看到添加用戶成功了,但不是administrator角色
$ rabbitmqctl list_users Listing users guest [administrator] zhdya []
添加角色
這里我們也將ymq用戶設(shè)置為administrator角色
命令格式
rabbitmqctl set_user_tags <username> <tag> $ rabbitmqctl set_user_tags zhdya administrator Setting tags for user "zhdya" to [administrator]
再次查看權(quán)限
$ rabbitmqctl list_users Listing users guest [administrator] zhdya [administrator]
清除權(quán)限信息
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
網(wǎng)絡(luò):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 界面
添加用戶
鼠標(biāo)點(diǎn)擊,劃紅線的角色,選擇一種
RabbitMQ的用戶角色分類:
none、management、policymaker、monitoring、administrator
RabbitMQ各類角色描述:
none
不能訪問 management plugin
management
用戶可以通過AMQP做的任何事外加:
列出自己可以通過AMQP登入的virtual hosts
查看自己的virtual hosts中的queues, exchanges 和 bindings
查看和關(guān)閉自己的channels 和 connections
查看有關(guān)自己的virtual hosts的“全局”的統(tǒng)計(jì)信息,包含其他用戶在這些virtual hosts中的活動(dòng)。
policymaker
management可以做的任何事外加:
查看、創(chuàng)建和刪除自己的virtual hosts所屬的policies和parameters
monitoring
management可以做的任何事外加:
列出所有virtual hosts,包括他們不能登錄的virtual hosts
查看其他用戶的connections和channels
查看節(jié)點(diǎn)級(jí)別的數(shù)據(jù)如clustering和memory使用情況
查看真正的關(guān)于所有virtual hosts的全局的統(tǒng)計(jì)信息
administrator
policymaker和monitoring可以做的任何事外加:
創(chuàng)建和刪除virtual hosts
查看、創(chuàng)建和刪除users
查看創(chuàng)建和刪除permissions
關(guān)閉其他用戶的connections
設(shè)置權(quán)限
該用戶無權(quán)訪問任何虛擬主機(jī)
點(diǎn)擊 用戶名 Set permission
設(shè)置可以訪問虛擬主機(jī) 中所有資源的配置、寫、讀權(quán)限以便管理其中的資源
至此rabbitMQ單機(jī)服務(wù)已經(jīng)完全搭建完畢,下面來操作單機(jī)多實(shí)例:
rabbitMQ 單機(jī)多實(shí)例
其實(shí)在操作前我在網(wǎng)上看到了很多這種方案,多數(shù)為如下:
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
不過我覺得對(duì)于新人來說過于簡陋和不負(fù)責(zé)任!??!
第一個(gè)節(jié)點(diǎn)
下面指定了特定hostname啟動(dòng)的,當(dāng)然你也可以指定 為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
啟動(dòng)第一個(gè)節(jié)點(diǎn)
RABBITMQ_NODE_PORT=5672 RABBITMQ_NODENAME=rabbit@master01 ./rabbitmq-server
第二個(gè)節(jié)點(diǎn)
[root@master01 sbin]# RABBITMQ_NODE_PORT=5673 RABBITMQ_NODENAME=rabbit1@master01 rabbitmq-server -detached Warning: PID file not written; -detached was passed.
你可能發(fā)現(xiàn)不了什么,假如你去掉-detached 你會(huì)發(fā)現(xiàn),最后你得到了一個(gè)這樣的錯(cuò)誤信息:崩潰了的信息。。。
Crash dump is being written to: erl_crash.dump...done
往上翻信息或者查看日志,你會(huì)看到這樣的提示:
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,[]]}}}
啟動(dòng)第二個(gè)節(jié)點(diǎn)
[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的狀態(tài):
cd /sbin/ ./rabbitmqctl status -n rabbit1
第三個(gè)節(jié)點(diǎn)
[root@master01 sbin]# RABBITMQ_NODE_PORT=5674 RABBITMQ_NODENAME=rabbit2@master01 rabbitmq-server -detached Warning: PID file not written; -detached was passed.
啟動(dòng)第三個(gè)節(jié)點(diǎn)
[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的狀態(tài):
cd /sbin/ ./rabbitmqctl status -n rabbit2
查看啟動(dòng)狀態(tài):
[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查看相關(guān)節(jié)點(diǎn)是否真的存在。下面就是如何實(shí)現(xiàn)集群操作了。
集群操作
把節(jié)點(diǎn)rabbit1 加入 rabbit中
1.停止第二個(gè)節(jié)點(diǎn)的應(yīng)用程序
./rabbitmqctl -n rabbit1@master01 stop_app
2.重新設(shè)置第二個(gè)節(jié)點(diǎn)的元數(shù)據(jù)和狀態(tài)為清空狀態(tài)。
./rabbitmqctl -n rabbit1@master01 reset
3.加入第一節(jié)點(diǎn)
./rabbitmqctl -n rabbit1@master01 join_cluster rabbit@localhost
4.重新啟動(dòng)第二節(jié)點(diǎn)
./rabbitmqctl -n rabbit1@master01 start_app
現(xiàn)在再次登陸web界面你可以到web端看到集群已經(jīng)存在了?。。?/p>
把節(jié)點(diǎn)rabbit2 加入 rabbit中
同如上步驟的1234:其中注意點(diǎn)是,如果你需要設(shè)置第三個(gè)集群節(jié)點(diǎn)為內(nèi)存模式,而非磁盤模式,可以參考當(dāng)前版本的命令提示,在最后加上 –ram 參數(shù)
添加第三節(jié)點(diǎn)的完整命令
./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
注意:如上第三步,如果你要設(shè)置第三個(gè)集群節(jié)點(diǎn)為內(nèi)存模式,而非磁盤模式,那就需要 --ram
./rabbitmqctl -n rabbit2@master01 join_cluster rabbit@master01 --ram
查看集群狀態(tài)
./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在安裝后可能會(huì)出現(xiàn)無法啟動(dòng),如:
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”
,保存。
以上是“CentOs 7.3中如何搭建RabbitMQ 3.6單機(jī)多實(shí)例服務(wù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。