RabbitMQ高可用集群搭建实战

1、集群搭建

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

2、集群方案的原理

RabbitMQ这款消息队列中间件产品本身是基于Erlang编写,Erlang语言天生具备分布式特性(通过同步Erlang集群各节点的cookie来实现)。

RabbitMQ本身不需要像ActiveMQ、Kafka那样通过ZooKeeper分别来实现HA方案和保存集群的元数据。

在这里插入图片描述
在这里插入图片描述
haproxy请求分发

可以理解为通过nginx来作后端的负载均衡,HaProxy可以通过监听一个统一的端口对外提供能力,然后内部进行分发。除支持http7层处理外,还顺便为mysql支持4层转发。

程序进行访问时,就不再访问具体的rabbitmq机器,而是访问这个HaProxy所在的机器。这里就提到需要一个额外的机器来提供服务,但是由于只为HaProxy使用,其根据很低,一个最低配机器即可,费用不大。同时,相应的端口也填写HaProxy所暴露的端口即可。对外即认为也只仍然只有一个rabbitmq,即对外是透明的。

KeepAlived高可用

保证服务不会单点的作法就是加机器,但加机器就会出现多个ip,如何保证前端程序使用单个ip又能保证后端的实际处理机器为多台,这就是KeepAlived的作用。

我们为了保证HaProxy的高可用,已经又加了一个机器,即为HaProxyA和HaProxyB。

通过KeepAlived,我们可以创造出第3个IP,由ip3来对外提供服务,但是与HaProxy的转发性质不同,这里的ip3实际上就是HaProxyA和HaProxyB的一个同名映射。可以理解为HaProxyA和HaProxyB都在争抢这个ip,哪个争抢到了,就由哪个来提供服务。

3、使用多台服务器进行集群搭建

官方文档:https://www.rabbitmq.com/clustering.html

#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执行

# rabbitmqctl stop_app: 类似于java中的STW, 让MQ不再接收请求
[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

4、负载均衡-HAProxy

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

安装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

配置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

在这里插入图片描述
注意,此时连接的rabbitmq的ip是HAProxy服务的IP地址和设置的监听端口号。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值