找回密码
 立即注册
首页 业界区 业界 RabbitMQ的安装集群、镜像队列配置

RabbitMQ的安装集群、镜像队列配置

寇油 5 天前
1 RabbitMQ集群简介

  RabbitMQ 集群是一个或多个节点的逻辑分组,每个节点共享用户、虚拟主机、队列、流、交换机、绑定、运行时参数和其他分布式状态。
  集群中的节点名称必须是唯一的。在集群中,节点使用节点名称来识别和相互联系。这意味着每个节点名称的主机名部分必须可以解析。CLI 工具也使用节点名称来识别和寻址节点。当节点启动时,它会检查是否已为其分配了节点名称。如果未显式配置值,则节点会解析其主机名并在其前面加上rabbit@ 以计算其节点名称。
  RabbitMQ代理操作所需的所有数据/状态都在所有节点之间复制。消息队列是此规则的例外,默认情况下消息队列驻留在一个节点上,但可以从所有节点查看和访问。要在集群中的节点之间复制队列,请使用支持复制的队列类型。
  一般提到的RabbitMQ镜像集群,实际上由集群加上经典消息队列的镜像复制策略构成。RabbitMQ 4.0及后续版本不再支持经典消息队列的镜像复制功能,因此本例采用RabbitMQ 3.13.7版实现。
2 安装配置规划

配置项主机一主机二  IP地址10.10.1.4510.10.1.46  主机名称KL5KL6  配置用户kylinkylin  安装文件目录/home/kylin/Install/home/kylin/Install  消息队列目录/data/rabbitmq/data/rabbitmq  相关端口使用默认值使用默认值  MQ测试用户test/password  MQ虚拟主机/test/  镜像策略名jxjq3 安装RabbitMQ

3.1 安装版本说明

  RabbitMQ仅4.0以下版本支持经典队列镜像,因此本例选择RabbitMQ 3.x版本中最新的3.13.7版。根据官方网站上RabbitMQ与Erlang的版本兼容性矩阵,Erlang版本选择26.x中最新的26.2.5.15。
  银河麒麟V10的桌面版和服务器版的安装体系差异很大,默认已经安装的包也不同,因此在安装Erlang之前,银河麒麟V10的桌面版和服务器版需要安装不同的包。本例基于以下银河麒麟版本建立。
  Kylin-Desktop-V10-GFB-HWE-Release-020-X86_64
  Kylin-Desktop-V10-GFB-020-Release-20.1.3-ARM64
  Kylin-Server-V10-GFB-Release-030-X86_64
  Kylin-Server-V10-GFB-030-Release-30.1.3-ARM64
3.2 麒麟V10桌面版(GFB)

3.2.1 安装前准备

使用管理员用户,创建安装文件目录。
  1. kylin@KL5:~$ mkdir Install
复制代码
3.2.2 安装ncurses

3.2.2.1 使用deb包安装

  银河麒麟V10桌面版(GFB)默认已安装libtinfo6、libncurses6、libncursesw6,版本为6.2-0kylin2,需要安装对应版本的libncurses-dev。本例拿到的是6.2-0kylin2.1版本,因为依赖关系除安装libncurses-dev外,需要安装对应版本的libtinfo6、libncurses6、libncursesw6。
  使用管理员账户,执行dpkg -i命令安装libncurses-dev及其依赖包。
  1. kylin@KL5:~/Install$ sudo dpkg -i libncurses-dev_6.2-0kylin2.1_amd64.deb libtinfo6_6.2-0kylin2.1_amd64.deb libncurses6_6.2-0kylin2.1_amd64.deb libncursesw6_6.2-0kylin2.1_amd64.deb
  2. 正在选中未选择的软件包 libncurses-dev:amd64。
  3. (正在读取数据库 ... 系统当前共安装有 187718 个文件和目录。)
  4. 准备解压 libncurses-dev_6.2-0kylin2.1_amd64.deb  ...
  5. 正在解压 libncurses-dev:amd64 (6.2-0kylin2.1) ...
  6. 准备解压 libtinfo6_6.2-0kylin2.1_amd64.deb  ...
  7. 正在解压 libtinfo6:amd64 (6.2-0kylin2.1) 并覆盖 (6.2-0kylin2) ...
  8. 准备解压 libncurses6_6.2-0kylin2.1_amd64.deb  ...
  9. 正在解压 libncurses6:amd64 (6.2-0kylin2.1) 并覆盖 (6.2-0kylin2) ...
  10. 准备解压 libncursesw6_6.2-0kylin2.1_amd64.deb  ...
  11. 正在解压 libncursesw6:amd64 (6.2-0kylin2.1) 并覆盖 (6.2-0kylin2) ...
  12. 正在设置 libtinfo6:amd64 (6.2-0kylin2.1) ...
  13. 正在设置 libncurses6:amd64 (6.2-0kylin2.1) ...
  14. 正在设置 libncursesw6:amd64 (6.2-0kylin2.1) ...
  15. 正在设置 libncurses-dev:amd64 (6.2-0kylin2.1) ...
  16. 正在处理用于 man-db (2.9.1-1kylin0k1) 的触发器 ...
  17. 正在处理用于 libc-bin (2.31-0kylin9.1k20.8) 的触发器 ...
