RabbitMQ 集羣

rabbitmq集羣環境搭建手冊

整體要求

Rabbitmq 是用 erlang 語言寫的,所以我們需要安裝 Erlang,安裝 erlang 又需要安裝 Python 與 simplejson,如果linux預裝的python版本過低,請先升級。

本節暫用預裝python,以後需要,再行升級。

 

一、     erlang環境配置

首先進入到安裝包所在目錄

cd  /home/devapp/mq

然後依次執行如下:

1、sudo rpm -ivh tcp_wrappers-7.6-58.el6.x86_64.rpm

2、sudo rpm -ivh tcp_wrappers-libs-7.6-58.el6.x86_64.rpm

3、sudo rpm -ivh erlang-18.3-1.el6.x86_64.rpm

4、sudo rpm -ivh socat-1.7.2.1-1.el6.rf.x86_64.rpm

5、sudo rpm -ivh rabbitmq-server-3.6.3-1.noarch.rpm

 

這樣基礎環境基本安裝完成

Erlang默認的安裝目錄爲:/usr/lib64/erlang

Rabbitmq默認的安裝目錄:/usr/lib/rabbitmq/lib/rabbitmq_server-3.6.3

 

配置相關環境變量:

sudo vi/etc/profile

加入如下語句:

#set erlang environment

ERL_HOME=/usr/lib64/erlang/

PATH=$ERL_HOME/bin:$PATH

export ERL_HOME PATH

 

#set rabbitmq environment

export PATH=$PATH:/usr/lib/rabbitmq/bin

二、     Rabbitmq環境配置

A.      配置web插件管理界面

進入目錄:

cd /usr/lib/rabbitmq/bin

執行:sudo rabbitmq-plugins enable rabbitmq_management

這裏你可以隨意增加管理的插件:sudo rabbitmq-plugins enable +plugins(rabbitmq支持的)。

 

啓動rabbitmq:

sudo rabbitmq-server start &

 

到這裏,理論上你已經可以通過http://25.0.88.54:15672/這個地址訪問了,rabbitmq默認的用戶名和密碼爲:guest/guest;

 

翻看官方的release文檔後,得知由於賬號guest具有所有的操作權限,並且又是默認賬號,出於安全因素的考慮,guest用戶只能通過localhost登陸使用,並建議修改guest用戶的密碼以及新建其他賬號管理使用rabbitmq(該功能是在3.3.0版本引入的)

 

雖然可以以比較猥瑣的方式:將ebin目錄下rabbit.apploopback_users裏的<<"guest">>刪除, 並重啓rabbitmq,可通過任意IP使用guest賬號登陸管理控制檯,但始終是違背了設計者的初衷。

B.      Web插件的用戶配置管理

1.      用戶管理

 

用戶管理包括增加用戶,刪除用戶,查看用戶列表,修改用戶密碼,設置用戶角色,權限等等。

 

(1)  新增一個用

 

rabbitmqctl  add_user  Username Password

 

(2) 刪除一個用戶

rabbitmqctl  delete_user  Username


(3) 修改用戶的密碼

rabbitmqctl  change_password Username  Newpassword


(4) 查看當前用戶列表

rabbitmqctl  list_users

 

2.      用戶角色

 

按照個人理解,用戶角色可分爲五類,超級管理員, 監控者, 策略制定者, 普通管理者以及其他。


(1)
超級管理員(administrator)

可登陸管理控制檯(啓用management plugin的情況下),可查看所有的信息,並且可以對用戶,策略(policy)進行操作。

(2)
監控者(monitoring)

可登陸管理控制檯(啓用management plugin的情況下),同時可以查看rabbitmq節點的相關信息(進程數,內存使用情況,磁盤使用情況等)

(3)
策略制定者(policymaker)

可登陸管理控制檯(啓用management plugin的情況下), 同時可以對policy進行管理。但無法查看節點的相關信息(上圖紅框標識的部分)

administrator的對比,administrator能看到這些內容

(4)
普通管理者(management)

僅可登陸管理控制檯(啓用management plugin的情況下),無法看到節點信息,也無法對策略進行管理。

(5)
其他

無法登陸管理控制檯,通常就是普通的生產者和消費者。

瞭解了這些後,就可以根據需要給不同的用戶設置不同的角色,以便按需管理。

 

設置用戶角色的命令爲:

rabbitmqctl  set_user_tags  User Tag


User爲用戶名, Tag爲角色名(對應於上面的administratormonitoringpolicymakermanagement,或其他自定義名稱)

也可以給同一用戶設置多個角色,例如

rabbitmqctl  set_user_tags  hncscwc monitoring  policymaker


3.      用戶權限


用戶權限指的是用戶對exchangequeue的操作權限,包括配置權限,讀寫權限。配置權限會影響到exchangequeue的聲明和刪除。讀寫權限影響到從queue裏取消息,向exchange發送消息以及queueexchange的綁定(bind)操作。

例如:queue綁定到某exchange上,需要具有queue的可寫權限,以及exchange的可讀權限;向exchange發送消息需要具有exchange的可寫權限;從queue裏取數據需要具有queue的可讀權限。詳細請參考官方文檔中"Howpermissions work"部分。

相關命令爲:

(1)
設置用戶權限

rabbitmqctl  set_permissions  -p VHostPath  User  ConfP  WriteP  ReadP


(2) 查看(指定hostpath)所有用戶的權限信息

rabbitmqctl  list_permissions [-p  VHostPath]


(3) 查看指定用戶的權限信息

rabbitmqctl  list_user_permissions User


(4)  清除用戶的權限信息

rabbitmqctl  clear_permissions  [-pVHostPath]  User

 

4.      實際應用配置

根據以上講解,進行用戶的簡單創建:

1、 sudo rabbitmqctl  add_user  admin  admin

2、  sudo rabbitmqctl set_user_tags  admin  administrator

3、  sudo rabbitmqctl list_users

4、  這是登錄控制檯管理頁面:

C.       環境配置

一般情況下,RabbitMQ的默認配置就足夠了。如果希望特殊設置的話,有兩個途徑:

一個是環境變量的配置文件 rabbitmq-env.conf ;

一個是配置信息的配置文件 rabbitmq.config;

注意,這兩個文件默認是沒有的,如果需要必須自己創建。

rabbitmq-env.conf

這個文件的位置是確定和不能改變的,位於:/etc/rabbitmq目錄下(這個目錄需要自己創建)。

文件的內容包括了RabbitMQ的一些環境變量,常用的有:

#RABBITMQ_NODE_PORT=   //端口號

#HOSTNAME=

RABBITMQ_NODENAME=mq

RABBITMQ_CONFIG_FILE=       //配置文件的路徑

RABBITMQ_MNESIA_BASE=/rabbitmq/data       //需要使用的MNESIA數據庫的路徑

RABBITMQ_LOG_BASE=/rabbitmq/log       //log的路徑

RABBITMQ_PLUGINS_DIR=/rabbitmq/plugins   //插件的路徑

具體的列表見:http://www.rabbitmq.com/configure.html#define-environment-variables

rabbitmq.config

這是一個標準的erlang配置文件。它必須符合erlang配置文件的標準。

它既有默認的目錄,也可以在rabbitmq-env.conf文件中配置。

文件的內容詳見:http://www.rabbitmq.com/configure.html#config-items

三、     Rabbitmq集羣模式的配

A.      集羣的簡單介紹

瞭解集羣中的基本概念:

RabbitMQ的集羣節點包括內存節點、磁盤節點。顧名思義內存節點就是將所有數據放在內存,磁盤節點將數據放在磁盤。不過,如前文所述,如果在投遞消息時,打開了消息的持久化,那麼即使是內存節點,數據還是安全的放在磁盤。

一個rabbitmq集 羣中可以共享 user,vhost,queue,exchange等,所有的數據和狀態都是必須在所有節點上覆制的,一個例外是,那些當前只屬於創建它的節點的消息隊列,儘管它們可見且可被所有節點讀取。rabbitmq節點可以動態的加入到集羣中,一個節點它可以加入到集羣中,也可以從集羣環集羣會進行一個基本的負載均衡。
集羣中有兩種節點:
1 內存節點:只保存狀態到內存(一個例外的情況是:持久的queue的持久內容將被保存到disk)
2 磁盤節點:保存狀態到內存和磁盤。
內存節點雖然不寫入磁盤,但是它執行比磁盤節點要好。集羣中,只需要一個磁盤節點來保存狀態 就足夠了
如果集羣中只有內存節點,那麼不能停止它們,否則所有的狀態,消息等都會丟失。

