当前位置:   article > 正文

CentOS7 YUM安装RabbitMQ3.8.19高可用集群_rabbitmq3.8.19延迟插件安装下载

rabbitmq3.8.19延迟插件安装下载

1. 安装基础环境

yum install -y wget unzip gcc gcc-c++ ntp git make mlocalte mlocate psmisc iotop net-tools lsof vim screen iotop mtr sysstat dmidecode lrzsz socat
  • 1

2. 部署JDK1.8

# 创建用于存储安装包的目录
mkdir /root/soft
cd /root/soft
# 此文件为jdk安装,rz为上传,也可手动上传。
rz jdk-8u261-linux-x64.tar.gz
# 上述文件提取链接,链接: https://pan.baidu.com/s/1bvHDSAezvxCF6D6pCYnDBg 提取码: qw8k
tar zxf jdk-8u261-linux-x64.tar.gz -C /usr/local/
chown -R root.root /usr/local/jdk1.8.0_261
# 设置环境变量
vim /etc/profile
#写入内容如下:
JAVA_HOME=/usr/local/jdk1.8.0_261
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC JAVA_HOME CLASSPATH
# 重新加载文件
source /etc/profile
# 查看java是否存在
java -version
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

3. 部署Erlang/OTP 23.3.4.4

# 下载Erlang
wget https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.4/erlang-23.3.4.4-1.el7.x86_64.rpm
# 上述文件若下载不下可进行提取,链接:https://pan.baidu.com/s/1F940ybqa8x2Enc5djZ_ntw 提取码: v5r3
rpm -ivh erlang-23.3.4.4-1.el7.x86_64.rpm
  • 1
  • 2
  • 3
  • 4

4. 部署单机版rabbitmq3.8.19及相关插件

# 下载rabbitmq
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.19/rabbitmq-server-3.8.19-1.el7.noarch.rpm
# 上述文件若下载不下可进行提取,链接: https://pan.baidu.com/s/16_eeTzSDrWCmeE-XnBiMTQ 提取码: at82
rpm -ivh rabbitmq-server-3.8.19-1.el7.noarch.rpm
##启用rabbitmq_delayed_message_exchange插件,实现延迟队列:
cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.8.19/plugins
wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.8.0/rabbitmq_delayed_message_exchange-3.8.0.ez
# 上述文件若下载不下可进行提取,链接: https://pan.baidu.com/s/1MxOkSEXsQDUJ3d0KaphvIw 提取码: ae3f
rabbitmq-plugins enable rabbitmq_delayed_message_exchange
##启用rabbitmq_management管理插件:
rabbitmq-plugins enable rabbitmq_management
rabbitmq-plugins list
##设置开机启动:
systemctl enable rabbitmq-server
systemctl list-unit-files|grep rabbit
systemctl restart rabbitmq-server
systemctl status rabbitmq-server
systemctl stop rabbitmq-server
##rabbitmq启动、状态、停止的非systemctl命令:
rabbitmq-server -detached
rabbitmqctl status
rabbitmqctl stop
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

注:1-4步每台服务器均需操作

5. 配置rabbitmq集群

5.1. 选取某IP为主节点,读取其cookie,并复制到其他节点(节点之间通过cookie确定相互是否可通信,权限400,只读)举例:目前有172.16.10.1(hostname为mq01-10-1)及172.16.10.2(hostname为mq02-10-2)两台服务器,我们已172.16.10.1为主节点

# 查看主节点也就是172.16.10.1的cookie
cd /var/lib/rabbitmq/
cat .erlang.cookie
# 将查看到的内容复制到172.16.10.2的/var/lib/rabbitmq/.erlang.cookie下(直接覆盖)
# 设置只读权限(每台服务器都需要执行)
chmod 400 .erlang.cookie
# 查看确认一下权限
ll -a .erlang.cookie
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5.2. 各节点依次启动rabbitmq

rabbitmq-server -detached 或者 systemctl restart rabbitmq-server
  • 1

5.3. 查看各节点和集群的状态:

rabbitmqctl status
rabbitmqctl cluster_status
  • 1
  • 2

5.4. 建立集群,以mq01-10-1为主节点

