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:UsersCurrent User.erlang.cookie(%HOMEDRIVE% + %HOMEPATH%.erlang.cookie) orC:Documents and SettingsCurrent 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

原文地址:https://www.cnblogs.com/zhangqingping/p/4167112.html