注:這裏我們採用各個節點皆爲磁盤節點。

 

普通模式:默認的集羣模式。

對於Queue來說,消息實體只存在於其中一個節點,A、B兩個節點僅有相同的元數據,即隊列結構。

當消息進入A節點的Queue中後,consumer從B節點拉取時,RabbitMQ會臨時在A、B間進行消息傳輸,把A中的消息實體取出並經過B發送給consumer。

所以consumer應儘量連接每一個節點,從中取消息。即對於同一個邏輯隊列,要在多個節點建立物理Queue。否則無論consumer連A或B,出口總在A,會產生瓶頸。

該模式存在一個問題就是當A節點故障後,B節點無法取到A節點中還未消費的消息實體。

如果做了消息持久化,那麼得等A節點恢復,然後纔可被消費;如果沒有持久化的話,然後就沒有然後了……

鏡像模式:把需要的隊列做成鏡像隊列,存在於多個節點,屬於RabbitMQHA方案

該模式解決了上述問題,其實質和普通模式不同之處在於,消息實體會主動在鏡像節點間同步,而不是在consumer取數據時臨時拉取。

該模式帶來的副作用也很明顯,除了降低系統性能外,如果鏡像隊列數量過多,加之大量的消息進入,集羣內部的網絡帶寬將會被這種同步通訊大大消耗掉。

所以在對可靠性要求較高的場合中適用。

(目前我們搭建的環境屬於該模式,但是他依賴與普通模式集羣,所以我們一步一步來。)

B.      普通模式集羣配置

由於我們上面已經將rabbitmq-server分別搭建在三臺不同的物理機上,成爲三個獨立的應用。集羣要做的就是將這三臺獨立的應用服務,以某種方式綁定在一起,實現信息共享,相輔相成。

三臺機器分別爲:後面是主機名

25.0.88.53  NBbyk388Eij1pxwdzIJUvJ1m

25.0.88.54  6oDVSazwJVrNyMhhZaToowLu

25.0.88.55  6QBCFIURM1JyeeO1rx5WM9c9

step1:

三臺機器作爲集羣節點,我們先將三個應用服務啓動起來: # sudo rabbitmq-server  start &

step2:

在安裝好的三臺節點服務器中,分別修改 sudovi /etc/hosts文件,指定535455hosts,如:

25.0.88.53  NBbyk388Eij1pxwdzIJUvJ1m

25.0.88.54  6oDVSazwJVrNyMhhZaToowLu

25.0.88.55  6QBCFIURM1JyeeO1rx5WM9c9

 

還有hostname文件也要正確,分別是NBbyk388Eij1pxwdzIJUvJ1m6oDVSazwJVrNyMhhZaToowLu6QBCFIURM1JyeeO1rx5WM9c9,如果修改hostname建議安裝rabbitmq前修改。

請注意RabbitMQ集羣節點必須在同一個網段裏,如果是跨廣域網效果就差。

step3:設置每個節點Cookie

Rabbitmq的集羣是依賴於erlang的集羣來工作的,所以必須先構建起erlang的集羣環境。Erlang的集羣中各節點是通過一個magic cookie來實現的,這個cookie存放在 /var/lib/rabbitmq/.erlang.cookie 中,文件必須是400的權限。所以必須保證各節點cookie保持一致,否則節點之間就無法通信。

將其中一臺節點上的.erlang.cookie的文件內容複製下來保存到其他節點上。記住這裏是覆蓋。但是要注意文件的權限和屬主屬組。

爲了安全起見我們先將文件備份:#sudo cp /var/lib/rabbitmq/.erlang.cookie /var/lib/rabbitmq/erlang.cookie.back

然後,修改下文件權限,#sudo chmod777  /var/lib/rabbitmq/.erlang.cookie

然後,#sudo vi /var/lib/rabbitmq/.erlang.cookie 把內容清空,然後把複製的值粘貼到裏面,記得保存,退出。使得三臺機器的.erlang.cookie內容一致,該文件是集羣節點進行通信的驗證密鑰,這樣才能保證erlang節點進行通訊。

最後,別忘記還原.erlang.cookie的權限,否則可能會遇到錯誤# sudo chmod 400/var/lib/rabbitmq/.erlang.cookie

