目录

 

1.RabbitMQ集群搭建

1.1 集群方案的原理

1.2 如下案例中使用多台云服务器进行集群搭建

1.3 负载均衡-HAProxy

1.3.1 安装HAProxy

1.3.2 配置HAProxy


1.RabbitMQ集群搭建

一般来说,如果只是为了学习RabbitMQ或者验证业务工程的正确性那么在本地环境或者测试环境上使用其单实例部署就可以了,但是出于MQ中间件本身的可靠性、并发性、吞吐量和消息堆积能力等问题的考虑,在生产环境上一般都会考虑使用RabbitMQ的集群方案。

1.1 集群方案的原理

RabbitMQ这款消息队列中间件产品本身是基于Erlang编写,Erlang语言天生具备分布式特性(通过同步Erlang集群各节点的cookie来实现)。RabbitMQ本身不需要像ActiveMQ、Kafka那样通过ZooKeeper分别来实现HA方案和保存集群的元数据。

rabbitmq python 集群 rabbitmq集群搭建_分布式

1.2 如下案例中使用多台云服务器进行集群搭建

#1、环境准备
hostnamectl set-hostname m1
hostnamectl set-hostname m2

#2、统一 erlang.cookie 文件中 cookie 值    将m1中的 .erlang.cookie 同步到 m2中
scp  /var/lib/rabbitmq/.erlang.cookie  m2:/var/lib/rabbitmq/.erlang.cookie

#3、Rabbitmq 集群添加节点
    #重启 m2机器中 rabbitmq 的服务 在 m2执行

[root@rabbitmq2 ~]# rabbitmqctl stop_app
[root@rabbitmq2 ~]# rabbitmqctl join_cluster --ram rabbit@m2
[root@rabbitmq2 ~]# rabbitmqctl start_app
[root@rabbitmq2 ~]# rabbitmq-plugins enable rabbitmq_management
[root@rabbitmq2 ~]# systemctl restart rabbitmq-server.service 

#4、查看集群信息  
rabbitmqctl cluster_status

1.3 负载均衡-HAProxy

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案,包括Twitter,Reddit,StackOverflow,GitHub在内的多家知名互联网公司在使用。HAProxy实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。

1.3.1 安装HAProxy

#1、安装   
yum  install haproxy


#2、配置haproxy.cfg文件   具体参照 如下 1.5.2 配置HAProxy
vim  /etc/haproxy/haproxy.cfg

#3、启动haproxy
systemctl start haproxy

#4、查看haproxy进程状态
systemctl status haproxy.service
#状态如下说明 已经启动成功 Active: active (running)
 
#访问如下地址对mq节点进行监控
http://服务器IP:1080/haproxy_stats

#代码中访问mq集群地址,则变为访问haproxy地址:5672

1.3.2 配置HAProxy

配置文件路径:/etc/haproxy/haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000


#对MQ集群进行监听
listen rabbitmq_cluster
	bind 0.0.0.0:5672
	option tcplog
	mode tcp 
	option  clitcpka
	timeout connect 1s 
	timeout client  10s
	timeout server  10s
	balance roundrobin
	server node1 节点1 ip地址:5672 check inter 5s rise 2 fall 3  
   server node2 节点2 ip地址:5672 check inter 5s rise 2 fall 3

#开启haproxy监控服务	
listen http_front
	bind 0.0.0.0:1080
	stats refresh 30s
	stats uri /haproxy_stats
	stats auth admin:admin

haproxy.cfg配置详解

listen rabbitmg cluster
bind 0.0.0.0:5672#通过5672对M1, M2进行映射
option tcplog #记录tcp连接的状态和时间
mode tcp#四层协议代理,即对TCP协议转发
option clitcpka #开启TCP的Keep Alive. (长连接模式)
timeout connect 1s #haproxy与mq建立连接的超时时间
timeout client 10s#客户端与haproxy最大空闲时间。
timeout server 10s #服务器与haproxy最大空闲时间
balance roundrobin #采用轮询转发消息
#每5秒发送一次心跳包,如连续两次有响应则代表状态良好。
#如连续三次没有响应,则视为服务故障,该节点将被剔除。
server node1 192.168.132.137:5672 check inter 5s rise 2 fall 3
server node2192.168.132.139:5672 check inter 5s rise 2 fall 3
listen http front
#监听端口
bind 0.0.0.0:1080
#统计页面自动刷新时间stats refresh 30s
#统计页面url
stats uri /haproxy?stats
#指定HAproxy访问用户名和密码设置
stats auth admin:admin