#建立集群之前请先在各个节点的/etc/hosts记录好主机名及IP
#在172.16.10.1中设置
vim /etc/hosts
172.16.10.2 mq02-10-2
#在172.16.10.2中etc/hosts设置
172.16.10.1 mq01-10-1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
#在主节点以外的mq02-10-2上执行:
rabbitmqctl stop_app
#显示如下
#-----------------------
#Stopping rabbit application on node rabbit@mq02-10-2 ...
#-----------------------
#重置
rabbitmqctl reset
#-----------------------------------
#Resetting node rabbit@mq02-10-2  ...
#-----------------------------------
#加入主节点
rabbitmqctl join_cluster rabbit@mq01-10-1
#------------------------------------------------------
#Clustering node rabbit@mq01-10-1 with rabbit@mq01-10-2
#------------------------------------------------------
#启动mq
rabbitmqctl start_app
#----------------------------------
#Starting node rabbit@mq01-10-2 ...
#completed with 0 plugins.
#----------------------------------
# 查看集群状态
rabbitmqctl cluster_status
#---------------------------------------------
#Cluster status of node rabbit@mq02-10-2 ...
#Basics
#Cluster name: rabbit@mq02-10-2
#Disk Nodes
#rabbit@mq01-10-1
#rabbit@mq02-10-2
#Running Nodes
#rabbit@mq01-10-1
#rabbit@mq02-10-2
#...........
#---------------------------------------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

5.5. 添加后台管理账户(只需一台操作,集群之间同步)

rabbitmqctl add_user xiaosen xiaosen123
#---------
# Adding user "xiaosen" ...
#--------
#设置该用户为管理员
rabbitmqctl set_user_tags xiaosen administrator
#-------
#Setting tags for user "xiaosen" to [administrator] ...
#------
#设置访问权限
rabbitmqctl set_permissions -p / xiaosen ".*" ".*" ".*"
#------
#Setting permissions for user "xiaosen" in vhost "/" ...
#-------
#设置镜像模式
rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
#-----------
#Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
#------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

6.访问rabbitmq管理平台:http://{ip}:15672 账号密码为5.5设置的

至此集群搭建完成
一些mq的使用命令

# 当前窗口启动 rabbitmq
rabbitmq-server
# 后台启动 rabbitmq
rabbitmq-server -detached
# 停止 rabbitmq
rabbitmqctl stop
# 查看所有队列
rabbitmqctl list_queues
# 查看所有虚拟主机
rabbitmqctl list_vhosts
# 在Erlang VM运行的情况下启动RabbitMQ应用
rabbitmqctl start_app 
rabbitmqctl stop_app
# 查看节点状态
rabbitmqctl status
# 查看所有可用的插件
rabbitmq-plugins list
# 启用插件
rabbitmq-plugins enable <plugin-name>
# 停用插件
rabbitmq-plugins disable <plugin-name>
# 添加用户
rabbitmqctl add_user username password
# 列出所有用户
rabbitmqctl list_users
# 删除用户
rabbitmqctl delete_user username
# 清除用户权限
rabbitmqctl clear_permissions -p vhostpath username
# 列出用户权限
rabbitmqctl list_user_permissions username
# 修改密码
rabbitmqctl change_password username newpassword
# 设置用户权限
rabbitmqctl set_permissions -p vhostpath username ".*" ".*" ".*"
# 创建虚拟主机
rabbitmqctl add_vhost vhostpath
# 列出虚拟主机上的所有权限
rabbitmqctl list_permissions -p vhostpath
# 删除虚拟主机
rabbitmqctl delete_vhost vhost vhostpath
# 移除所有数据,要在 rabbitmqctl stop_app 之后使用
rabbitmqctl reset
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

为了保证rabbitmq的高可用性,我们可以在这基础上部署haproxy及keepalived来保证服务的高可用性
配合阿里云的havip(高可用虚拟IP,设ip为:172.16.100.100)来进行搭建。实现主备切换功能。主宕机了由备来替补
如图:
在这里插入图片描述

1. 安装haproxy(主及备都需要安装)
yum -y install haproxy keepalived
cd /etc/haproxy/
cp haproxy.cfg haproxy.cfg.old
vim haproxy.cfg
# 配置如下:
global
    log         127.0.0.1 local2 info
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
defaults
    mode                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option http-server-close
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           90s
    timeout connect         90s
    timeout client          120s
    timeout server          120s
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
listen private_monitoring
    bind 0.0.0.0:5100
    mode http
    option httplog
    stats enable
    stats uri /stats
    stats refresh 30s
    # 查看haproxy状态的登录账号及密码 可访问:http://{HaVip}:5100/stats进行查看
    stats auth admin:mg_admin
    stats realm welcome login\ Haproxy
    stats hide-version