設置好cookie後先將三個節點的rabbitmq重啓

# sudo rabbitmqctl stop

# sudo rabbitmq-server start &

step4:

停止所有節點RabbitMq服務,然後使用detached參數獨立運行,這步很關鍵,尤其增加節點停止節點後再次啓動遇到無法啓動都可以參照這個順序

     [devapp@NBbyk388Eij1pxwdzIJUvJ1m~]#sudo  rabbitmqctl stop

    [devapp@6oDVSazwJVrNyMhhZaToowLu ~]# sudo  rabbitmqctl stop
     [devapp@6QBCFIURM1JyeeO1rx5WM9c9 ~]# sudo  rabbitmqctlstop

 

    [devapp@NBbyk388Eij1pxwdzIJUvJ1m ~]# sudo  rabbitmq-server -detached

    [devapp@6oDVSazwJVrNyMhhZaToowLu ~]# sudo rabbitmq-server -detached
     [devapp@6QBCFIURM1JyeeO1rx5WM9c9 ~]# sudo rabbitmq-server-detached

 

分別查看下每個節點

[devapp@6oDVSazwJVrNyMhhZaToowLu ~]# sudo rabbitmqctlcluster_status

 

 

Step5:

將53、55作爲集羣節點與54連接起來,在panyuntao1上,執行如下命令:

55# sudo rabbitmqctl stop_app

55# sudo rabbitmqctl join_cluster --ram rabbit@6oDVSazwJVrNyMhhZaToowLu

55# sudo rabbitmqctl start_app

53# sudo rabbitmqctl stop_app

53# sudo rabbitmqctl join_cluster --ram rabbit@6oDVSazwJVrNyMhhZaToowLu

53# sudo rabbitmqctl start_app

上述命令先停掉rabbitmq應用,然後調用cluster命令,使兩者成爲一個集羣,最後重啓rabbitmq應用。在這個cluster命令下,5355是內存節點,54是磁盤節點(RabbitMQ啓動後,默認是磁盤節點)。

54 如果要使5355在集羣裏也是磁盤節點,join_cluster 命令去掉--ram參數即可

      #rabbitmqctljoin_cluster rabbit@queue   

只要在節點列表裏包含了自己,它就成爲一個磁盤節點。在RabbitMQ集羣裏,必須至少有一個磁盤節點存在。

Step6:

      此時,在查看各個集羣節點的狀態信息:

55# sudo rabbitmqctl cluster_status

每一個節點應該都是一樣的信息,並且都是磁盤節點。

Step7:

往任意一臺集羣節點裏寫入消息隊列,會複製到另一個節點上,我們看到兩個節點的消息隊列數一致:

      這個時候我們登陸控制檯如下圖:

會出現三個節點信息。

然後,我們隨便在一臺機器上創建一個測試queue,你會發現不管queue被創建在哪臺機器上,兩臺機器監聽的queue是一致的。

[devapp@6oDVSazwJVrNyMhhZaToowLu rabbitmq]$sudo rabbitmqctl list_queues

 

 

這樣RabbitMQ集羣就正常工作了。

這種模式更適合非持久化隊列,只有該隊列是非持久的,客戶端才能重新連接到集羣裏的其他節點,並重新創建隊列。假如該隊列是持久化的,那麼唯一辦法是將故障節點恢復起來。  

爲什麼RabbitMQ不將隊列複製到集羣裏每個節點呢?這與它的集羣的設計本意相沖突,集羣的設計目的就是增加更多節點時,能線性的增加性能(CPU、內存)和容量(內存、磁盤)。理由如下:

1. storage space: If every cluster node had a full copy ofevery queue, adding nodes wouldn’t give you more storage capacity. For example,if one node could store 1GB of messages, adding two more nodes would simplygive you two more copies of the same 1GB of messages.

2. performance: Publishing messages would require replicatingthose messages to every cluster node. For durable messages that would requiretriggering disk activity on all nodes for every message. Your network and diskload would increase every time you added a node, keeping the performance of thecluster the same (or possibly worse).

當然RabbitMQ新版本集羣也支持隊列複製(有個選項可以配置)。比如在有五個節點的集羣裏,可以指定某個隊列的內容在2個節點上進行存儲,從而在性能與高可用性之間取得一個平衡。

