从0到1在Linux上搭建RabbitMq集群教程

搭建环境阿里云CenterOS8.2,Erlang23.x,RabbitMq3.8.x

1、准备三台阿里云服务器,情况如下

 

2、鉴于后续端口通信等操作需要,提前做好前置工作

1)阿里云安全组内开通15672端口,便于远程管理rabbitmq集群

2)配置三台主机的hosts文件,因为rabbitmq是通过主机名称识别的,命令如下

vi /etc/hosts
172.16.213.251  iZbp145wkv4vf3oba7qge2Z iZbp145wkv4vf3oba7qge2Z
172.16.213.250  iZbp145wkv4vf3oba7qge1Z iZbp145wkv4vf3oba7qge1Z
172.16.213.246  iZbp145wkv4vf3oba7qge0Z iZbp145wkv4vf3oba7qge0Z
#三台机器分别做如上hosts文件修改
#OK之后可以互ping一下,例如
ping iZbp145wkv4vf3oba7qge0Z
#如果通代表ok

3、开始分别在三台机器上安装rabbitmq,整体步骤比较简单,按步骤执行如下命令即可

cd /usr/local
mkdir erlang
cd erlang
##########
#安装erlang
rpm -ivh 'download_file?file_path=erlang%2F23%2Fel%2F8%2Fx86_64%2Ferlang-23.2.3-1.el8.x86_64.rpm'
#设置镜像仓库
wget --no-cache http://www.convirture.com/repos/definitions/rhel/6.x/convirt.repo -O /etc/yum.repos.d/convirt.repo
#安装socat
yum install socat
#下载rabbitmq最新版,如果版本不是最新,可以到官网找最新https://github.com/rabbitmq/
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.11/rabbitmq-server-3.8.11-1.el7.noarch.rpm
#导入证书
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
#执行安装
rpm -ivh rabbitmq-server-3.8.11-1.el7.noarch.rpm
  [/usr/lib/tmpfiles.d/rabbitmq-server.conf:1] Line references path below legacy directory /var/run/, updating /var/run/rabbitmq → /run/rabbitmq; please update the tmpfiles.d/ drop-in file accordingly.
#设置开机启动
systemctl enable rabbitmq-server.service
#启动服务
systemctl start rabbitmq-server.service
#停止服务
systemctl stop rabbitmq-server.service
#安装远程访问插件,否则集群后看不到状态情况
rabbitmq-plugins enable rabbitmq_management
#至此,可以通过http://ip:15672查看远程管理界面了,但是没办法登录,guest只能localhost访问
#因为要构建集群,所以只要在s1主服务器创建账户即可

#如下命令在s1执行
rabbitmqctl add_user admin yourpwd
rabbitmqctl set_user_tags admin administrator
    Setting tags for user "admin" to [administrator] ...
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
#这样可以通过admin登录管理了

4、至此,已经在三台服务器成功安装了rabbitmq,并且可以通过远端web进行访问管理了

5、开始构建集群环境

#从s1服务器上复制.erlang.cookie文件到s2,s3,当然也可以直接修改.erlang.cookie文件内容
scp /var/lib/rabbitmq/.erlang.cookie root@172.16.213.250:/var/lib/rabbitmq/
scp /var/lib/rabbitmq/.erlang.cookie root@172.16.213.246:/var/lib/rabbitmq/


#在三台服务器上,分别设置.erlang.cookie的权限
chown rabbitmq:root /var/lib/rabbitmq/.erlang.cookie
chmod 600 /var/lib/rabbitmq/.erlang.cookie

6、开始编组,分别登陆s2,s3服务器,执行如下命令,将s2,s3编组到s1中

rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@iZbp145wkv4vf3oba7qge2Z #s1,默认是disk节点,加参数
--ram代表是内存节点

rabbitmqctl start_app

7、到此已经完成了编组,但目前的集群模式还是普通模式,也就是所谓的从元数据模式,实现不了高可用目的;下面执行命令配置为镜像模式;

rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
    Setting policy "ha-all" for pattern "^" to "{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}" with priority "0"
#上面命令在3台服务器上都要执行

 

 

8、更多命令操作

# 添加账号:
rabbitmqctl add_user admin admin
# 添加 权限tag
rabbitmqctl set_user_tags admin administrator
 
# 删除用户(删除guest用户)
rabbitmqctl delete_user guest
Deleting user "guest"
 
# 修改用户的密码
rabbitmqctl  change_password  Username  Newpassword
 
[ root@aliyun_us_b_mq_m~]# rabbitmqctl  change_password  admin 0GM1aol4z8GeSZY99
Changing password for user "admin"
 
# 查看当前用户列表
rabbitmqctl  list_users
Listing users
admin   [administrator]

#修改节点类型disk还是ram
rabbitmqctl stop_app
rabbitmqctl change_cluster_node_type disc
rabbitmqctl start_app

#状态查看
rabbitmqctl cluster_status

#卸载
yum list|grep rabbitmq

yum -y remove rabbitmq-server.noarch

 

9、使用集群

 1  var connectionFactory = new ConnectionFactory()
 2             {
 3                 //HostName = "192.168.205.128", 集群不在此处声明
 4                 UserName = "guest",
 5                 Password = "guest",
 6                 AutomaticRecoveryEnabled = true,//如果connection挂掉是否重新连接
 7                 TopologyRecoveryEnabled = true//连接恢复后,连接的交换机,队列等是否一同恢复
 8             };  //创建连接工厂
 9             var connection = connectionFactory.CreateConnection(new string[] {
10                 "192.168.23.149",
11                 "192.168.23.144",
12                 "192.168.23.143"
13             });  //创建connection
14             var channel = connection.CreateModel(); //创建channel

 

 

参考文章:

https://www.linuxidc.com/Linux/2017-10/147707p2.htm

https://www.linuxidc.com/Linux/2016-12/137812.htm

https://www.rabbitmq.com/clustering.html

https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.11/rabbitmq-server-3.8.11-1.el7.noarch.rpm

https://www.cnblogs.com/reblue520/p/13231431.html

https://www.jianshu.com/p/88765d6dc8f2

https://my.oschina.net/devopsxin/blog/1924462

posted @ 2021-02-03 20:15  jackchain  阅读(681)  评论(0编辑  收藏  举报