复制代码
  如果无法获取麒麟官方安装包,可以下载debian提供的包,版本为6.2+20201114-2或其他接近版本。可以使用中科大的镜像网站。
3.2.2.2 使用源代码安装

  从gnu官方网站下载最新版本的源代码包ncurses-6.5.tar.gz,放置到安装目录。解压缩后进入解压后的目录,执行configure命令。
  1. kylin@KL5:~/Install$ tar -zxf ncurses-6.5.tar.gz
  2. kylin@KL5:~/Install$ cd ncurses-6.5/
  3. kylin@KL5:~/Install/ncurses-6.5$ ./configure
  4. checking for ggrep... no
  5. checking for grep... grep
  6. checking for egrep... grep -E
  7. Configuring NCURSES 6.5 ABI 6 (Fri Sep 19 10:43:14 CST 2025)
  8. …………
  9. 这里很多提示信息,此处省略。
  10. …………
  11. ** Configuration summary for NCURSES 6.5 20240427:
  12.        extended funcs: yes
  13.        xterm terminfo: xterm-new
  14.         bin directory: /usr/bin
  15.         lib directory: /usr/lib
  16.     include directory: /usr/include
  17.         man directory: /usr/share/man
  18.    terminfo directory: /usr/share/terminfo
复制代码
  继续执行make指令进行编译。
  1. kylin@KL5:~/Install/ncurses-6.5$ make
  2. ( cd man && make DESTDIR="" RPATH_LIST="/usr/lib" all )
  3. make[1]: 进入目录“/home/kylin/Install/ncurses-6.5/man”
  4. /bin/sh ./MKterminfo.sh ./terminfo.head ./../include/Caps ./../include/Caps-ncurses ./terminfo.tail >terminfo.5
  5. …………
  6. 这里很多提示信息,此处省略。
  7. …………
  8. compiling demo (obj_s)
  9. /usr/bin/g++  -o demo ../objects/demo.o -L../lib -lncurses++w -L../lib -lformw -lmenuw -lpanelw -lncursesw   -lutil     -DHAVE_CONFIG_H -DBUILDING_NCURSES_CXX -I../c++ -I. -I../include -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -DNDEBUG -O2  -DNCURSES_STATIC
  10. make[1]: 离开目录“/home/kylin/Install/ncurses-6.5/c++”
