A. 在linux下安裝rabbitmq失敗怎麼解決
RabbitMQ 是由 LShift 提供的一個 Advanced Message Queuing Protocol (AMQP) 的開源實現,由以高性能、健壯以及可伸縮性出名的 Erlang 寫成,因此也是繼承了這些優點。
AMQP 里主要要說兩個組件:Exchange 和 Queue (在 AMQP 1.0 里還會有變動),如下圖所示,綠色的 X 就是 Exchange ,紅色的是 Queue ,這兩者都在 Server 端,又稱作 Broker ,這部分是 RabbitMQ 實現的,而藍色的則是客戶端,通常有 Procer 和 Consumer 兩種類型:
1:mq的安裝需要Erlang,所以首先下載Erlang,下載地址:http://www.erlang.org/download.html直接下載源碼,編譯安裝即可。
將下載好的tar包解壓編譯安裝,如下命令:
tar -zxvf otp_src_R16B03-1.tar.gz
cd otp_src_R16B03-1
./configure && make install
安裝過程中可能出現如下錯誤:
configure:error:
No curses library functions found
configure: error:/bin/sh'/home/niewf/software/erlang_R13B01/erts/configure'
failed for erts
解決方法:
yum list|grep ncurses
yum -y install ncurses-devel
yum install ncurses-devel
或者直接下載ncurses包編譯安裝。
下載地址:http://download.chinaunix.net/download/0008000/7242.shtml
tar zxvf ncurses.tar.gz #解壓縮並且釋放 文件包
cd ncurses #進入解壓縮的目錄(注意版本)
./configure #按照你的系統環境製作安裝配置文件
make #編譯源代碼並且編譯NCURSES庫
su root #切換到root用戶環境
make install #安裝編譯好的NCURSES庫
完成後繼續返回上一步操作。
2:安裝python,如果系統中python版本低於2.5的話需要升級python到2.6以上,具體可參考:http://gavinshaw.blog.51cto.com/385947/610585
3:安裝simplejson,直接下載simplejson源碼包編譯安裝即可,下載地址:https://pypi.python.org/pypi/simplejson/。
下載simplejson源碼包後,運行python setup.py install即可完成安裝。
4:安裝rabbit mq,下載地址:https://www.rabbitmq.com/install-generic-unix.html
下載後放入相應目錄解壓,進入%RABBITMQ_HOME%/sbin目錄下運行:./rabbitmq-server start即可啟動mq。
如果遇到如下錯誤,則參考http://leeon.me/a/rabbitmq-start-fail-note解決方案
ERROR: epmd error for host "xxx": address (cannot connect to host/port)
到此mq已經安裝完成。
在%RABBITMQ_HOME%/sbin目錄運行./rabbitmqctl status可查看當前mq狀態。
同時mq也提供了界面查看當前mq狀態,但是需要啟用該插件功能,運行如下命令:
rabbitmq-plugins enable rabbitmq_management,然後在瀏覽器中輸入:http://host-name:15672/#/即可訪問,頁面結果如下:
B. 怎麼在Ubuntu linux系統上安裝和使用RabbitMQ
到www dot rabbitmq dot com下載 /releases/rabbitmq-server/v3.6.6/rabbitmq-server_3.6.6-1_all.deb 安裝文件,然後在root許可權下運行 dpkg -i rabbitmq-server_3.6.6-1_all.deb
或者添加自定義軟體源:
echo 'deb http://www.rabbitmq.com/debian/ testing main' |
sudo tee /etc/apt/sources.list.d/rabbitmq.list
添加源的加密公鑰
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc |
sudo apt-key add -
更新源
sudo apt-get update
通過軟體庫安裝軟體
sudo apt-get install rabbitmq-server
C. linux怎麼安裝rabbitmq
RabbitMQ 安裝
# Install from EPEL
[root@vdevops~]# yum --enablerepo=epel -y install rabbitmq-server
[root@vdevops~]# systemctl start rabbitmq-server
[root@vdevops /]# journalctl -xe
Nov 12 01:30:01 vdevops.org CROND[3856]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: DIAGNOSTICS
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: ===========
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: attempted to contact: [rabbit@vdevops]
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: rabbit@vdevops:
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: * unable to connect to epmd (port 4369) on vdevops: timeout (timed out)
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: current node details:
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: - node name: rabbitmqctl3831@vdevops
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: - home dir: /var/lib/rabbitmq
Nov 12 01:30:04 vdevops.org rabbitmqctl[3831]: - cookie hash: s++dDMtIhAufFItw+ercBw==
Nov 12 01:30:04 vdevops.org polkitd[1007]: Unregistered Authentication Agent for unix-process:3798:36632 (system bus name :1.23, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected f
Nov 12 01:30:04 vdevops.org systemd[1]: rabbitmq-server.service: control process exited, code=exited status=2
Nov 12 01:30:04 vdevops.org systemd[1]: Failed to start RabbitMQ broker.
-- Subject: Unit rabbitmq-server.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit rabbitmq-server.service has failed.
--
-- The result is failed.
Nov 12 01:30:04 vdevops.org systemd[1]: Unit rabbitmq-server.service entered failed state.
Nov 12 01:30:04 vdevops.org systemd[1]: rabbitmq-server.service failed.
Nov 12 01:30:42 vdevops.org systemd[1]: Got automount request for /proc/sys/fs/binfmt_misc, triggered by 3860 (find)
Nov 12 01:30:42 vdevops.org systemd[1]: Mounting Arbitrary Executable File Formats File System...
-- Subject: Unit proc-sys-fs-binfmt_misc.mount has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit proc-sys-fs-binfmt_misc.mount has begun starting up.
Nov 12 01:30:42 vdevops.org systemd[1]: Mounted Arbitrary Executable File Formats File System.
-- Subject: Unit proc-sys-fs-binfmt_misc.mount has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit proc-sys-fs-binfmt_misc.mount has finished starting up.
--
-- The start-up result is done.
rabbitmq-server啟動報錯:Failed to start RabbitMQ broker
度娘解析:主機hostname配置錯誤,原本hostname為vdevops.org,更改為vdevops,修改hosts文件中hostname設置
[root@vdevops /]# hostnamectl set-hostname vdevops # hostname更改重啟啟動rabbitmq-server
[root@vdevops /]# systemctl status rabbitmq-server
● rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2016-11-12 01:32:41 CST; 3min 58s ago
Process: 3831 ExecStop=/usr/lib/rabbitmq/bin/rabbitmqctl stop (code=exited, status=2)
Main PID: 3891 (beam)
CGroup: /system.slice/rabbitmq-server.service
├─3891 /usr/lib64/erlang/erts-5.10.4/bin/beam -W w -K true -A30 -P 1048576 -- -root /usr/lib64/erlang -progname erl -- -home /var/lib/rabbitmq -- -pa /usr/lib/rabbitmq/lib/rabbitmq_server-3.3.5/sbin/../ebin -nos...
├─3908 /usr/lib64/erlang/erts-5.10.4/bin/epmd -daemon
├─3963 inet_gethost 4
└─3964 inet_gethost 4
Nov 12 01:32:39 vdevops systemd[1]: Starting RabbitMQ broker...
Nov 12 01:32:39 vdevops systemd[1]: rabbitmq-server.service: Got notification message from PID 3908, but reception only permitted for main PID 3891
Nov 12 01:32:40 vdevops rabbitmq-server[3891]: RabbitMQ 3.3.5. Copyright (C) 2007-2014 GoPivotal, Inc.
Nov 12 01:32:40 vdevops rabbitmq-server[3891]: ## ## Licensed under the MPL. See http://www.rabbitmq.com/
Nov 12 01:32:40 vdevops rabbitmq-server[3891]: ## ##
Nov 12 01:32:40 vdevops rabbitmq-server[3891]: ########## Logs: /var/log/rabbitmq/[email protected]
Nov 12 01:32:40 vdevops rabbitmq-server[3891]: ###### ## /var/log/rabbitmq/[email protected]
Nov 12 01:32:40 vdevops rabbitmq-server[3891]: ##########
Nov 12 01:32:41 vdevops systemd[1]: Started RabbitMQ broker.
Nov 12 01:32:41 vdevops rabbitmq-server[3891]: Starting broker... completed with 0 plugins.
[root@vdevops~]# systemctl enable rabbitmq-server
Created symlink from /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service to /usr/lib/systemd/system/rabbitmq-server.service.
防火牆設置
[root@linuxprobe~]# firewall-cmd --add-port=5672/tcp --permanent
success
[root@linuxprobe~]# firewall-cmd --reload
success
要使用RabbitMQ,請先添加用戶
[root@vdevops ~]# rabbitmqctl add_user linuxprobe password
Creating user "linuxprobe" ...
...done.
[root@vdevops ~]# rabbitmqctl list_users
Listing users ...
guest [administrator]
linuxprobe []
...done.
# granting admin role to a user is like follows
[root@vdevops ~]# rabbitmqctl change_password linuxprobe strongpassword
Changing password for user "linuxprobe" ...
...done.
# granting admin role to a user is like follows
[root@vdevops ~]# rabbitmqctl set_user_tags linuxprobe administrator
Setting tags for user "linuxprobe" to [administrator] ...
...done.
# deleting a user is like follows
[root@vdevops ~]# rabbitmqctl delete_user linuxprobe
Deleting user "linuxprobe " ...
...done.
要使用RabbitMQ,也要添加虛擬主機
[root@vdevops ~]# rabbitmqctl add_vhost /my_vhost
Creating vhost "/my_vhost" ...
...done.
[root@vdevops ~]# rabbitmqctl list_vhosts
Listing vhosts ...
/
/my_vhost
...done.
[root@vdevops ~]# rabbitmqctl delete_vhost /my_vhost
Deleting vhost "/my_vhost" ...
...done.
給虛擬主機的用戶授予許可權,請如下配置
# rabbitmqctl set_permissions [-p vhost] [user] [permission ? (modify) (write) (read)]
[root@vdevops ~]# rabbitmqctl set_permissions -p /my_vhost linuxprobe ".*" ".*" ".*"
Setting permissions for user "linuxprobe" in vhost "/my_vhost" ...
...done.
#
[root@vdevops ~]# rabbitmqctl list_user_permissions linuxprobe
Listing permissions for user "linuxprobe" ...
/my_vhost .* .* .*
...done.
# deleting permission of a specific user is like follows
[root@vdevops ~]# rabbitmqctl clear_permissions -p /my_vhost linuxprobe
Clearing permissions for user "linuxprobe" in vhost "/my_vhost" ...
...done.
RabbitMQ:使用Web UI
[root@vdevops ~]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.
[root@vdevops ~]# systemctl restart rabbitmq-server
客戶端訪問
http://vdevops:15672/
rabbitmqadmin使用
可以配置RabbitMQ使用rabbitmqadmin命
在Web UI上下載rabbitmqadmin。 登錄到Web UI並移動到「http://(主機名或IP地址):15672 /
cli」,然後顯示以下屏幕,您可以在這里下載rabbitmqadmin,點擊here右鍵保存rabbitmqadmin.txt,然後上傳到伺服器上面更改為rabbitmqadmin,並添加執行許可權
將rabbitmqadmin上傳到RabbitMQ伺服器並設置合適的許可權,此示例顯示如下設置
[root@localhost ~]# ll /usr/local/bin/rabbitmqadmin
-rwxr-xr-x. 1 root root 36112 Nov 11 17:23 /usr/local/bin/rabbitmqadmin
[root@vdevops bin]# rabbitmqadmin list users
+------------+------------------------------+---------------+
| name | password_hash | tags |
+------------+------------------------------+---------------+
| guest | k2rhpzCrZBKkY5WRhUWrGm5DCkg= | administrator |
| linuxprobe | P+YxK1QXIYrJpek5i//rPjn5SME= | administrator |
+------------+------------------------------+---------------+
[root@vdevops bin]# cd
[root@vdevops ~]# rabbitmqadmin list users
+------------+------------------------------+---------------+
| name | password_hash | tags |
+------------+------------------------------+---------------+
| guest | k2rhpzCrZBKkY5WRhUWrGm5DCkg= | administrator |
| linuxprobe | P+YxK1QXIYrJpek5i//rPjn5SME= | administrator |
+------------+------------------------------+---------------+
[root@vdevops ~]# rabbitmqadmin list vhosts
+-----------+---------+
| name | tracing |
+-----------+---------+
| / | False |
| /my_vhost | False |
+-----------+---------+
詳細可以參考http://www.linuxprobe.com/centos-deploy-rabbitmq.html
D. RabbitMQ 進階- 阿里雲伺服器部署RabbitMQ集群
如果RabbitMQ集群只有一個broker節點,那麼該節點的失效將導致整個服務臨時性的不可用,並且可能會導致message的丟失(尤其是在非持久化message存儲於非持久化queue中的時候)。可以將所有message都設置為持久化,並且使用持久化的queue,但是這樣仍然無法避免由於緩存導致的問題:因為message在發送之後和被寫入磁碟並執行fsync之間存在一個雖然短暫但是會產生問題的時間窗。通過publisher的confirm機制能夠確保客戶端知道哪些message已經存入磁碟,盡管如此,一般不希望遇到因單點故障導致服務不可用。
如果RabbitMQ集群是由多個broker節點構成的,那麼從服務的整體可用性上來講,該集群對於單點失效是有彈性的,但是同時也需要注意:盡管exchange和binding能夠在單點失效問題上倖免於難,但是queue和其上持有的message卻不行,這是因為queue及其內容僅僅存儲於單個節點之上,所以一個節點的失效表現為其對應的queue不可用。
為了提高程序的吞吐量,保持消息的可靠性,一台機器掛了後,RabbitMQ能夠正常生產,消費消息。
rabbitmq有三種模式:單機模式,普通集群模式,鏡像集群模式
Demo級別的,一般只是本機測試玩玩而已,生產環境下不會用的。
在多台機器上啟動多個rabbitmq實例,每個機器啟動一個。
但是你創建的queue,只會放在一個rabbtimq實例上,但是每個實例都同步queue的元數據(存放含queue數據的真正實例位置)。消費的時候,實際上如果連接到了另外一個實例,那麼那個實例會從queue所在實例上拉取數據過來。
示意圖
這種方式確實很麻煩,也不怎麼好,沒做到所謂的分布式,就是個普通集群。
普通集群的方式,確實達到了消息的高可用,但沒辦法保證可靠性,沒做到分布式,簡而言之,只是一個普通的集群。
這種模式,才是所謂的rabbitmq的高可用模式,跟普通集群模式不一樣的是,你創建的queue,無論元數據還是queue里的消息都會存在於多個實例上,然後每次你寫消息到queue的時候,都會自動把消息到多個實例的queue里進行消息同步。
上圖中每個節點有一個queue,生產者生產完畢數據後投遞到指定交換機的隊列,交換機的隊列進行消息同步。
每個節點queue都有一個完整的rabbitmq節點,所以這種方式叫做鏡像集群
好處: 任何一個節點宕機後,其它節點不受影響,正常使用
壞處:
確保機器中安裝了Docker,若未安裝,可看:【雲原生】Docker入門 – 阿里雲伺服器Linux環境下安裝Docker
查看拉取的鏡像
成功運行
設置節點1
瀏覽器輸入 您的ip地址:15673
再次測試即可成功~
File —> New —> Project —> Maven —> 直接Next 進入下一步創建普通的Maven工程即可
創建一個默認的Maven聚合工程,將src文件夾刪除,該工程就是一個Maven聚合工程
引入依賴如下:
在項目內,新建一個Moudle,rabbitmq-order-procer 默認Maven工程,下一步即可
在項目內,新建一個Moudle,rabbitmq-order-cousumer 默認Maven工程,下一步即可
Maven聚合工程創建完成圖
Maven依賴圖
自行手寫MainApplication即可
創建完成!
編寫完成!
啟動消費者
交換機
=
15674
15675
成功消費數據!
已成功同步消息~