C.       鏡像模式集羣的配置

上面配置RabbitMQ默認集羣模式,但並不保證隊列的高可用性,儘管交換機、綁定這些可以複製到集羣裏的任何一個節點,但是隊列內容不會複製,雖然該模式解決一部分節點壓力,但隊列節點宕機直接導致該隊列無法使用,只能等待重啓,所以要想在隊列節點宕機或故障也能正常使用,就要複製隊列內容到集羣裏的每個節點,需要創建鏡像隊列。

我們看看如何鏡像模式來解決複製的問題,從而提高可用性。

鏡像模式集羣配置方式有兩種:一種是通過控制檯配置,另一種是通過命令行配置。

1.      控制檯配置

·        登錄控制檯

·        Navigate to Admin > Policies > Add/ update a policy.

·        Enter "ha-nodes" next to Nameand "^nodes\." next to Pattern.

·        Enter "ha-mode" ="nodes" in the first line next to Policy, then "ha-params"in the second line, set the second line's type to "List", and thenenter "rabbit@nodeA" and "rabbit@nodeB" in the sublistwhich appears.

·        Click Add policy.

·        填寫信息如下:

 

這樣你會發現所有符合規則的queues會出現如下狀況

滿足條件的隊列會進行鏡像複製。

2.      命令行配置

使用Rabbit鏡像功能,需要基於rabbitmq策略來實現,政策是用來控制和修改羣集範圍的某個vhost隊列行爲和Exchange行爲

cluster中任意節點啓用策略,策略會自動同步到集羣節點

# sudo rabbitmqctlset_policy -p hrsystem ha-allqueue "^"'{"ha-mode":"all"}'

這行命令在vhost名稱爲hrsystem創建了一個策略,策略名稱爲ha-allqueue,策略模式爲 all 即複製到所有節點,包含新增節點,

策略正則表達式爲 “^” 表示所有匹配所有隊列名稱。

例如rabbitmqctl set_policy -p hrsystem ha-allqueue"^message" '{"ha-mode":"all"}'

注意:"^message" 這個規則要根據自己修改,這個是指同步"message"開頭的隊列名稱,我們配置時使用的應用於所有隊列,所以表達式爲"^"

官方set_policy說明參見

set_policy [-p vhost]{name} {pattern} {definition} [priority]

-p vhost :  可選參數,針對指定Vhost下的queue進行設置

Name   :  policy的名稱

Patten  :  queue的匹配模式(正則表達式)

Definition:  鏡像定義,包括三部分ha-modeha-paramsha-sync-mode

                ha-mode:指明鏡像隊列的模式,有效值all/exactly/nodes

                              all:表示在集羣所有節點上進行鏡像

         exactly:表示在指定個數的節點上進行鏡像,節點個數由ha-params決定

         nodes:表示在指定節點上進行鏡像,節點名稱通過ha-params指定

                ha-params:ha-mode模式需要用到的參數

                ha-sync-mode:鏡像隊列中消息同步方式,有效值automaticmanually

priority:可選參數,policy的優先級。

 

Demo:隊列名稱以hello開頭的所有隊列進行鏡像,並在集羣的兩個節點上完成鏡像,policy的設置命令爲:

#sudo rabbitmqctlset_policy hello-ha “^hello” ‘{“ha-mode”:”exactly”,”ha-params”:2,”ha-sync-mode”:”automatic”}’

http://www.rabbitmq.com/man/rabbitmqctl.1.man.html

ha-mode:

ha-mode

ha-params

Result

all

(absent)

Queue is mirrored across all nodes in the cluster. When a new node is added to the cluster, the queue will be mirrored to that node.

exactly

count

Queue is mirrored to count nodes in the cluster. If there are less than count nodes in the cluster, the queue is mirrored to all nodes. If there are more than count nodes in the cluster, and a node containing a mirror goes down, then a new mirror will not be created on another node. (This is to prevent queues migrating across a cluster as it is brought down.)

nodes

node names

Queue is mirrored to the nodes listed in node names. If any of those node names are not a part of the cluster, this does not constitute an error. If none of the nodes in the list are online at the time when the queue is declared then the queue will be created on the node that the declaring client is connected to.

創建隊列時需要指定ha 參數,如果不指定x-ha-prolicy 的話將無法複製

