一.搭建单机RabbitMQ
1.安装Erlang环境
tar xvzf otp_src_R16B.tar.gz
cd otp_src_R14B03
./configure1.安装Erlang环境
tar xvzf otp_src_R16B.tar.gz
cd otp_src_R14B03
根据 configure的提示安装一些其他包(yum -y install xxx)
检查完毕以后
make
make install (需要用root权限)
2.安装RabbitMQ
$ tar xvzf rabbitmq-server-3.3.1.tar.gz
$ cd rabbitmq-server-3.3.1
$ make
$ make TARGET_DIR=/opt/rabbitmq SBIN_DIR= /opt/rabbitmq/ sbin MAN_DIR= /opt/rabbitmq /man install
同样,少什么用yum装什么
3.开启监控后台
mkdir /etc/rabbitmq
./sbin/rabbitmq-plugins enable rabbitmq_management
删除后台guest用户只能在localhost登录的限制
cd /opt/rabbitmq/ ebin
vim rabbit.app
把loopback_users里的<<"guest">>删掉
4.单机运行RabbitMQ
启动: ./sbin/rabbitmq-server start(后台运行: nohup ./sbin/ rabbitmq-server start >nohup.out& )
停止:./sbin/ rabbitmqctl stop
5.访问监控后台
http://192.168.7.79:15672
guest/guest
二.搭建Rabbit集群
1.在各台机器上安装rabbitMQ以后,先修改各台机器的hosts
以 192.168.130.245 为例(hostname: host130-245 )
vim /etc/hosts
127.0.0.1 host130-245
192.168.130.245 host130-245
192.168.130.246 host130-246
2.复制.erlang.cookie
在主节点服务器上复制 $HOST/.erlang.cookie 到各个从节点相同位置
cd ~
chmod 777 .erlang.cookie
scp .erlang.cookie root@ 192.168.130.246:/root/
将主节点和其他子节点的.erlang.cookie权限改回400
chmod 400 .erlang.cookie
3.采用-detached 启动各节点
cd /opt/rabbitmq/sbin/
./rabbitmqctl stop
nohup ./rabbitmq-server -detached >nohup.out&
4.组成集群
到子节点上执行
cd /opt/rabbitmq/sbin/
./rabbitmqctl stop_app
./rabbitmqctl join cluster --ram rabbit@host130-245(如果不加--ram 则默认是disk模式)
./rabbitmqctl start_app
查看集群配置是否成功
./rabbitmqctl cluster_status
5.设置镜像队列策略
在任意节点执行
./rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
6.安装并配置HaProxy负载
tar -zxvf haproxy-1.4.24.tar.gz
cd haproxy-1.4.24
make TARGET=linux24 PREFIX=/opt/haproxy (此处为安装地址)
make install PREFIX= /opt/haproxy
从安装文件内复制haproxy.cfg
cp haproxy-1.4.24/example/haproxy.cfg haproxy/
编辑 haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen rabbitmq_cluster 0.0.0.0:56720
mode tcp
balance roundrobin
server host130-245 192.168.130.245:5672 check inter 2000 rise 2 fall 5
server host130-246 192.168.130.246:5672 check inter 2000 rise 2 fall 5
7.启动haproxy
cd /opt/haproxy/sbin/
./haproxy -f ../haproxy.cfg
ps: [ALERT] 236/190345 (21350) : [haproxy.main()] Cannot chroot(/usr/share/haproxy).
如果遇到这个报错,就执行
mkdir /usr/share/haproxy
检查是否启动成功
netstat -anp|grep 56720
ps:所需的安装包的地址:
http://pan.baidu.com/s/1nuBO8B3
7.启动haproxy
cd /opt/haproxy/sbin/
./haproxy -f ../haproxy.cfg
ps: [ALERT] 236/190345 (21350) : [haproxy.main()] Cannot chroot(/usr/share/haproxy).
如果遇到这个报错,就执行
mkdir /usr/share/haproxy
检查是否启动成功
netstat -anp|grep 56720
ps:所需的安装包的地址:
http://pan.baidu.com/s/1nuBO8B3