在CentOS上實現RabbitMQ的負載均衡可以通過以下幾種方法:
sudo yum install haproxy -y
/etc/haproxy/haproxy.cfg
文件,添加以下配置:global
log /dev/log local0
log /dev/log local1 notice
daemon
maxconn 256
defaults
log global
mode tcp
option tcplog
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
frontend rabbitmq_front
bind *:5672
default_backend rabbitmq_back
backend rabbitmq_back
balance roundrobin
server rabbit1 192.168.1.101:5672 check
server rabbit2 192.168.1.102:5672 check
sudo systemctl start haproxy
sudo systemctl enable haproxy
rabbitmqctl set_policy ha-all "" '{"ha-mode":"all"}'
rabbitmqctl set_parameter federation_upstream_set "rabbit@rabbit2"
在 rabbit2 上配置Federation:rabbitmqctl set_policy ha-all "" '{"ha-mode":"all"}'
rabbitmqctl set_parameter federation_downstream_set "rabbit@rabbit1"
sudo rabbitmq-plugins enable rabbitmq_shovels
sudo rabbitmq-plugins enable rabbitmq_shovel_management
rabbitmqadmin declare shovel nameshovel_from_rabbit1_to_rabbit2 source rabbit@rabbit1 destination rabbit@rabbit2 routing_key queue_name
rabbitmqctl set_policy ha-all "" '{"ha-mode":"all"}'
以上方法各有優缺點,選擇哪種方法取決于你的具體需求和環境。HAProxy適用于簡單的負載均衡,Federation和Shovel適用于跨集群的消息路由,而Mirroring Queue則適用于高可用性和負載均衡的組合。