rabbiMQ主备

一、HAproxy

下载haproxy :[https://www.haproxy.org/#down]

https://www.haproxy.org/download/2.5/src/haproxy-2.5.0.tar.gz

编译:

make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1  USE_PCRE=1 USE_SYSTEMD=1
make install make install PREFIX=/opt/haproxy

config.conf

#全局配置
global
        #日志输出配置,所有日志都记录在本机,通过local0输出
        log 127.0.0.1 local0 info
        #最大连接数
        maxconn 4096
        #改变当前的工作目录
        chroot /home/haproxy
        #以指定的UID运行haproxy进程
        uid 99
        #以指定的GID运行haproxy进程
        gid 99
        #以守护进程方式运行haproxy #debug quiet
        daemon
        #debug
        #当前进程pid文件
        pidfile /home/haproxy/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

        maxconn 2000
        #连接超时
        timeout connect 5s
        #客户端超时
        timeout client 120s
        #服务端超时
        timeout server 120s

#绑定配置
listen rabbitmq_cluster
        bind 0.0.0.0:5672
        #配置TCP模式
        mode tcp
        #加权轮询
        balance roundrobin
        server rabbit188 192.168.1.188:5672 check inter 5000 rise 2 fall 3 weight 1
        server rabbit60 192.168.1.188:5672 check inter 5000 rise 2 fall 3 weight 1
                # 备注: rabbitmq集群节点配置 #inter 每隔五秒对mq集群做健康检查,2次正确证明服务器可用,3次失败证明服务器不可用,并且配置主备机制

listen rabbitmq_admin
    bind 0.0.0.0:15672
    balance  roundrobin
    server      rabbit1 192.168.1.188:15672
    server      rabbit2 192.168.1.60:15672

    #haproxy监控页面地址
listen monitor
        bind 0.0.0.0:8100
        mode http
        option httplog
        stats enable
        stats uri /stats
        stats refresh 5s
                    stats auth admin:1234 #登陆账户名密码

启动:

haproxy -f config.conf

二、MQ集群

MQ集群搭建:

主节点1:192.168.1.188
从节点2:192.168.1.60

修改 hostname文件 vim /etc/hostname ,其他同,这个东西改完要重启服务器节点之间才能通信

rabbit188.localdomain

修改hosts文件 vim /etc/hosts

192.168.1.188 rabbit0       //注意不能带.注意-主机名称也要更改
192.168.1.60 rabbit1

MQ安装:

 ./rabbitmq-Install-bin

页面访问:192.168.1.1.188:15672 账号:admin 密码:*********

从节点:

systemctl stop rabbitmq-server
rm -rf /var/lib/rabbitmq/*
systemctl start rabbmitmq-server

# on rabbit2
rabbitmqctl stop_app
# => Stopping node rabbit@rabbit2 ...done.

rabbitmqctl reset
# => Resetting node rabbit@rabbit2 ...

rabbitmqctl join_cluster rabbit@rabbit1
# => Clustering node rabbit@rabbit2 with [rabbit@rabbit1] ...done.

rabbitmqctl start_app
# => Starting node rabbit@rabbit2 ...done.

节点之间使用相同Erlang Cookie,关闭对端后传输主节点 .erlang.cookie 到从

scp /var/lib/rabbitmq/.erlang.cookie 192.168.1.60:/var/lib/rabbitmq/

重置节点集群状态:

[root@localhost ~]# rabbitmqctl stop_app&&rabbitmqctl reset     
Stopping rabbit application on node rabbit@localhost ...
Resetting node rabbit@localhost ...
[root@localhost ~]# rabbitmqctl start_app
Starting node rabbit@localhost ...
[root@localhost ~]#

实操记录 主:

[root@rabbit188 ~]# vim /etc/hostname
rabbit188.localhost
[root@rabbit188 ~]#

[root@rabbit188 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.188 rabbit188
192.168.1.60 rabbit60
[root@rabbit188 ~]# reboot
[root@rabbit188 ~]# ./rabbitmq-Install-bin
[root@rabbit188 ~]# scp /var/lib/rabbitmq/.erlang.cookie 192.168.1.60:/var/lib/rabbitmq/

实操记录 从:

[root@rabbit60 ~]# vim /etc/hostname
rabbit60.localhost
[root@rabbit60 ~]#
[root@rabbit60 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.188 rabbit188
192.168.1.60 rabbit60
[root@rabbit60 ~]#

[root@rabbit60 ~]# reboot

[root@rabbit60 ~]# systemctl stop rabbitmq-server
[root@rabbit60 ~]# rm -rf /var/lib/rabbitmq/*
[root@rabbit60 ~]# systemctl start rabbitmq-server
[root@rabbit60 ~]# rabbitmqctl cluster_status

[root@rabbit60 ~]# rabbitmqctl stop_app
Stopping rabbit application on node rabbit@rabbit60 ...
[root@rabbit60 ~]# rabbitmqctl reset
Resetting node rabbit@rabbit60 ...
[root@rabbit60 ~]# rabbitmqctl join_cluster rabbit@rabbit188
Clustering node rabbit@rabbit60 with rabbit@rabbit188

21:15:58.192 [warn]  Feature flags: the previous instance of this node must have failed to write the `feature_flags` file at `/var/lib/rabbitmq/mnesia/rabbit@rabbit60-feature_flags`:

21:15:58.212 [warn]  Feature flags:   - list of previously disabled feature flags now marked as such: [:maintenance_mode_status]

21:15:59.098 [warn]  Feature flags: the previous instance of this node must have failed to write the `feature_flags` file at `/var/lib/rabbitmq/mnesia/rabbit@rabbit60-feature_flags`:

21:15:59.099 [warn]  Feature flags:   - list of previously enabled feature flags now marked as such: [:maintenance_mode_status]

21:15:59.215 [error] Failed to create a tracked connection table for node :rabbit@rabbit60: {:node_not_running, :rabbit@rabbit60}

21:15:59.215 [error] Failed to create a per-vhost tracked connection table for node :rabbit@rabbit60: {:node_not_running, :rabbit@rabbit60}

21:15:59.215 [error] Failed to create a per-user tracked connection table for node :rabbit@rabbit60: {:node_not_running, :rabbit@rabbit60}
[root@rabbit60 ~]# 

[root@rabbit60 ~]# rabbitmqctl start_app
Starting node rabbit@rabbit60 ...
[root@rabbit60 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@rabbit60 ...
Basics

Cluster name: rabbit@rabbit60

Disk Nodes

rabbit@rabbit188
rabbit@rabbit60

Running Nodes

rabbit@rabbit188
rabbit@rabbit60

Versions

rabbit@rabbit188: RabbitMQ 3.9.7 on Erlang 23.3.4.7
rabbit@rabbit60: RabbitMQ 3.9.7 on Erlang 23.3.4.7

Maintenance status

Node: rabbit@rabbit188, status: not under maintenance
Node: rabbit@rabbit60, status: not under maintenance

...
Listeners

Node: rabbit@rabbit188, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbit188, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbit188, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: rabbit@rabbit60, interface: [::], port: 15672, protocol: http, purpose: HTTP API
Node: rabbit@rabbit60, interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: rabbit@rabbit60, interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
...

三、镜像队列

镜像队列:可以同步队列消息到集群中其他节点,任意节点执行,所有节点同步策略

[root@rabbit188 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
[root@rabbit188 ~]# 

json参数说明:

ha-mode:镜像模式,分类:all/exactly/nodes,all存储在全部节点;exactly存储x个节点,节点的个数由ha-params指定;nodes指定存储的节点上名称,经过ha-params指定;
ha-params:做为参数,为ha-mode的补充;
ha-sync-mode:镜像消息同步方式:automatic(自动),manually(手动);

同步test开头队列:

rabbitmqctl set_policy ha-all "^test" '{"ha-mode":"all"}'

查看同步情况

[root@rabbit60 ~]#  rabbitmqctl list_queues name slave_pids synchronised_slave_pids
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name    slave_pids      synchronised_slave_pids
test    [<rabbit@rabbit188.1639117875.6317.0>]  [<rabbit@rabbit188.1639117875.6317.0>]
[root@rabbit60 ~]#

查看镜像队列:

[root@rabbit188 ~]# rabbitmqctl list_policies
Listing policies for vhost "/" ...
vhost   name    pattern apply-to        definition      priority
/       ha-all  ^       all     {"ha-mode":"all","ha-sync-mode":"automatic"}    0
[root@rabbit188 ~]#
上一篇:说说分布式文件存储系统


下一篇:性能测试中的nginx和MQ配置优化