第11章 RabbitMQ扩展
11.1 消息追踪
11.1.1 Firehose
11.1.2 rabbitmq_tracing插件
11.1.3 案例:可靠性检测
11.2 负载均衡
11.2.1 客户端内部实现负载均衡
- 轮询法
-
加权轮询法
-
随机法
-
加权随机法
-
源地址哈希法
-
最小连接数法
11.2.2 使用HAProxy实现负载均衡
HAProxy提供高可用性、负载均衡及基于TCP和HTTP应用的代理,
- 安装HAProxy
- 配置HAProxy
HAProxy使用单一配置文件来定义所有属性,包括从前端IP到后端服务器。代码清单11-9展示了3个RabbitMQ节点组成集群的负载均衡配置。
配置相关环境说明如下所述。
HAProxy主机:192.168.0.9 5671
RabbitMQ1:192.168.0.2 5672
RabbitMQ2:192.168.0.3 5672
RabbitMQ3:192.168.0.4 5672
# 代码清单 11-9 HAProxy的配置
# 全局配置
global
# 日志输出配置,所有日志都记录在本级,通过local0输出
log 127.0.0.1 local0 info
# 最大连接数
maxconn 4096
# 改变当前的工作目录
chroot /opt/haproxy-1.7.8
# 以指定的UID运行haproxy进程
uid 99
# 以指定的GID运行haproxy进程
gid
# 以守护进程方式运行haproxy #debug #quiet
daemon
# debug
# 当前进程pid文件
pidfile /opt/haproxy-1.7.8/haproxy.pid
# 默认配置
defaults
# 应用全局的日志配置
log global
# 默认的模式 mode{tcp|http|health}
# TCP是4层,HTTP是7层,health只返回OK
mode tcp
# 日志类别 tcplog
option tcplog
# 不记录健康检查日志信息
option dontlognull
# 3次失败则认为服务不可用
retries 3
# 每个进程可用的最大连接数
maxconn 2000
#连接超时
timeout connect 5s
#客户端超时
timeout client 120s
#服务端超时
timeout server 120s
# 绑定配置
listen rabbitmq_cluster : 5671
# 配置TCP模式
mode tcp
# 简单的轮询
balance roundrobin
# RabbitMQ集群节点配置
server rmq_node1 192.168.0.2:5672 check inter 5000 rise 2 fall 3 weight 1
server rmq_node2 192.168.0.3:5672 check inter 5000 rise 2 fall 3 weight 1
server rmq_node3 192.168.0.4:5672 check inter 5000 rise 2 fall 3 weight 1
# haproxy 监控页面地址
listen monitor : 8100
mode http
option httplog
stats enable
stats uri /stats
stats refresh 5s
- 的
- 的