ConsumerCancellation

Clientsthat are consuming from a mirrored queue may wish to know that the queue fromwhich they have been consuming has failed over. When a mirrored queue failsover, knowledge of which messages have been sent to which consumer is lost, andtherefore all unacknowledged messages are redelivered with theredelivered flag set. Consumers may wish to know this is going to happen.

Ifso, they can consume with the argument x-cancel-on-ha-failover set to true. Their consuming will then be cancelled on failover and a consumer cancellationnotification sent. It is then theconsumer's responsibility to reissue basic.consume to start consuming again.

Forexample (in Java):

Channel channel = ...;
Consumer consumer = ...;
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-cancel-on-ha-failover", true);
channel.basicConsume("my-queue", false, args, consumer);

Thiscreates a new consumer with the argument set.

Someexamples

Policy where queues whose names begin with "ha." aremirrored to all nodes in the cluster:

rabbitmqctl

rabbitmqctl set_policy ha-all "^ha\." '{"ha-mode":"all"}'

rabbitmqctl (Windows)

rabbitmqctl set_policy ha-all "^ha\." "{""ha-mode"":""all""}"

HTTP API

PUT /api/policies/%2f/ha-all {"pattern":"^ha\.", "definition":{"ha-mode":"all"}}

Web UI

·         Navigate to Admin > Policies > Add / update a policy.

·         Enter "ha-all" next to Name, "^ha\." next to Pattern, and "ha-mode" = "all" in the first line next to Policy.

·         Click Add policy.

Policywhere queues whose names begin with "two." are mirrored to any two nodes in the cluster, with automaticsynchronisation:

rabbitmqctl

rabbitmqctl set_policy ha-two "^two\." \
   '{"ha-mode":"exactly","ha-params":2,"ha-sync-mode":"automatic"}'

rabbitmqctl (Windows)

rabbitmqctl set_policy ha-two "^two\." ^
   "{""ha-mode"":""exactly"",""ha-params"":2,"ha-sync-mode":"automatic"}"

HTTP API

PUT /api/policies/%2f/ha-two
{"pattern":"^two\.", "definition":{"ha-mode":"exactly", "ha-params":2,"ha-sync-mode":"automatic"}}

Web UI

·         Navigate to Admin > Policies > Add / update a policy.

·         Enter "ha-two" next to Name and "^two\." next to Pattern.

·         Enter "ha-mode" = "exactly" in the first line next to Policy, then "ha-params" = 2 in the second line, then "ha-sync-mode" = "automatic" in the third, and set the type on the second line to "Number".

·         Click Add policy.

Policy where queues whose names begin with "nodes." are mirrored tospecific nodes in the cluster:

rabbitmqctl

rabbitmqctl set_policy ha-nodes "^nodes\." \
   '{"ha-mode":"nodes","ha-params":["rabbit@nodeA", "rabbit@nodeB"]}'

rabbitmqctl (Windows)

rabbitmqctl set_policy ha-nodes "^nodes\." ^
   "{""ha-mode"":""nodes"",""ha-params"":[""rabbit@nodeA"", ""rabbit@nodeB""]}"

HTTP API

