rabbitMQ 安裝,集群搭建, 編碼


RabbitMQ

一、背景


命令行工具:
http://www.rabbitmq.com/man/rabbitmqctl.1.man.html

介紹入門文章:
http://blog.csdn.net/anzhsoft/article/details/19563091
內容比較清晰: http://www.diggerplus.org/archives/3110

Exchange、Queue
producer把消息發送到Exchange(帶上route key),consumer聲明queue(帶上bind key),然后根據結合Exchange的類型(fanout、direct、topic、headers),進行路由到不同的queue上面。
 fanout(廣播):route key和 bind key不生效,所有bind到改Exchange的queue都會收到同一份message。bind的時候,queue的名稱必須不一樣(如果一樣,兩個一樣的consumer,只有其中一個能收到消息)
direct:必須要route key 和 bind key完全相等,才會收到(一個queue可以提供多個bind key到Exchange,只要其中一個滿足,就會收到消息;多個queue也可以用同一個bind key,這樣會收到同樣的消息)

topic:對route key進行了簡單的正則匹配

同時還支持RPC模式:


如果在Exchange上面沒有bind一個queue,即使producer發送的是一個持久化消息,后面再bind queue的consumer也是收不到之前的消息的。所以queue必須要先創建,如果想一直收到消息,queue必須是持久化的。

q, err := ch.QueueDeclare( 
  "queuename", // name
  true, // durable
  false, // delete when usused
  true, // exclusive
  false, // no-wait
  nil, // arguments
 

同樣,如果想consumer沒起來之前,也能收到producer之前發送的消息,那需要提前把對應的queue name創建好,bind好對應的Exchange


二、開搞,部署,搭建集群


可參考集群部署:
http://blog.csdn.net/jljf_hh/article/details/17381425

部署之前,必須要弄明白erlang cookie這個玩意兒。
Rabbitmq的集群是依附於erlang的集群來工作的,所以必須先構建起erlang的集群景象。Erlang的集群中各節點是經由過程一個magic cookie來實現的,這個cookie存放在 $home/.erlang.cookie 中(像我的root用戶安裝的就是放在我的root/.erlang.cookie中),文件是400的權限。所以必須包管各節點cookie對峙一致,不然節點之間就無法通信。

Erlang cookie
Erlang nodes use a cookie to determine whether they are allowed to communicate with each other - for two nodes to be able to communicate they must have the same cookie. The cookie is just a string of alphanumeric characters. It can be as long or short as you like.
Erlang will automatically create a random cookie file when the RabbitMQ server starts up. The easiest way to proceed is to allow one node to create the file, and then copy it to all the other nodes in the cluster.
On Unix systems, the cookie will be typically located in ll /var/lib/rabbitmq/.erlang.cookie or$HOME/.erlang.cookie.
On Windows, the locations are C:\Users\Current User\.erlang.cookie(%HOMEDRIVE% + %HOMEPATH%\.erlang.cookie) orC:\Documents and Settings\Current User\.erlang.cookie, and C:\Windows\.erlang.cookie for RabbitMQ Windows service. If Windows service is used, the cookie should be placed in both places.
As an alternative, you can insert the option "-setcookie cookie" in the erl call in the rabbitmq-server andrabbitmqctl scripts. 

參考:http://www.tuicool.com/articles/YbYvIj


Ubuntu上面部署:
直接參考官網,幾個命令就搞掂:
To use our APT repository:
1)Add the following line to your /etc/apt/sources.list:
  deb http://www.rabbitmq.com/debian/ testing main
(Please note that the word testing in this line refers to the state of our release of RabbitMQ, not any particular Debian distribution. You can use it with Debian stable, testing or unstable, as well as with Ubuntu. We describe the release as "testing" to emphasise that we release somewhat frequently.)
(optional) To avoid warnings about unsigned packages, add our public key to your trusted key list using apt-key(8):
2)wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc
   sudo apt-key add rabbitmq-signing-key-public.asc
3)Run apt-get update.
Install packages as usual; for instance,
sudo apt-get install rabbitmq-server

CentOS上面部署:
安裝一個erlang的安裝環境:
rpm -i http://mirror.bjtu.edu.cn/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

yum install erlang
安裝rabbitMQ(RPM鏈接可以從http://www.rabbitmq.com/install-rpm.html來獲取)

rpm -ivh http://www.rabbitmq.com/releases/rabbitmq-server/v3.4.2/rabbitmq-server-3.4.2-1.noarch.rpm

 

雖然總結起來是這么幾行,特么的琢磨了好幾個小時。

安裝完畢之后,就可以組織集群了。


啟動:/sbin/service rabbitmq-server start
可以通過命令開啟管理界面插件:rabbitmq-plugins enable rabbitmq_management

然后:http://IP:15672/ 進入管理界面來查看rabbitMQ的信息。

開始搭建集群:
1。添加用戶

在兩台機器分別創建一個用戶,並設置為管理員,否則通過管理界面是無法登陸的。

rabbitmqctl add_user sky password
rabbitmqctl set_user_tags sky administrator


2.同步erlang cookie 數據
如果修改erlang cookie文件之前,erlang的進程和rabbitmq已經啟動,把他們都stop或者kill掉。改完erlang cookie之后再重啟

Erlang cookie的路徑上面說過,保證內容一致即可。


3.配置hosts,把各個節點的host name和 IP配置在hosts中,以便可以相互通信
這里的hostname 是 rabbit@OLYMTECH 格式中@后面的文字,例如這里是:10.12.13.54 OLYMTECH 

4.分別啟動兩個節點
rabbitmq-server -detached


5.在從節點上面,join到master節點,
host2# rabbitmqctl stop_app
host2# rabbitmqctl join_cluster rabbit@OLYMTECH                  PS:這里OLYMTECH是不用執行這個的,是讓OLYMTECH以外的node加入到以他為名的cluster中去
host2# rabbitmqctl start_app如果加入和啟動成功,就能在管理界面上面看到節點信息了(這里兩個節點都是持久化類型的,如果有需要可以在join_cluster 加上—ram選項,配置為內存節點):

這里如果出現啟動或stop失敗,ps rabbit進程,按個全部kill掉,再重來一遍。


6.添加策略
rabbitmqctl set_policy ha-all "^ha\." '{"ha-mode":"all"}'
凡是ha. 開始的queue的數據,會同步到集群的所有node
OK,這樣就搭建好了,然后就可以寫代碼搞起了。

 

這里如果要做負載,HA,就需要在前面搭一套TCP的代理,負責監聽轉發,例如:HAProxy, LVS+keepalive。我還沒搞過。


程序連接時報錯:
2014/12/16 11:44:31 emit_log.go:14: Failed to connect to RabbitMQ: Exception (403) Reason: "no access to this ghost"

是因為沒有配置該用戶的訪問權限,可以通過
rabbitmqctl add_vhost skytest
來添加,並賦予權限:
rabbitmqctl set_permissions -p sky skytest ".*" ".*" ".*"
同樣,代碼在連接的時候,必須制定對應的vhost,否則是沒有訪問權限滴:
conn, err := amqp.Dial("amqp://sky:password@10.44.55.66:5672/skytest”)

go語言的庫,可參考:

https://godoc.org/github.com/streadway/amqp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM