高可用架構是 Keepalived + HAproxy,用 HAproxy 來作 RabbitMQ 負載均衡和高可用,用 Keepalived 來保證 HAproxy 的高可用。spring
組件 | 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.100shell
以前安裝過RabbitMQ。bash
也安裝過單節點的HAProxy,IP地址爲:192.168.0.104。網絡
如今,須要把單節點的HAProxy克隆一份,造成兩個HAProxy,新克隆出來的HAProxy節點的IP地址爲:192.168.0.105。架構
在兩個節點使用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文件:fetch
vi /etc/keepalived/keepalived.conf
修改192.168.0.104節點的配置文件:spa
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節點的配置文件:code
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
修改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