【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
概述
高可用架构是 Keepalived + HAproxy,用 HAproxy 来做 RabbitMQ 负载均衡和高可用,用 Keepalived 来保证 HAproxy 的高可用。
组件 | IP地址 | 端口 |
RabbitMQ磁盘节点-01 | 192.168.0.101 | 5672 |
RabbitMQ磁盘节点-02 | 192.168.0.102 | 5672 |
RabbitMQ磁盘节点-03 | 192.168.0.103 | 5672 |
HAproxy-01 | 192.168.0.104 | |
HAproxy-02 | 192.168.0.105 | |
Keepalived-Master | 192.168.0.104 | |
Keepalived-Backup | 192.168.0.105 |
虚拟IP:192.168.0.100
之前安装过RabbitMQ。
也安装过单节点的HAProxy,IP地址为:192.168.0.104。
现在,需要把单节点的HAProxy克隆一份,形成两个HAProxy,新克隆出来的HAProxy节点的IP地址为:192.168.0.105。
安装KeepAlived
在两个节点使用yum安装keepalived:
yum install -y keepalived
在两个节点的/etc/keepalived/文件夹下编写检测haproxy的shell脚本:
#!/bin/bash
case "$1" in
master)
notify master
service haproxy start
exit 0
;;
backup)
notify backup
service haproxy stop
exit 0
;;
fault)
notify fault
service haproxy stop
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac
编辑keepalived.cfg文件:
vi /etc/keepalived/keepalived.conf
修改192.168.0.104节点的配置文件:
vrrp_script chk_haproxy {
script "service haproxy status" # 服务探测,返回0说明服务是正常的
interval 1 # 每隔1秒探测一次
weight -2 # 不正常时,权重-1,即haproxy上线,权重加2;下线,权重减2
}
vrrp_instance haproxy {
state MASTER # 主机为MASTER,备机为BACKUP
interface ens33 # 监测网络端口,用ipconfig查看
virtual_router_id 108 # 主备机必须相同
priority 100 # 主备机取不同的优先级,主机要大。
advert_int 1 # VRRP Multicast广播周期秒数
authentication {
auth_type PASS # VRRP认证方式
auth_pass 1234 # VRRP口令 主备机密码必须相同
}
track_script { # 调用haproxy进程检测脚本,备节点不配置
chk_haproxy
}
track_interface {
bond0
}
virtual_ipaddress { # VIP 漂移地址 即集群IP地址
192.168.0.100/25 dev ens33
}
}
修改192.168.0.105节点的配置文件:
vrrp_instance haproxy {
state BACKUP
interface ens33
virtual_router_id 108
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
track_interface {
ens33
}
virtual_ipaddress {
192.168.0.100
}
notify_master "/etc/keepalived/notify.sh master" # 当前节点成为master时,通知脚本执行任务,一般用于启动某服务
notify_backup "/etc/keepalived/notify.sh backup" # 当前节点成为backup时,通知脚本执行任务,一般用于关闭某服务
}
在两个节点分别启动keepalived:
systemctl stop NetworkManager
systemctl start keepalived
systemctl enable keepalived
通过虚拟IP访问控制台
通过程序测试
修改RabbitMQ的连接地址为虚拟IP:
spring:
#消息队列配置
rabbitmq:
#addresses: 192.168.0.101:5672,192.168.0.102:5672,192.168.0.103:5672
host: 192.168.0.100 # 虚拟IP
port: 5672
username: guest
password: guest
virtual-host: /
publisher-returns: true
publisher-confirms: true
connection-timeout: 5000ms
listener:
simple:
acknowledge-mode: manual
prefetch: 1000
执行发送消息和接收消息,结果为:
2019-05-05 23:04:15.508 INFO 77132 --- [.168.0.100:5672] cc.xuepeng.config.RabbitMQConfig : ConfirmCallback -> 消息发布到交换器成功,id:CorrelationData [id=581660c3-e3f9-40a4-8905-50dd3681fd50]
2019-05-05 23:04:15.518 INFO 68844 --- [cTaskExecutor-1] cc.yesway.service.DirectQueueListener : 消费消息成功: ack
来源:oschina
链接:https://my.oschina.net/u/2450666/blog/3045619