1.Docker 搭建RabbitMQ单机
#management是带管理页的
docker pull rabbitmq:3.8.5-management
#5672:应用访问端口;15672:控制台Web端口号
#RabbitMQ的一个重要注意事项是它根据所谓的 “节点名称” 存储数据,默认为主机名
#RABBITMQ_DEFAULT_VHOST:默认虚拟机名;RABBITMQ_DEFAULT_USER:默认的用户名;RABBITMQ_DEFAULT_PASS:默认用户名的密码
mkdir -p /Users/sherlock/dev/rabbitmq/
docker run -id --name=rabbitmq-3.8.5 -p 15672:15672 -p 5672:5672 \
--hostname myRabbitmq \
-v /Users/sherlock/dev/rabbitmq:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
rabbitmq:3.8.5-management
#访问 http://localhost:15672
#进入后台新增用户 reader/xxx tag选择administrator
#点击新用户名,设置新用户可以管理虚拟主机/,这里点击默认的set permission、Set topic permission即可。
2.Docker 搭建RabbitMQ普通集群
mkdir -p /Users/sherlock/dev/rabbitmqcluster/rabbitmq01
mkdir -p /Users/sherlock/dev/rabbitmqcluster/rabbitmq02
mkdir -p /Users/sherlock/dev/rabbitmqcluster/rabbitmq03
#--hostname 设置容器的主机名RABBITMQ_ERLANG_COOKIE 节点认证作用,部署集成时 需要同步该值
docker run -d --hostname rabbitmq01 --name rabbitmqCluster01 \
-v /Users/sherlock/dev/rabbitmqcluster/rabbitmq01:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' \
rabbitmq:3.8.5-management
docker run -d --hostname rabbitmq02 --name rabbitmqCluster02 \
-v /Users/sherlock/dev/rabbitmqcluster/rabbitmq02:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' \
--link rabbitmqCluster01:rabbitmq01 rabbitmq:3.8.5-management
docker run -d --hostname rabbitmq03 --name rabbitmqCluster03 \
-v /Users/sherlock/dev/rabbitmqcluster/rabbitmq03:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' \
--link rabbitmqCluster01:rabbitmq01 --link rabbitmqCluster02:rabbitmq02 rabbitmq:3.8.5-management
2.1 普通集群配置
先进入第一个容器操作 docker exec -it rabbitmqCluster01 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit
再进入第二个容器操作 docker exec -it rabbitmqCluster02 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app
exit
进入第三个容器操作 docker exec -it rabbitmqCluster03 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app
exit
检测是否配置成功:http://xxx:15672/#/ overview面板中的Nodes信息,可查看到节点信息
账号/密码:guest/guest
3.Docker 搭建RabbitMQ镜像集群
mkdir -p /Users/sherlock/dev/rabbitmqMirrorCluster/rabbitmq01
mkdir -p /Users/sherlock/dev/rabbitmqMirrorCluster/rabbitmq02
mkdir -p /Users/sherlock/dev/rabbitmqMirrorCluster/rabbitmq03
#--hostname 设置容器的主机名RABBITMQ_ERLANG_COOKIE 节点认证作用,部署集成时 需要同步该值
docker run -d --hostname rabbitmq01 --name rabbitmqMirrorCluster01 \
-v /Users/sherlock/dev/rabbitmqMirrorCluster/rabbitmq01:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' \
rabbitmq:3.8.5-management
docker run -d --hostname rabbitmq02 --name rabbitmqMirrorCluster02 \
-v /Users/sherlock/dev/rabbitmqMirrorCluster/rabbitmq02:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' \
--link rabbitmqMirrorCluster01:rabbitmq01 rabbitmq:3.8.5-management
docker run -d --hostname rabbitmq03 --name rabbitmqMirrorCluster03 \
-v /Users/sherlock/dev/rabbitmqMirrorCluster/rabbitmq03:/var/lib/rabbitmq \
-e RABBITMQ_DEFAULT_USER=admin \
-e RABBITMQ_DEFAULT_PASS=admin \
-p 15675:15672 -p 5675:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' \
--link rabbitmqMirrorCluster01:rabbitmq01 --link rabbitmqMirrorCluster02:rabbitmq02 rabbitmq:3.8.5-management
3.1 镜像集群配置
先进入第一个容器操作 docker exec -it rabbitmqMirrorCluster01 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
exit
再进入第二个容器操作 docker exec -it rabbitmqMirrorCluster02 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app
exit
进入第三个容器操作 docker exec -it rabbitmqMirrorCluster03 bash
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl join_cluster --ram rabbit@rabbitmq01
rabbitmqctl start_app
exit
以上步骤基本和普通容器没区别
3.2 镜像集群策略配置
进入第一个容器操作 docker exec -it rabbitmqMirrorCluster01 bash
#在vhost名称为/创建了一个策略,策略名称为ha-all,策略模式为 all 即复制到所有节点,包含新增节点,策略正则表达式为 “^” 表示所有匹配所有队列名称
rabbitmqctl set_policy -p / ha-all "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
#为用户赋权,用户admin具有/ 这个virtual host中所有资源的配置、写、读权限,以便管理其中的资源
rabbitmqctl set_permissions -p / admin '.*' '.*' '.*'
检测是否配置成功:http://xxx:15673/#/ overview面板中的Nodes信息,可查看到节点信息
账号/密码:guest/guest
为了dev开发调试,新增用户 reader/xxx tag=administrator,并授权 Set permission,Set topic permission。
3.3 负载均衡设置
#Nginx
http {
# ...
#rabbitmq管理界面
upstream rabbitManage {
server 192.168.0.8:15673;
server 192.168.0.8:15674;
server 192.168.0.8:15675;
}
server {
listen 15672;
server_name 192.168.0.8;
location / {
proxy_pass http://rabbitManage;
index index.html index.htm;
}
}
}
# rabbitmq通信
stream {
upstream rabbitTcp{
server 192.168.0.8:5673;
server 192.168.0.8:5674;
server 192.168.0.8:5675;
}
server {
listen 5672;
proxy_pass rabbitTcp;
}
}
参考
评论区