PUT /api/policies/%2f/ha-nodes
{"pattern":"^nodes\.", "definition":{"ha-mode":"nodes", "ha-params":["rabbit@nodeA", "rabbit@nodeB"]}

Web UI

·         Navigate to Admin > Policies > Add / update a policy.

·         Enter "ha-nodes" next to Name and "^nodes\." next to Pattern.

·         Enter "ha-mode" = "nodes" in the first line next to Policy, then "ha-params" in the second line, set the second line's type to "List", and then enter "rabbit@nodeA" and "rabbit@nodeB" in the sublist which appears.

·         Click Add policy.

PerformanceImprovements since RabbitMQ 3.6.0

WithRabbitMQ 3.6.0 we introduced a new policy that can be configured with mirroredqueues:ha-sync-batch-size. Bysynchronising messages in batches, the synchronisation process can be sped upconsiderably.

You can determine which slaves are synchronised with the followingrabbitmqctl invocation:

rabbitmqctl list_queues name slave_pidssynchronised_slave_pids

You can manually synchronise a queue with:

rabbitmqctl sync_queue name

And you can cancel synchronisation with:

rabbitmqctl cancel_sync_queue name

These features are also available through the management plugin.

注:如有任何問題,詳情參考官網:http://www.rabbitmq.com/ha.html

四、     Rabbitmq日誌追蹤設置

首先添加日誌追蹤插件

執行:sudo rabbitmq-plugins enable rabbitmq_tracing

控制檯出現如下頁面:

安裝插件後:

會發現多了兩個exchange amq.rabbitmq.traceamq.rabbitmq.log 類型均爲topic

隊列會相應增加一個queueamq.gen-字符串

日誌的默認存儲路徑爲:/var/tmp/rabbitmq-tracing/

 

注:日誌追蹤是按virtual host進行日誌傳輸,進行日誌追蹤的virtual host必須具有guest用戶權限。

五、     HAProxy集成rabbitMQ

首先,rabbitmq鏡像集羣已經安裝完成,通過HA對其進行代理,做負載均衡就很簡單了,只需要配置一些配置文件即可。

那麼我們現在最先需要做的就是安裝HAProxy代理服務器。

有兩種方式進行安裝:

A.      用yum方式安裝

首先執行:# sudo yum clean all    做一下清理工作。

然後執行:# sudo yum install gcc   如果系統已經安裝,忽略此步。

最後執行:# sudo yum install haproxy   待安裝完成後,haproxy就算安裝完成了,是不是很簡單。

這樣,可以通過執行:#haproxy –v 查看ha的版本號,

啓動# /usr/sbin/haproxy-D -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid

可以看出haproxy的配置文件默認在/etc/haproxy/下,/usr/sbin/haproxy爲可執行文件,所有ha進程執行行爲都在/var/run/haproxy.pid這個裏。

重新啓動:#sudo service haproxy restart * Restarting haproxy haproxy

 

如果爲了方便管理與查找,或者是覺得自定義安裝的版本太低,也可以自己下載haproxy源碼自己編譯安裝,我這裏用的是下面這種。

B.      下載HAProxy源碼編譯安裝

還是執行前兩步:

首先執行:# sudo yum clean all    做一下清理工作。

然後執行:# sudo yum install gcc   如果系統已經安裝,忽略此步。

1.      下載源碼haproxy安裝包

這裏我下載了:haproxy-1.6.3.tar.gz

上傳文件到根目錄/home/devapp/

然後執行:

#sudo mv /home/devapp/haproxy-1.6.3.tar.gz /data1/haproxy-1.6.3.tar.gz

將文件移動到/data1/目錄下

2.      安裝haproxy

首先執行:#sudo tar  –zxvf  haproxy-1.6.3.tar.gz    解壓文件

            #sudo mv  haproxy-1.6.3  haproxy       將文件重命名

然後執行:# sudo make TARGET=generic PREFIX=/data1/haprpxy

            #make install PREFIX=/data1/haproxy       等待安裝完成

最後執行:#sudo vi /data1/haproxy/haproxy.cfg

新建配置文件haproxy.cfg內容如下

3.      配置haproxy.cfg

###########全局配置#########

global

log /data1/dev/log    local0

log /data1/dev/log    local1 notice

# 改變當前工作目錄

chroot /var/lib/haproxy

# 創建監控所用的套接字目錄

stats socket /data1/run/haproxy/admin.sock mode660 level admin

# haproxypid存放路徑,啓動進程的用戶必須有權限訪問此文件

pidfile /var/run/haproxy.pid 

# 最大連接數,默認4000

maxconn 4000  

# 默認用戶

user  haproxy

# 默認用戶組

group haproxy 

# 創建1個進程進入deamon模式運行。此參數要求將運行模式設置爲"daemon

# Default SSL material locations

daemon       

#ca-base /etc/ssl/certs

#crt-base /etc/ssl/private

# Default ciphers to use on SSL-enabledlistening sockets.

# For more information, see ciphers(1SSL). Thislist is from:

#https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/

#ssl-default-bind-ciphersECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS

#ssl-default-bind-options no-sslv3

###########默認配置#########

defaults

log global

# 默認的模式mode {tcp|http|health }tcp4層,http7層,health

#只會返回OK

mode   http

# 採用http日誌格式

option httplog

# 啓用該項,日誌中將不會記錄空連接。所謂空連接就是在上游的負載均衡器

# 或者監控系統爲了探測該服務是否存活可用時,需要定期的連接或者獲取某

#固定的組件或頁面,或者探測掃描端口是否在監聽或開放等動作被稱爲空連接;

# 官方文檔中標註,如果該服務上游沒有其他的負載均衡器的話,建議不要使用

# 該參數,因爲互聯網上的惡意掃描或其他動作就不會被記錄下來

option dontlognull                                                                       

 

timeout connect 5000   # 連接超時時間

timeout client 50000   # 客戶端連接超時時間

timeout server 50000  # 服務器端連接超時時間

option httpclose        # 每次請求完畢後主動關閉http通道

option httplog          # 日誌類別http日誌格式

#option forwardfor     # 如果後端服務器需要獲得客戶端真實ip需要配置的參數,可以從Http Header中獲得客戶端ip 

option redispatch       # serverId對應的服務器掛掉後,強制定向到其他健康的服務器

timeout connect 10000  # default 10 second timeout if a backend isnot found

maxconn    60000     # 最大連接數

retries    3             # 3次連接失敗就認爲服務不可用,也可以通過後面設置

#errorfile 400 /etc/haproxy/errors/400.http

#errorfile 403 /etc/haproxy/errors/403.http

#errorfile 408 /etc/haproxy/errors/408.http

#errorfile 500 /etc/haproxy/errors/500.http

#errorfile 502 /etc/haproxy/errors/502.http

#errorfile 503 /etc/haproxy/errors/503.http

#errorfile 504 /etc/haproxy/errors/504.http

##################################################################

listen http_front

      bind0.0.0.0:1080            #監聽端口 

      statsrefresh 30s             #統計頁面自動刷新時間 

      statsuri /haproxy?stats      #統計頁面url 

      statsrealm Haproxy Manager #統計頁面密碼框上提示文本 

      statsauth admin:admin      #統計頁面用戶名和密碼設置 

#stats hide-version           #隱藏統計頁面上HAProxy的版本信息

#####################我把RabbitMQ的管理界面也放在HAProxy後面了###############################

listen rabbitmq_admin

    bind0.0.0.0:8004

   server node1 25.0.88.53:15672

   server node2 25.0.88.54:15672

   server node3 25.0.88.55:15672

##################################################################

listen rabbitmq_cluster

    bind0.0.0.0:5678

   option tcplog

    modetcp

   timeout client  3h

   timeout server  3h

      option  clitcpka

      #負載均衡算法(#banlance roundrobin 輪詢,balancesource

      #session值,支持static-rrleastconnfirsturi等參數)

      #balanceurl_param useri

      #balanceurl_param session_id check_post 64

   #balance hdr(User-Agent)

   #balance hdr(host)

   #balance hdr(Host) use_domain_only

   #balance rdp-cookie

   #balance leastconn

   #balance source //ip

      balanceroundrobin     

#check inter 2000 是檢測心跳頻率,rise 22次正確認爲服務器可#用,fall 33次失敗認爲服務器不可用

   server   node1 25.0.88.53:5672check inter 5s rise 2 fall 3

   server   node2 25.0.88.54:5672check inter 5s rise 2 fall 3

server   node325.0.88.55:5672 check inter 5s rise 2 fall 3

 

######至此,haproxy.cfg配置完成,重新啓動haproxy服務即可######

 

Haproxy的當前工作目錄

/var/lib/haproxy

haproxypid存放路徑,啓動進程的用戶必須有權限訪問此文件

pidfile /var/run/haproxy.pid

 

注:以上配置文件中的路徑不存在的別忘記自行創建。

 

啓動:#sudo /data1/haproxy/sbin/haproxy –f  /data1/haproxy.cfg

 

4.      Web控制檯介紹

首先可以通過控制檯查看haproxy的監控管理:

訪問地址:http://25.0.88.53:1080/haproxy?stats

通過控制檯可以看出,代理應用端口的輸入輸出量,以及應用的健康檢查,應用狀態等參數。

這裏我對rabbitmq的控制檯管理也做了代理:

通過訪問地址:http://25.0.88.53:8004/  可以隨機查看rabbitmq的控制檯管理,而不用一個登陸具體應用地址。

 

注:可以通過trace追蹤日誌,查看具體消息的走向,即通過代理端口轉發至哪個應用,做消息處理。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章