listen rabbitmq_cluster
    bind 0.0.0.0:5672
    timeout client          3h
    timeout server          3h
    mode tcp
    option tcplog
    balance roundrobin
    #mq的地址
    server mq001 172.16.10.1:5672 check inter 2000 rise 2 fall 2
    server mq002 172.16.10.2:5672 check inter 2000 rise 2 fall 2
listen rabbitmq_admin
    bind 0.0.0.0:15672
    #mq管理后台的地址
    server mq001 172.16.10.1:15672
    server mq002 172.16.10.2:15672
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
#(UDP配置取消注释,添加local0的haproxy日志)
vim /etc/rsyslog.conf 
-----------------------------------------------------------------
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Save haproxy log
 local2.*                       /var/log/haproxy/haproxy.log
-----------------------------------------------------------------
vim /etc/sysconfig/rsyslog
------------------------------
#SYSLOGD_OPTIONS=""
SYSLOGD_OPTIONS="-r -m 0 -c 2"
------------------------------
# 重启日志服务
systemctl restart rsyslog.service
# 开机自启动
systemctl enable haproxy
# 启动服务
systemctl start haproxy
# 重启
systemctl restart haproxy
# 查看状态
systemctl status haproxy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
2.配置keepalived主从
# 配置主节点的keepalived.conf
vim /etc/keepalived/keepalived.conf
-----------------------------------------------
global_defs {
    router_id HA115
    script_user root
    enable_script_security
}
vrrp_script chk_haproxy
{
     script "/etc/keepalived/scripts/haproxy_check.sh"
     interval 1
     timeout 1
     fall 2
}
vrrp_instance haproxy {
    state MASTER
    interface eth0
    virtual_router_id 115
    # 开启抢占模式 主机宕机后再恢复需要再次回到主
    preempt yes
    # 优先级 主机要比备机的优先级高
    priority  150
    unicast_src_ip 172.16.10.3
    unicast_peer {
	    172.16.10.4
    }
    virtual_ipaddress {
        172.16.10.100
    }
    track_script {
         chk_haproxy
    }
    notify_master "/etc/keepalived/scripts/haproxy_master.sh"
}
----------------------------------------------------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
## 配置备节点的keepalived.conf
vim /etc/keepalived/keepalived.conf
----------------------------------------------------------
global_defs {
    router_id HA115
    script_user root
    enable_script_security
}
vrrp_script chk_haproxy
{
     script "/etc/keepalived/scripts/haproxy_check.sh"
     interval 1
     timeout 1
     fall 2
}
vrrp_instance haproxy {
    state BACKUP
    interface eth0
    virtual_router_id 115
    # 开启抢占模式 主机宕机后再恢复需要再次回到主
    preempt yes
    # 优先级 备机要主机的优先级低
    priority  100
    unicast_src_ip 172.16.10.4
    unicast_peer { 
        172.16.10.3
    } 
    virtual_ipaddress {
        172.16.10.100
    }
    track_script {
         chk_haproxy
    }
    notify_master "/etc/keepalived/scripts/haproxy_master.sh"
}
----------------------------------------------------------
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
# 配置检测脚本haproxy_check.sh 两台服务器都需要配置
cd /etc/keepalived && mkidr scripts && vim haproxy_check.sh
----------------------------------------------------------
#!/bin/bash
LOGFILE="/var/log/keepalived-haproxy-state.log"
date >> $LOGFILE
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
        echo "fail: check_haproxy status" >> $LOGFILE
        exit 1
else
        echo "success: check_haproxy status" >> $LOGFILE
        exit 0
fi
----------------------------------------------------------
# 配置检测脚本haproxy_master.sh 两台服务器都需要配置
cd /etc/keepalived && mkidr scripts && vim haproxy_master.sh
----------------------------------------------------------
LOGFILE="/var/log/keepalived-haproxy-state.log"
echo "Being Master ..." >> $LOGFILE
----------------------------------------------------------
#设置可执行权限 必须设置700权限,若权限过大,keepalived会认为不安全,从而导致运行失败
chmod 700 haproxy_check.sh
chmod 700 haproxy_master.sh
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
# 设置keepalived开启自启
systemctl enable keepalived.service
# 启动keepalived主从
systemctl restart keepalived
# 查看产生的日志
tail -f /var/log/messages
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

至此完成了高可用的搭建
可以试着关掉172.16.100.1或者172.16.100.2来验证一下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/568739
推荐阅读
相关标签
  

闽ICP备14008679号