rabbitMq集群搭建及nginx负载均衡

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程
我们搭建一个三台rabbitMq组成的集群
拉取镜像

docker pull rabbitmq:3.7-management

创建目录,给挂载卷做好准备

mkdir rabbitmqcluster
cd rabbitmqcluster/
mkdir rabbitmq01 rabbitmq02 rabbitmq03

启动节点

docker run -d --hostname rabbitmq01 --name rabbitmqCluster01 -v /home/soft/rabbitmqcluster/rabbitmq01:/var/lib/rabbitmq -p 15672:15672 -p 5672:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie' rabbitmq:3.7-management

docker run -d --hostname rabbitmq02 --name rabbitmqCluster02 -v /home/soft/rabbitmqcluster/rabbitmq02:/var/lib/rabbitmq -p 15673:15672 -p 5673:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie'  --link rabbitmqCluster01:rabbitmq01 rabbitmq:3.7-management

docker run -d --hostname rabbitmq03 --name rabbitmqCluster03 -v /home/soft/rabbitmqcluster/rabbitmq03:/var/lib/rabbitmq -p 15674:15672 -p 5674:5672 -e RABBITMQ_ERLANG_COOKIE='rabbitmqCookie'  --link rabbitmqCluster01:rabbitmq01 --link rabbitmqCluster02:rabbitmq02  rabbitmq:3.7-management

设置集群,将容器节点加入集群

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

查看node信息,可以看到3台
rabbitMq集群搭建及nginx负载均衡_第1张图片

如上rabbitMq的集群环境已搭建完毕,以下使用nginx做负载均衡
配置文件nginx_rabbitmq.conf,保存位置/root/rabbitmqcluster,用于启动nginx容器准备

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
	
	proxy_redirect          off;
	proxy_set_header        Host $host;
	proxy_set_header        X-Real-IP $remote_addr;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	client_max_body_size    10m;
	client_body_buffer_size   128k;
	proxy_connect_timeout   5s;
	proxy_send_timeout      5s;
	proxy_read_timeout      5s;
	proxy_buffer_size        4k;
	proxy_buffers           4 32k;
	proxy_busy_buffers_size  64k;
	proxy_temp_file_write_size 64k;
	#rabbitmq管理界面
	upstream rabbitManage {
		server 192.168.37.101:15672;
		server 192.168.37.101:15673;
		server 192.168.37.101:15674;
	}
	server {
        listen       15675;
        server_name  192.168.37.101; 
        location / {  
            proxy_pass   http://rabbitManage;
            index  index.html index.htm;  
        }  

    }
}
# rabbitmq通信
stream{
	upstream rabbitTcp{
        server 192.168.37.101:5672;
        server 192.168.37.101:5673;
	server 192.168.37.101:5674;
    }

    server {
        listen 5675;
        proxy_pass rabbitTcp;
    }
}

启动nginx

docker run -it -d --name nginxRabbitmq -v /root/rabbitmqcluster/nginx_rabbitmq.conf:/etc/nginx/nginx.conf  --privileged --net=host nginx

接着读者可以通过 http://192.168.37.101:15675 进行管理 以及通过 5675 端口 进行rabbitmq通信。
rabbitMq集群搭建及nginx负载均衡_第2张图片

另外一种方式,使用镜像,并使用ha_proxy,使用虚拟ip的方式,参考博客https://blog.csdn.net/belonghuang157405/article/details/83540148

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程

你可能感兴趣的:(集群,搭建rabbitMq集群)