复制代码
  编译成功后,用管理员账户执行make install安装。
  1. kylin@KL5:~/Install/ncurses-6.5$ sudo make install
  2. ( cd man && make DESTDIR="" RPATH_LIST="/usr/lib" install )
  3. make[1]: 进入目录“/home/kylin/Install/ncurses-6.5/man”
  4. /bin/sh ../edit_man.sh normal installing /usr/share/man . terminfo.5 *-config.1 ./*.[0-9]* ...made /home/kylin/Install/ncurses-6.5/man_alias.sed
  5. …………
  6. 这里很多提示信息,此处省略。
  7. …………
  8. installing ./cursesapp.h in /usr/include
  9. installing ./cursesf.h in /usr/include
  10. installing ./cursesm.h in /usr/include
  11. installing ./cursesp.h in /usr/include
  12. installing ./cursesw.h in /usr/include
  13. installing ./cursslk.h in /usr/include
  14. installing etip.h in /usr/include
  15. make[1]: 离开目录“/home/kylin/Install/ncurses-6.5/c++”
复制代码
3.2.3 安装OpenSSL

  从OpenSSL官方网站下载指定版本的源代码包openssl-1.1.1w.tar.gz,放置到安装目录。解压缩openssl-OpenSSL_1_1_1w.zip,进入解压后的目录。
  1. kylin@KL5:~/Install$ unzip openssl-OpenSSL_1_1_1w.zip
  2. Archive:  openssl-OpenSSL_1_1_1w.zip
  3. e04bd3433fd84e1861bf258ea37928d9845e6a86
  4.    creating: openssl-OpenSSL_1_1_1w/
  5.   inflating: openssl-OpenSSL_1_1_1w/ACKNOWLEDGEMENTS
  6.   inflating: openssl-OpenSSL_1_1_1w/AUTHORS
  7.   inflating: openssl-OpenSSL_1_1_1w/CHANGES
  8.   inflating: openssl-OpenSSL_1_1_1w/CONTRIBUTING
  9. …………
  10. 这里很多提示信息,此处省略。
  11. …………
  12.   inflating: openssl-OpenSSL_1_1_1w/util/shlib_wrap.sh.in
  13.   inflating: openssl-OpenSSL_1_1_1w/util/su-filter.pl
  14.   inflating: openssl-OpenSSL_1_1_1w/util/unlocal_shlib.com.in
  15.    creating: openssl-OpenSSL_1_1_1w/wycheproof/
  16. kylin@KL5:~/Install$ cd openssl-OpenSSL_1_1_1w/
复制代码
  执行config命令指定安装目录进行配置。
  1. kylin@KL5:~/Install/openssl-OpenSSL_1_1_1w$ ./config --prefix=/usr/local/openssl
  2. Operating system: x86_64-whatever-linux2
  3. Configuring OpenSSL version 1.1.1w (0x1010117fL) for linux-x86_64
  4. Using os-specific seed configuration
  5. Creating configdata.pm
  6. Creating Makefile
  7. **********************************************************************
  8. ***                                                                ***
  9. ***   OpenSSL has been successfully configured                     ***
  10. ***                                                                ***
  11. ***   If you encounter a problem while building, please open an    ***
  12. ***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
  13. ***   and include the output from the following command:           ***
  14. ***                                                                ***
  15. ***       perl configdata.pm --dump                                ***
  16. ***                                                                ***
  17. ***   (If you are new to OpenSSL, you might want to consult the    ***
  18. ***   'Troubleshooting' section in the INSTALL file first)         ***
  19. ***                                                                ***
  20. **********************************************************************
复制代码
  继续执行make指令进行编译。
  1. kylin@KL5:~/Install/openssl-OpenSSL_1_1_1w$ make
  2. /usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
  3.     "-oMakefile" include/crypto/bn_conf.h.in > include/crypto/bn_conf.h
  4. /usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
  5.     "-oMakefile" include/crypto/dso_conf.h.in > include/crypto/dso_conf.h
  6. /usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
  7.     "-oMakefile" include/openssl/opensslconf.h.in > include/openssl/opensslconf.h
  8. make depend && make _all
  9. …………
  10. 这里很多提示信息,此处省略。
  11. …………
  12. /usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
  13.     "-oMakefile" util/shlib_wrap.sh.in > "util/shlib_wrap.sh"
  14. chmod a+x util/shlib_wrap.sh
  15. make[1]: Leaving directory '/home/kylin/Install/openssl-OpenSSL_1_1_1w'
复制代码
  编译成功后,用管理员账户执行make install安装。
  1. kylin@KL5:~/Install/openssl-OpenSSL_1_1_1w$ sudo make install
  2. make depend && make _build_libs
  3. make[1]: Entering directory '/home/kylin/Install/openssl-OpenSSL_1_1_1w'
  4. make[1]: Leaving directory '/home/kylin/Install/openssl-OpenSSL_1_1_1w'
  5. make[1]: Entering directory '/home/kylin/Install/openssl-OpenSSL_1_1_1w'
  6. make[1]: Nothing to be done for '_build_libs'.
  7. make[1]: Leaving directory '/home/kylin/Install/openssl-OpenSSL_1_1_1w'
  8. …………
  9. 这里很多提示信息,此处省略。
  10. …………
  11. /usr/local/openssl/share/doc/openssl/html/man7/SM2.html
  12. /usr/local/openssl/share/doc/openssl/html/man7/ssl.html
  13. /usr/local/openssl/share/doc/openssl/html/man7/X25519.html
  14. /usr/local/openssl/share/doc/openssl/html/man7/X448.html -> /usr/local/openssl/share/doc/openssl/html/man7/X25519.html
  15. /usr/local/openssl/share/doc/openssl/html/man7/x509.html
复制代码
3.3 麒麟V10高级服务器版(GFB)

3.3.1 安装前准备

  银河麒麟V10高级服务器版,允许使用root直接登录,这里安全起见,创建用户kylin并加入wheel组,作为管理员用户使用。
  1. [root@localhost ~]# adduser kylin
  2. [root@localhost ~]# passwd kylin
  3. 更改用户 kylin 的密码 。
  4. 新的 密码:
  5. 重新输入新的 密码:
  6. passwd:所有的身份验证令牌已经成功更新。
  7. [root@localhost ~]# usermod -aG wheel kylin
复制代码
  再使用root用户创建/data共享目录。
  1. [root@localhost ~]# mkdir /data
  2. [root@localhost ~]# chmod a+rwx /data
  3. [root@localhost ~]# chmod -t /data
复制代码
  使用root用户修改主机名,并重新登录。
  1. [root@localhost ~]# hostnamectl set-hostname KL5
  2. [root@localhost ~]# logout
复制代码
  使用kylin用户创建目录Install用于放置所有安装文件。
  1. [kylin@KL5 ~]$ mkdir Install
复制代码
3.3.2 安装ncurses

  在麒麟V10高级服务器版扩展盘中可以找到ncurses-devel的RPM包,其中x86版文件名为“ncurses-devel-6.2-3.gfb03.ky10.x86_64.rpm”,ARM版文件名为“ncurses-devel-6.2-3.gfb03.ky10.aarch64.rpm”。使用管理员账户安装RPM包。
  1. [kylin@KL5 Install]$ sudo rpm -i ncurses-devel-6.2-3.gfb03.ky10.x86_64.rpm
  2. 警告:ncurses-devel-6.2-3.gfb03.ky10.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 34fe765a: NOKEY
复制代码
3.4 安装Erlang

  从Erlang官网网站下载指定版本的源代码包otp_src_26.2.5.15.tar.gz,放置到安装目录。解压缩安装包后进入解压后的目录,执行configure命令。
  1. [kylin@KL5 Install]$ tar -zxf otp_src_26.2.5.15.tar.gz
  2. [kylin@KL5Install]$ cd otp_src_26.2.5.15/
  3. [kylin@KL5 otp_src_26.2.5.15]$ ./configure
  4. === Running configure in /home/kylin/Install/otp_src_26.2.5.15/erts ===
  5. ./configure --disable-option-checking --cache-file=/dev/null --srcdir="/home/kylin/Install/otp_src_26.2.5.15/erts"
  6. checking build system type... x86_64-pc-linux-gnu
  7. checking host system type... x86_64-pc-linux-gnu
  8. checking target system type... x86_64-pc-linux-gnu
  9. …………
  10. 这里很多提示信息,此处省略。
  11. …………
  12. *********************************************************************
复制代码
  在配置Erlang的时候会提示因为缺少ODBC library、GLU headers等,导致odbc、wx等不可用,这个不影响RabbitMQ的一般使用,暂时不用理会。
  继续执行make指令进行编译。
  1. [kylin@KL5 otp_src_26.2.5.15]$ make
  2. MAKE   depend
  3. make[1]: 进入目录“/home/kylin/Install/otp_src_26.2.5.15/erts/emulator”
  4. MAKE   generate
  5. make[2]: 进入目录“/home/kylin/Install/otp_src_26.2.5.15/erts/emulator”
  6. GEN    x86_64-pc-linux-gnu/opt/jit/OPCODES-GENERATED
  7. GEN    x86_64-pc-linux-gnu/opt/jit/beam_asm_global.hpp
  8. …………
  9. 这里很多提示信息,此处省略。
  10. …………
  11. GEN    /home/kylin/Install/otp_src_26.2.5.15/bin/start.script
  12. GEN    /home/kylin/Install/otp_src_26.2.5.15/bin/start_sasl.script
  13. GEN    /home/kylin/Install/otp_src_26.2.5.15/bin/start_clean.script
  14. GEN    /home/kylin/Install/otp_src_26.2.5.15/bin/no_dot_erlang.script
  15. make[2]: 离开目录“/home/kylin/Install/otp_src_26.2.5.15/erts/start_scripts”
  16. make[1]: 离开目录“/home/kylin/Install/otp_src_26.2.5.15/erts”
复制代码
  编译成功后,用管理员账户执行make install安装。
  1. [kylin@KL5 otp_src_26.2.5.15]$ sudo make install
  2. MAKE   erl_interface
  3. make[1]: 进入目录“/home/kylin/Install/otp_src_26.2.5.15/lib/erl_interface”
  4. === Entering application erl_interface
  5. make[2]: 进入目录“/home/kylin/Install/otp_src_26.2.5.15/lib/erl_interface/src”
  6. …………
  7. 这里很多提示信息,此处省略。
  8. …………
  9. ln -s ../lib/erlang/bin/dialyzer dialyzer
  10. ln -s ../lib/erlang/bin/typer typer
  11. ln -s ../lib/erlang/bin/escript escript
  12. ln -s ../lib/erlang/bin/ct_run ct_run
  13. [kylin@KL5 otp_src_26.2.5.15]$
复制代码
  测试Erlang,使用erl命令进入Erlang控制台,使用crypto:start测试OpenSSL,使用halt命令退出。
  1. [kylin@KL5 otp_src_26.2.5.15]$ erl
  2. Erlang/OTP 26 [erts-14.2.5.11] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit:ns]
  3. Eshell V14.2.5.11 (press Ctrl+G to abort, type help(). for help)
  4. 1> crypto:start().
  5. ok
  6. 2> halt().
复制代码
3.5 安装RabbitMQ

  解压缩rabbitmq-server-generic-unix-3.13.7.tar.xz,将解压后的目录移动到指定位置。
  1. [kylin@KL5 Install]$ tar -xf rabbitmq-server-generic-unix-3.13.7.tar.xz
  2. [kylin@KL5 Install]$ mv rabbitmq_server-3.13.7/ /data/rabbitmq
复制代码
  编辑配置文件/etc/profile,将/data/rabbitmq/sbin加入到PATH环境变量中,使用source /etc/profile命令更新环境变量。
  1. [kylin@KL5 Install]$ sudo vi /etc/profile
  2. …………
  3. 其他配置内容。
  4. …………
  5. export PATH=$PATH:/data/rabbitmq/sbin
  6. [kylin@KL5 Install]$ source /etc/profile
复制代码
  使用rabbitmq-server -detached以后台服务方式启动RabbitMQ,使用rabbitmqctl status查看RabbitMQ状态,可以看到RabbitMQ开启了5672端口和25672端口,分别服务于amqp和clustering协议。
  1. [kylin@KL5 ~]$ rabbitmq-server -detached
  2. [kylin@KL5 ~]$ rabbitmqctl status
  3. Status of node rabbit@localhost ...cha
  4. Runtime
  5. OS PID: 122102
  6. OS: Linux
  7. Uptime (seconds): 7
  8. Is under maintenance?: false
  9. RabbitMQ version: 3.13.7
  10. …………
  11. 此处有很多状态信息,此处忽略。
  12. …………
  13. Listeners
  14. Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
  15. Interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
复制代码
  使用rabbitmqctl start_app和rabbitmq-plugins enable rabbitmq_management命令开启管理控制台,再使用rabbitmqctl status查看RabbitMQ状态,可以看到15672端口已经打开,此时可以使用浏览器管理消息队列。
  1. [kylin@KL5 ~]$ rabbitmqctl start_app
  2. rabbitmqctl start_app
  3. Starting node rabbit@localhost ...
  4. [kylin@KL5 ~]$ rabbitmq-plugins enable rabbitmq_management
  5. rabbitmq-plugins enable rabbitmq_management
  6. Enabling plugins on node rabbit@localhost:
  7. rabbitmq_management
  8. The following plugins have been configured:
  9.   rabbitmq_management
  10.   rabbitmq_management_agent
  11.   rabbitmq_web_dispatch
  12. Applying plugin configuration to rabbit@localhost...
  13. The following plugins have been enabled:
  14.   rabbitmq_management
  15.   rabbitmq_management_agent
  16.   rabbitmq_web_dispatch
  17. started 3 plugins.
  18. [kylin@KL5 ~]$ rabbitmqctl status
  19. Status of node rabbit@localhost ...
  20. Runtime
  21. …………
  22. 此处有很多状态信息,此处忽略。
  23. …………
  24. Listeners
  25. Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
  26. Interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
  27. Interface: [::], port: 15672, protocol: http, purpose: HTTP API
复制代码
3.6 注册RabbitMQ为服务

  使用管理员用户编辑/etc/systemd/system/rabbitmq.service文件,在文件中加入如下内容。
  1. [kylin@KL5 ~]$ sudo vi /etc/systemd/system/rabbitmq.service
  2. [Unit]
  3. Description = RabbitMQ Server
  4. After = network.target
  5. [Service]
  6. Type = simple
  7. User = kylin
  8. Group = kylin
  9. ExecStart = /data/rabbitmq/sbin/rabbitmq-server
  10. ExecStop = /data/rabbitmq/sbin/rabbitmqctl stop
  11. Restart = on-failure
  12. RestartSec = 60s
  13. LimitNOFILE = 65536
  14. [Install]
  15. WantedBy = multi-user.target
复制代码
  重新加载服务,将服务设置为开机启动。
  1. [kylin@KL5 system]$ sudo systemctl daemon-reload
  2. [kylin@KL5 system]$ sudo systemctl enable rabbitmq
  3. Created symlink /etc/systemd/system/multi-user.target.wants/rabbitmq.service → /etc/systemd/system/rabbitmq.service.
复制代码
  启动服务并查看服务状态。如果之前RabbitMQ已启动,先结束进程。
  1. [kylin@KL5 ~]$ ps -AL|grep beam
  2.    2212    2212 ?        00:00:00 beam.smp
  3. [kylin@KL5 ~]$ kill 2212
  4. [kylin@KL5 ~]$ sudo systemctl start rabbitmq
  5. [kylin@KL5 ~]$ sudo systemctl status rabbitmq
  6. ● rabbitmq.service - RabbitMQ Server
  7.    Loaded: loaded (/etc/systemd/system/rabbitmq.service; enabled; vendor preset: disabled)
  8.    Active: active (running) since Wed 2025-09-24 10:24:47 CST; 2s ago
  9. Main PID: 1012826 (rabbitmq-server)
  10.     Tasks: 26
  11.    Memory: 124.6M
  12.    CGroup: /system.slice/rabbitmq.service
  13.            ├─1012826 /bin/sh /data/rabbitmq/sbin/rabbitmq-server
  14.            ├─1012833 /usr/local/lib/erlang/erts-14.2.5.11/bin/beam.smp -W w -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -pc unicode -P 1048576 -t 5000000 -stbt db -zdbbl >
  15.            ├─1012839 erl_child_setup 65536
  16.            ├─1012858 sh -s disksup
  17.            ├─1012860 /usr/local/lib/erlang/lib/os_mon-2.9.1/priv/bin/memsup
  18.            ├─1012861 /usr/local/lib/erlang/lib/os_mon-2.9.1/priv/bin/cpu_sup
  19.            ├─1012862 /usr/local/lib/erlang/erts-14.2.5.11/bin/inet_gethost 4
  20.            ├─1012863 /usr/local/lib/erlang/erts-14.2.5.11/bin/inet_gethost 4
  21.            └─1012916 /bin/sh -s rabbit_disk_monitor
复制代码
4 配置RabbitMQ集群

4.1 修改hosts文件

  修改/etc/hosts文件,将集群涉及的主机名称添加到hosts文件中。
  1. kylin@KL5:~$ sudo vi /etc/hosts
  2. 127.0.0.1       localhost
  3. 10.10.1.45      KL5
  4. 10.10.1.46      KL6
复制代码
4.2 开放防火墙端口

  麒麟高级服务器版默认开启了防火墙,需要开放RabbitMQ相关的端口,包括4369(EPMD)、5671-5672(AMQP)、15672(管理页面)、25672(节点间通讯)、35672-35682(CLI工具)等,以上端口均为TCP协议。
  1. [kylin@KL5 ~]$ sudo firewall-cmd --add-port=4369/tcp --permanent
  2. success
  3. [kylin@KL5 ~]$ sudo firewall-cmd --add-port=5671-5672/tcp --permanent
  4. success
  5. [kylin@KL5 ~]$ sudo firewall-cmd --add-port=15672/tcp --permanent
  6. success
  7. [kylin@KL5 ~]$ sudo firewall-cmd --add-port=25672/tcp --permanent
  8. success
  9. [kylin@KL5 ~]$ sudo firewall-cmd --add-port=35672-35682/tcp --permanent
  10. success
  11. [kylin@KL5 ~]$ sudo firewall-cmd --reload
  12. success
  13. [kylin@KL5 ~]$ sudo firewall-cmd --list-ports
  14. 4369/tcp 15672/tcp 25672/tcp 35672-35682/tcp 5671-5672/tcp
复制代码
4.3 复制cookie文件

  将.erlang.cookie从主机服务器复制到其他服务器相同目录。本例中.erlang.cookie文件在kylin用户目录下。如果提示没有权限,需要先修改从机上的文件权限。
  1. kylin@KL5:~$ scp .erlang.cookie kylin@KL6:/home/kylin
  2. kylin@KL6's password:
  3. .erlang.cookie                                              100%   20     7.8KB/s   00:00
复制代码
  修改.erlang.cookie文件之前如果已经用rabbitmq-server启动了消息队列,在修改.erlang.cookie文件会无法使用rabbitmqctl控制,需要结束进程,rabbitmq的进程名称为beam.smp。
  1. [kylin@KL5 ~]$ ps -AL|grep beam.smp
  2.   33029   33029 ?        00:00:00 beam.smp
  3. [kylin@KL5 ~]$ kill 33029
复制代码
4.4 将从机加入集群

  分别启动主机和从机的服务后,在从机上使用rabbitmqctl stop_app指令停止应用,使用rabbitmqctl join_cluster指令将从机加入主机集群,再使用rabbitmqctl start_app指令启动应用。此时可以使用rabbitmqctl cluster_status指令查看集群状态。
  1. kylin@KL6:~$ rabbitmqctl stop_app
  2. Stopping rabbit application on node rabbit@KL6 ...
  3. kylin@KL6:~$ rabbitmqctl join_cluster rabbit@KL5
  4. Clustering node rabbit@KL6 with rabbit@KL5
  5. kylin@KL6:~$ rabbitmqctl start_app
  6. Starting node rabbit@KL6 ...
  7. kylin@KL6:~$ rabbitmqctl cluster_status
  8. Cluster status of node rabbit@KL6 ...
  9. Basics
  10. Cluster name: rabbit@KL6
  11. Total CPU cores available cluster-wide: 8
  12. Cluster Tags
  13. (none)
  14. Disk Nodes
  15. rabbit@KL5
  16. rabbit@KL6
  17. Running Nodes
  18. rabbit@KL5
  19. rabbit@KL6
  20. Versions
  21. rabbit@KL6: RabbitMQ 3.13.7 on Erlang 26.2.5.15
  22. rabbit@KL5: RabbitMQ 3.13.7 on Erlang 26.2.5.15
  23. CPU Cores
  24. Node: rabbit@KL6, available CPU cores: 4
  25. Node: rabbit@KL5, available CPU cores: 4
  26. Maintenance status
  27. Node: rabbit@KL6, status: not under maintenance
  28. Node: rabbit@KL5, status: not under maintenance
  29. …………
  30. 此处有很多状态信息,此处忽略。
  31. …………
复制代码
  此时,集群已经建立,但模式为普通模式,下一步需要将队列配置为镜像模式。配置可以采用管理页面方式,也可以使用命令行方式配置。
4.5 配置镜像策略

4.5.1 页面方式配置镜像

  使用浏览器打开主机的配置页面,使用默认用户guest/guest登录,创建需要的用户、虚拟主机、交换机、消息队列等,在创建消息队列时选择classic类型。
  使用有管理员权限用户登录,在Admin->olicies->User Policies管理项下,增加管理策略。Pattern填写“^”,Apply to选择“Exchanges and queues”,Definition定义“ha-mode=all”、“ha-sync-mode=automatic”。
1.png

4.5.2 命令方式配置镜像

  使用rabbitmqctl set_policy命令设置复制策略。
  1. [kylin@KL5 ~]$ rabbitmqctl set_policy jxjq "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}' -p /test/
  2. Setting policy "jxjq" for pattern "^" to "{"ha-mode":"all","ha-sync-mode":"automatic"}" with priority "0" for vhost "/test/" ...
复制代码
  其中“jxjq”为策略名称,“^”为匹配字符,“{"ha-mode":"all","ha-sync-mode":"automatic"}'”为JSON格式的策略定义,“-p /test/”表示应用的Virtual host。
5 集群切换测试

  集群启动-后,连接管理页面可以查看集群状态、队列状态,此时队列显示主节点为KL5,KL5为集群主机,KL6为集群从机。
2.png

  在正常状态下,测试向消息队列faultQueue发送了6条数据。
3.png

  通过暂停虚拟机的方式,将KL5主机暂停。在KL5主机暂停的最初十几秒内,从机上的消息队列暂时不可连接,管理页面也没有响应,应该在切换集群主从。之后可正常连接从机上的消息队列,收发消息,管理页面可以看到KL5节点变成停止状态,消息队列显示主节点为KL6,KL6为集群主机。
4.png

  在KL5故障状态下,向消息队列faultQueue又发送了8条数据,现在KL6显示共有14条数据。
5.png

  通过查看运行日志,可以看到节点KL6已经发现KL5无法连接了。
  1. [kylin@KL6 ~]$ tail -f /data/rabbitmq/var/log/rabbitmq/rabbit@KL6.log
  2. 2025-09-24 14:08:59.801496+08:00 [error] <0.2019.0> ** Node rabbit@KL5 not responding **
  3. 2025-09-24 14:08:59.801496+08:00 [error] <0.2019.0> ** Removing (timedout) connection **
  4. 2025-09-24 14:08:59.801496+08:00 [error] <0.2019.0>
  5. 2025-09-24 14:08:59.802145+08:00 [info] <0.5951.0> rabbit on node rabbit@KL5 down
  6. 2025-09-24 14:09:02.910433+08:00 [warning] <0.6301.0> Management delegate query returned errors:
  7. 2025-09-24 14:09:02.910433+08:00 [warning] <0.6301.0> [{<30547.2322.0>,{exit,{nodedown,rabbit@KL5},[]}}]
  8. 2025-09-24 14:09:02.910772+08:00 [warning] <0.6302.0> Management delegate query returned errors:
  9. 2025-09-24 14:09:02.910772+08:00 [warning] <0.6302.0> [{<30547.2322.0>,{exit,{nodedown,rabbit@KL5},[]}}]
  10. 2025-09-24 14:09:02.911090+08:00 [info] <0.5951.0> node rabbit@KL5 down: net_tick_timeout
复制代码
  使用命令行方式,也可以看到集群节点包括KL5和KL6,但目前只有KL6运行。
  1. [kylin@KL6 ~]$ rabbitmqctl cluster_status
  2. Cluster status of node rabbit@KL6 ...
  3. Basics
  4. Cluster name: rabbit@KL6
  5. Total CPU cores available cluster-wide: 1
  6. Disk Nodes
  7. rabbit@KL5
  8. rabbit@KL6
  9. Running Nodes
  10. rabbit@KL6
  11. …………
  12. 这里很多状态信息,此处省略。
  13. …………
复制代码
  此时恢复虚拟机,将KL5主机恢复并接入。正常情况下集群恢复,但集群主节点仍然是KL6。
6.png

7.png

  通过查看KL6的日志,可以看到集群已经发现KL5苏醒,并选举KL6继续做主节点,并等待KL5重新加入集群。
  1. 2025-09-24 14:39:09.201846+08:00 [info] <0.13111.0> node rabbit@KL5 up
  2. 2025-09-24 14:39:09.220700+08:00 [info] <0.13111.0> Autoheal: I am the winner, waiting for [rabbit@KL5] to stop
  3. 2025-09-24 14:40:10.813167+08:00 [info] <0.13111.0> rabbit on node rabbit@KL5 up
  4. 2025-09-24 14:40:10.816809+08:00 [info] <0.13247.0> Mirrored queue 'dataQueue' in vhost '/test/': Synchronising: 0 messages to synchronise
复制代码
  通过查看KL5的日志,也可以看到KL5选举失败,重新启动并加入了集群。
  1. 2025-09-24 14:39:09.541787+08:00 [info] <0.2163.0> Successfully stopped RabbitMQ and its dependencies
  2. 2025-09-24 14:39:19.222195+08:00 [warning] <0.2278.0> Mnesia->Khepri fallback handling: Mnesia function failed 99 times. Possibly an infinite retry loop; trying one last time
  3. 2025-09-24 14:40:09.542739+08:00 [warning] <0.2163.0> Autoheal: timed out waiting for a safe-to-start message from the winner (rabbit@KL6); will retry
  4. 2025-09-24 14:40:09.542924+08:00 [info] <0.2163.0> RabbitMQ is asked to start...
  5. …………
  6. 这里很多状态信息,此处省略。
  7. …………
  8. 2025-09-24 14:40:10.576539+08:00 [notice] <0.2398.0> Feature flags: checking nodes `rabbit@KL5` and `rabbit@KL6` compatibility...
  9. 2025-09-24 14:40:10.579022+08:00 [notice] <0.2398.0> Feature flags: nodes `rabbit@KL5` and `rabbit@KL6` are compatible
  10. 2025-09-24 14:40:10.582058+08:00 [notice] <0.44.0> Application mnesia exited with reason: stopped
  11. 2025-09-24 14:40:10.606314+08:00 [error] <0.2466.0> Mnesia(rabbit@KL5): ** ERROR ** mnesia_event got {inconsistent_database, starting_partitioned_network, rabbit@KL6}
  12. 2025-09-24 14:40:10.606314+08:00 [error] <0.2466.0>
复制代码
  有时可以在管理页面看到KL5集群节点并没有恢复,而是显示出现网络分区。同时消息队列中消息数量会变成主机、从机原本各自消息数量之和,如某队列集群故障前有消息12条,故障时收到消息6条,此时会显示18+12=30条。
8.png

  使用命令行方式查看集群状态,也是相同的信息。
  1. [kylin@KL6 ~]$ rabbitmqctl cluster_status
  2. rabbitmqctl cluster_status
  3. Cluster status of node rabbit@KL6 ...
  4. Basics
  5. Cluster name: rabbit@KL6
  6. Total CPU cores available cluster-wide: 2
  7. Disk Nodes
  8. rabbit@KL5
  9. rabbit@KL6
  10. Running Nodes
  11. rabbit@KL6
  12. …………
  13. 这里很多状态信息,此处省略。
  14. …………
  15. Network Partitions
  16. Node rabbit@KL6 cannot communicate with rabbit@KL5
  17. …………
  18. 这里很多状态信息,此处省略。
  19. …………
复制代码
  此时两台主机均认为自己应该是集群主机,因此形成了两个集群网络,此时需要人工干预。一般此时应将KL5降为从机,保持KL6为主机。可以在KL5上使用rabbitmqctl stop_app、rabbitmqctl start_app重启动消息队列服务的方式完成。之后集群状态恢复,消息队列中消息数量恢复。
9.png

10.png

  因此可以看到,在集群节点只有两台时,单节点故障后可以集群可以正常使用,但故障消除后,有些情况可以自动恢复,有些情况不能自动恢复。一般如果是主机间网络通讯故障造成的,一般会出现网络分区,不能自动恢复。如果是某台主机服务停止,通过重启动可以自动恢复。对于第一种情况,可以在rabbitmq.conf文件中配置cluster_partition_handling = autoheal来解决。
  1. [kylin@KL7 ~]$ vi /data/rabbitmq/etc/rabbitmq/rabbitmq.conf
  2. cluster_partition_handling = autoheal
复制代码
6 参考文献

  https://www.rabbitmq.com/docs/which-erlang
  https://www.rabbitmq.com/docs/3.13
  https://rabbitmq.cn/docs/
  https://developer.aliyun.com/article/1477942
  https://github.com/rabbitmq/rabbitmq-server/blob/v3.8.9/docs/rabbitmq.conf.example

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

昨天 13:47

举报

前排留名,哈哈哈
您需要登录后才可以回帖 登录 | 立即注册