本篇內(nèi)容主要講解“CentOs中怎么搭建RabbitMQ3.6單機(jī)多實(shí)例服務(wù)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“CentOs中怎么搭建RabbitMQ3.6單機(jī)多實(shí)例服務(wù)”吧!
我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、沈北新ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的沈北新網(wǎng)站制作公司rabbitmq簡(jiǎn)介
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$ rabbitmqctl add_user zhdya 123456 creating user "zhdya"
刪除用戶
rabbitmqctl delete_user$ rabbitmqctl delete_user admin_test deleting user "admin_test"
修改密碼
rabbitmqctl change_password$ 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$ 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 "/"
官方文檔
安裝:
訪問控制:
網(wǎng)絡(luò):
配置:
集群:
命令:
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ì)于新人來說過于簡(jiǎn)陋和不負(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)存在了?。?!
把節(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端看下效果吧!?。?/p>
至此,集群搭建完畢。
遇到的問題:
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”
,保存。
到此,相信大家對(duì)“CentOs中怎么搭建RabbitMQ3.6單機(jī)多實(shí)例服務(wù)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!