這篇文章主要講解了“RabbitMQ消息中間件搭建過程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“RabbitMQ消息中間件搭建過程”吧!
成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括安州網(wǎng)站建設(shè)、安州網(wǎng)站制作、安州網(wǎng)頁制作以及安州網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,安州網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到安州省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
消息中間件也可以稱消息隊(duì)列,是指用高效可靠的消息傳遞機(jī)制進(jìn)行與平臺(tái)無關(guān)的數(shù)據(jù)交流,并基于數(shù)據(jù)通信來進(jìn)行分布式系統(tǒng)的集成。通過提供消息傳遞和消息隊(duì)列模型,可以在分布式環(huán)境下擴(kuò)展進(jìn)程的通信。 RabbitMQ是使用Erlang語言開發(fā)的開源消息隊(duì)列系統(tǒng),基于AMQP協(xié)議來實(shí)現(xiàn)。AMQP的主要特征是面向消息、隊(duì)列、路由(包括點(diǎn)對(duì)點(diǎn)和發(fā)布/訂閱)、可靠性、安全。AMQP協(xié)議更多用在企業(yè)系統(tǒng)內(nèi)對(duì)數(shù)據(jù)一致性、穩(wěn)定性和可靠性要求很高的場景,對(duì)性能和吞吐量的要求還在其次。Erlang是一種通用的面向并發(fā)的編程語言 特點(diǎn)
可靠性
擴(kuò)展性
高可用性
多種協(xié)議
多語言客戶端
管理界面
插件機(jī)制
準(zhǔn)備三臺(tái)虛擬機(jī) 192.168.13.139 rabbitmq-1 192.168.13.142 rabbitmq-2 192.168.13.143 rabbitmq-3
配置hosts文件更改三臺(tái)MQ節(jié)點(diǎn)的計(jì)算機(jī)名分別為rabbitmq-1、rabbitmq-2 和rabbitmq-3,然后修改hosts配置件,并關(guān)閉防火墻和selinux
192.168.13.139 [root@rabbitmq-1 ~]# hostnamectl set-hostname rabbitmq-1 [root@rabbitmq-1 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.13.139 rabbitmq-1 192.168.13.142 rabbitmq-2 192.168.13.143 rabbitmq-3 [root@rabbitmq-1 ~]# systemctl stop firewalld && setenforce 0
192.168.13.142 [root@rabbitmq-2 ~]# hostnamectl set-hostname rabbitmq-2 [root@rabbitmq-2 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.13.139 rabbitmq-1 192.168.13.142 rabbitmq-2 192.168.13.143 rabbitmq-3 [root@rabbitmq-2 ~]# systemctl stop firewalld && setenforce 0
192.168.13.143 [root@rabbitmq-3 ~]# hostnamectl set-hostname rabbitmq-3 [root@rabbitmq-3 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.13.139 rabbitmq-1 192.168.13.142 rabbitmq-2 192.168.13.143 rabbitmq-3 [root@rabbitmq-3 ~]# systemctl stop firewalld && setenforce 0
安裝rabbitmq軟件以及配置
192.168.13.139 [root@rabbitmq-1 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel 1.yum安裝erlang [root@rabbitmq-1 ~]# wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm #Erlang環(huán)境包 [root@rabbitmq-1 ~]# yum install erlang-20.3-1.el7.centos.x86_64.rpm -y 測試; [root@rabbitmq-1 ~]# erl Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false] Eshell V9.3 (abort with ^G) 1> 2.安裝rabbitmq [root@rabbitmq-1 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm [root@rabbitmq-1 ~]# yum -y install rabbitmq-server-3.7.5-1.el7.noarch.rpm 3.啟動(dòng) [root@rabbitmq-1 ~]# systemctl daemon-reload [root@rabbitmq-1 ~]# systemctl start rabbitmq-server [root@rabbitmq-1 ~]# systemctl enable rabbitmq-server [root@rabbitmq-1 ~]# systemctl status rabbitmq-server [root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management #管理界面 #如果報(bào)錯(cuò),重啟服務(wù)就好了
192.168.13.139 4.創(chuàng)建用戶 注意:在一臺(tái)機(jī)器操作 添加用戶和密碼 [root@rabbitmq-1 ~]# rabbitmqctl add_user yjssjm yjssjm Adding user "yjssjm" ... 設(shè)置為管理員 [root@rabbitmq-1 ~]# rabbitmqctl set_user_tags yjssjm administrator Setting tags for user "yjssjm" to [administrator] ... 查看用戶 [root@rabbitmq-1 ~]# rabbitmqctl list_users Listing users ... guest [administrator] yjssjm [administrator] ...done. [root@rabbitmq-1 ~]# rabbitmqctl set_permissions -p "/" yjssjm ".*" ".*" ".*" Setting permissions for user "yjssjm" in vhost "/" ... #此處設(shè)置權(quán)限時(shí)注意'.*'之間需要有空格 三個(gè)'.*'分別代表了conf權(quán)限,read權(quán)限與write權(quán)限 例如:當(dāng)沒有給 5.開啟用戶遠(yuǎn)程登錄 [root@rabbitmq-1 ~]# cd /etc/rabbitmq/ [root@rabbitmq-1 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config [root@rabbitmq-1 rabbitmq]# ls enabled_plugins rabbitmq.config [root@rabbitmq-1 rabbitmq]# vim rabbitmq.config 修改如下: 在61行左右 {loopback_users, []} [root@rabbitmq-1 ~]# systemctl restart rabbitmq-server #重啟服務(wù)
192.168.13.142 [root@rabbitmq-2 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel 1.yum安裝erlang [root@rabbitmq-2 ~]# wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm #Erlang環(huán)境包 [root@rabbitmq-2 ~]# yum install erlang-20.3-1.el7.centos.x86_64.rpm -y [root@rabbitmq-2 ~]# erl Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false] Eshell V9.3 (abort with ^G) 1> 2.安裝rabbitmq [root@rabbitmq-2 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm [root@rabbitmq-2 ~]# yum -y install rabbitmq-server-3.7.5-1.el7.noarch.rpm 3.啟動(dòng) [root@rabbitmq-2 ~]# systemctl daemon-reload [root@rabbitmq-2 ~]# systemctl start rabbitmq-server [root@rabbitmq-2 ~]# systemctl enable rabbitmq-server [root@rabbitmq-2 ~]# systemctl status rabbitmq-server [root@rabbitmq-2 ~]# rabbitmq-plugins enable rabbitmq_management #管理界面 #如果報(bào)錯(cuò),重啟服務(wù)就好了 4.開啟用戶遠(yuǎn)程登錄 [root@rabbitmq-2 ~]# cd /etc/rabbitmq/ [root@rabbitmq-2 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config [root@rabbitmq-2 rabbitmq]# ls enabled_plugins rabbitmq.config [root@rabbitmq-2 rabbitmq]# vim rabbitmq.config 修改如下: 在61行左右 {loopback_users, []} [root@rabbitmq-2 ~]# systemctl restart rabbitmq-server
192.168.13.143 [root@rabbitmq-3 ~]# yum install -y *epel* gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel 1.yum安裝erlang [root@rabbitmq-3 ~]# wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-20.3-1.el7.centos.x86_64.rpm/download.rpm #Erlang環(huán)境包 [root@rabbitmq-3 ~]# yum install erlang-20.3-1.el7.centos.x86_64.rpm -y [root@rabbitmq-3 ~]# erl Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false] Eshell V9.3 (abort with ^G) 1> 2.安裝rabbitmq [root@rabbitmq-3 ~]# wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.5/rabbitmq-server-3.7.5-1.el7.noarch.rpm [root@rabbitmq-3 ~]# yum -y install rabbitmq-server-3.7.5-1.el7.noarch.rpm 3.啟動(dòng) [root@rabbitmq-3 ~]# systemctl daemon-reload [root@rabbitmq-3 ~]# systemctl start rabbitmq-server [root@rabbitmq-3 ~]# systemctl enable rabbitmq-server [root@rabbitmq-3 ~]# systemctl status rabbitmq-server [root@rabbitmq-3 ~]# rabbitmq-plugins enable rabbitmq_management #管理界面 #如果報(bào)錯(cuò),重啟服務(wù)就好了 4.開啟用戶遠(yuǎn)程登錄 [root@rabbitmq-3 ~]# cd /etc/rabbitmq/ [root@rabbitmq-3 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.5/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config [root@rabbitmq-3 rabbitmq]# ls enabled_plugins rabbitmq.config [root@rabbitmq-3 rabbitmq]# vim rabbitmq.config 修改如下: 在61行左右 {loopback_users, []} [root@rabbitmq-3 ~]# systemctl restart rabbitmq-server
查看端口 [root@rabbitmq-1 rabbitmq]# 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 1205/epmd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 985/sshd tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 988/beam.smp tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1285/master tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 988/beam.smp tcp6 0 0 :::4369 :::* LISTEN 1205/epmd tcp6 0 0 :::22 :::* LISTEN 985/sshd tcp6 0 0 ::1:25 :::* LISTEN 1285/master tcp6 0 0 :::5672 :::* LISTEN 988/beam.smp ========================================================================================= 4369 -- erlang端口 5672 --程序連接端口 15672 -- 管理界面ui端口 25672 -- server間內(nèi)部通信端口
訪問頁面:192.168.13.139:15672 rabbitmq默認(rèn)管理員用戶:guest 密碼:guest 新添加的用戶為:yjssjm 密碼:yjssjm
192.168.13.139 1.首先創(chuàng)建好數(shù)據(jù)存放目錄和日志存放目錄: [root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/data [root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/logs [root@rabbitmq-1 ~]# chmod 777 -R /data/rabbitmq [root@rabbitmq-1 ~]# chown rabbitmq.rabbitmq /data/ -R [root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq-env.conf #創(chuàng)建配置文件 添加下面的內(nèi)容: RABBITMQ_MNESIA_BASE=/data/rabbitmq/data RABBITMQ_LOG_BASE=/data/rabbitmq/logs [root@rabbitmq-1 ~]# systemctl restart rabbitmq-server #重啟服務(wù) 2.拷erlang.cookie Rabbitmq的集群是依附于erlang的集群來工作的,所以必須先構(gòu)建起erlang的集群。Erlang的集群中 各節(jié)點(diǎn)是經(jīng)由各個(gè)cookie來實(shí)現(xiàn)的,這個(gè)cookie存放在/var/lib/rabbitmq/.erlang.cookie中,文件是400的權(quán)限。所以必須保證各節(jié)點(diǎn)cookie一致,不然節(jié)點(diǎn)之間就無法通信. (官方在介紹集群的文檔中提到過.erlang.cookie 一般會(huì)存在這兩個(gè)地址:第一個(gè)是home/.erlang.cookie;第二個(gè)地方就是/var/lib/rabbitmq/.erlang.cookie。如果我們使用解壓縮方式安裝部署的rabbitmq,那么這個(gè)文件會(huì)在{home}目錄下,也就是$home/.erlang.cookie。如果我們使用rpm等安裝包方式進(jìn)行安裝的,那么這個(gè)文件會(huì)在/var/lib/rabbitmq目錄下。) ======================================================================================= [root@rabbitmq-1 ~]# cat /var/lib/rabbitmq/.erlang.cookie HOUCUGJDZYTFZDSWXTHJ scp的方式將rabbitmq-1節(jié)點(diǎn)的.erlang.cookie的值復(fù)制到其他兩個(gè)節(jié)點(diǎn)中。 [root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.13.142:/var/lib/rabbitmq/ [root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie root@192.168.13.143:/var/lib/rabbitmq/
192.168.13.142 1.首先創(chuàng)建好數(shù)據(jù)存放目錄和日志存放目錄: [root@rabbitmq-2 ~]# mkdir -p /data/rabbitmq/data [root@rabbitmq-2 ~]# mkdir -p /data/rabbitmq/logs [root@rabbitmq-2 ~]# chmod 777 -R /data/rabbitmq [root@rabbitmq-2 ~]# chown rabbitmq.rabbitmq /data/ -R [root@rabbitmq-2 ~]# vim /etc/rabbitmq/rabbitmq-env.conf #創(chuàng)建配置文件 添加下面的內(nèi)容: RABBITMQ_MNESIA_BASE=/data/rabbitmq/data RABBITMQ_LOG_BASE=/data/rabbitmq/logs [root@rabbitmq-2 ~]# systemctl restart rabbitmq-server #重啟服務(wù) 2.將mq-2作為內(nèi)存節(jié)點(diǎn)加到mq-1節(jié)點(diǎn)集群中 [root@rabbitmq-2 ~]# rabbitmqctl stop_app #停止節(jié)點(diǎn),切記不是停止服務(wù) [root@rabbitmq-2 ~]# rabbitmqctl reset #如果有數(shù)據(jù)需要重置,沒有則不用
如果執(zhí)行# rabbitmqctl stop_app 這條命令報(bào)錯(cuò)
注意查看回顯,如果不是以上圖片中的就是錯(cuò)誤;如果報(bào)錯(cuò),重啟rabbitmq服務(wù) [root@rabbitmq-2 ~]# systemctl restart rabbitmq-server 如果還是報(bào)錯(cuò):將傳過來的文件給與正確的權(quán)限 [root@rabbitmq-2 ~]# chmod 400 /var/lib/rabbitmq/.erlang.cookie [root@rabbitmq-2 ~]# chown rabbitmq.rabbitmq /var/lib/rabbitmq/.erlang.cookie
將節(jié)點(diǎn)2加入集群,指定角色 [root@rabbitmq-2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 #添加到內(nèi)存節(jié)點(diǎn) Clustering node 'rabbit@rabbitmq-2' with 'rabbit@rabbitmq-1' ... [root@rabbitmq-2 ~]# rabbitmqctl start_app #啟動(dòng)節(jié)點(diǎn) Starting node 'rabbit@rabbitmq-2' ... completed with 3 plugins.
192.168.13.143 1.首先創(chuàng)建好數(shù)據(jù)存放目錄和日志存放目錄: [root@rabbitmq-3 ~]# mkdir -p /data/rabbitmq/data [root@rabbitmq-3 ~]# mkdir -p /data/rabbitmq/logs [root@rabbitmq-3 ~]# chmod 777 -R /data/rabbitmq [root@rabbitmq-3 ~]# chown rabbitmq.rabbitmq /data/ -R [root@rabbitmq-3 ~]# vim /etc/rabbitmq/rabbitmq-env.conf 添加下面的內(nèi)容: RABBITMQ_MNESIA_BASE=/data/rabbitmq/data RABBITMQ_LOG_BASE=/data/rabbitmq/logs [root@rabbitmq-3 ~]# systemctl restart rabbitmq-server 2.將mq-2作為內(nèi)存節(jié)點(diǎn)加到mq-1節(jié)點(diǎn)集群中 [root@rabbitmq-3 ~]# rabbitmqctl stop_app [root@rabbitmq-3 ~]# rabbitmqctl reset [root@rabbitmq-3 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 Clustering node 'rabbit@rabbitmq-3' with 'rabbit@rabbitmq-1' ... [root@rabbitmq-3 ~]# rabbitmqctl start_app Starting node 'rabbit@rabbitmq-3' ... completed with 3 plugins.
(1)默認(rèn)rabbitmq啟動(dòng)后是磁盤節(jié)點(diǎn),在這個(gè)cluster命令下,mq-2和mq-3是內(nèi)存節(jié)點(diǎn), mq-1是磁盤節(jié)點(diǎn)。 (2)如果要使mq-2、mq-3都是磁盤節(jié)點(diǎn),去掉--ram參數(shù)即可。 (3)如果想要更改節(jié)點(diǎn)類型,可以使用命令rabbitmqctl change_cluster_node_type disc(ram),前提是必須停掉rabbitmq應(yīng)用
查看集群狀態(tài)
在 RabbitMQ 集群任意節(jié)點(diǎn)上執(zhí)行 rabbitmqctl cluster_status來查看是否集群配置成功。 在mq-1磁盤節(jié)點(diǎn)上面查看 [root@rabbitmq-1 ~]# rabbitmqctl cluster_status
每臺(tái)機(jī)器顯示出三臺(tái)節(jié)點(diǎn),表示已經(jīng)添加成功! 登錄查看 打開瀏覽器輸入http://192.168.13.139:15672
感謝各位的閱讀,以上就是“RabbitMQ消息中間件搭建過程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)RabbitMQ消息中間件搭建過程這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!