RabbiMQ是用Erang开发的,集群非常方便,因为Erlang天生就是⼀门分布式语言,但其本身并不支持负载均衡。支持高并发,支持可扩展。支持AJAX,持久化,用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面表现不俗。
RabbiMQ模式分为三种:1.单一模式 2.普通模式 3.镜像模式(先搭建一个普通集群模式,在这个模式的基础上再配置镜像模式实现高可用)
192.168.203.158 rabbitmq-1
192.168.203.157 rabbitmq-2
192.168.203.137 rabbitmq-3
[root@rabbitmq-1 rabbitmq-test]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.203.158 rabbitmq-1
192.168.203.157 rabbitmq-2
192.168.203.137 rabbitmq-3
安装依赖
[root@rabbitmq-1 ~]# yum install -y epel-release gcc-c++ unixODBC unixODBC-devel openssl-devel ncurses-devel
yum安装erlang
[root@rabbitmq-1 ~]# curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
[root@rabbitmq-1 ~]# yum install erlang-21.3.8.21-1.el7.x86_64
测试;
[root@rabbitmq-1 ~]# erl
安装rabbitmq
[root@rabbitmq-1 ~]# yum -y install rabbitmq-server-3.7.10-1.el7.noarch.rpm
启动
[root@rabbitmq-1 ~]# systemctl daemon-reload
[root@rabbitmq-1 ~]# systemctl start rabbitmq-server
[root@rabbitmq-1 ~]# systemctl enable rabbitmq-server
每台都操作开启rabbitmq的web访问界面:
[root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management
添加用户和密码
[root@rabbitmq-1 ~]# rabbitmqctl add_user mm 123456
添加为管理员
[root@rabbitmq-1 ~]# rabbitmqctl set_user_tags mm administrator
查看用户
[root@rabbitmq-1 ~]# rabbitmqctl list_users
此处设置权限时注意'.*'之间需要有空格 三个'.*'分别代表了conf权限,read权限与write权限全给
[root@rabbitmq-1 ~]# rabbitmqctl set_permissions -p "/" newrain ".*" ".*" ".*"
root@rabbitmq-1 ~]# cd /etc/rabbitmq/
[root@rabbitmq-1 rabbitmq]# cp /usr/share/doc/rabbitmq-server-3.7.10/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
[root@rabbitmq-1 rabbitmq]# ls
enabled_plugins rabbitmq.config
[root@rabbitmq-1 rabbitmq]# vim rabbitmq.config
rabbitmq默认管理员用户:guest 密码:guest
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/data
[root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/logs
[root@rabbitmq-1 ~]# chmod 777 -R /data/rabbitmq
[root@rabbitmq-1 ~]# chown rabbitmq.rabbitmq /data/ -R
创建配置文件:
[root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq-env.conf
[root@rabbitmq-1 ~]# cat /etc/rabbitmq/rabbitmq-env.conf
RABBITMQ_MNESIA_BASE=/data/rabbitmq/data
RABBITMQ_LOG_BASE=/data/rabbitmq/logs
重启服务
[root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
Rabbitmq的集群是依附于erlang的集群来⼯作的,所以必须先构建起erlang的集群景象。各节点是通过magic cooki来实现,所以必须保持每个节点的cooki一致,不然无法通信
[root@rabbitmq-1 ~]# cat /var/lib/rabbitmq/.erlang.cookie #查看1的cookie
将1的cooki拷贝给2,3
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie [email protected]:/var/lib/rabbitmq/
[root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie [email protected]:/var/lib/rabbitmq/
[root@rabbitmq-2 ~]# systemctl restart rabbitmq-server
[root@rabbitmq-2 ~]# rabbitmqctl stop_app #停止节点
[root@rabbitmq-2 ~]# rabbitmqctl reset #如果有数据需要重置,没有则不用
[root@rabbitmq-2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 #添加到磁盘节点
Clustering node 'rabbit@rabbitmq-2' with 'rabbit@rabbitmq-1' ...
[root@rabbitmq-2 ~]# rabbitmqctl start_app #启动节点
Starting node 'rabbit@rabbitmq-2' ...
[root@rabbitmq-1 ~]# git clone https://gitea.beyourself.org.cn/newrain001/rabbitmq-test.git && \
cd rabbitmq-test && yum install -y python3 python3-devel && \
pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple && \
export FLASK_ENV=development ; flask run --reload -p 80 -h 0.0.0.0
web进入192.168.203.158:15672和192.168.203.158查看
创建镜像集群
[root@rabbitmq-1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
[root@rabbitmq-2 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
[root@rabbitmq-3 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
再进入web端查看并操作
1.安装
[root@rabbitmq-1 ~]# yum -y install haproxy
2.修改/etc/haproxy/haproxy.cfg
[root@rabbitmq-1 ~]# cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
[root@rabbitmq-1 ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
nbproc 4
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
defaults
mode http
log global
retries 3
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 2048
#---------------------------------------------------------------------
##监控查看本地状态#####
listen admin_stats
bind *:80 #(此处需要修改端口,将80修改为其他端口)
mode http
option httplog
option httpclose
log 127.0.0.1 local0 err
stats uri /haproxy
stats auth newrain:123456
stats refresh 30s
####################################
###反代监控
frontend server
bind *:5670
log global
mode tcp
#option forwardfor
default_backend rabbitmq
maxconn 3
backend rabbitmq
mode tcp
log global
balance roundrobin
server rabbitmq1 192.168.203.158:5672 check inter 2000s rise 2 fall 3
server rabbitmq2 192.168.203.157:5672 check inter 2000s rise 2 fall 3
server rabbitmq3 192.168.203.137:5672 check inter 2000s rise 2 fall 3
[root@rabbitmq-1 ~]# systemctl start haproxy
[root@rabbitmq-1 ~]# vim rabbitmq-test/settings.py
# rabbitmq 配置
MQName="message_queue"
MQUser="guest"
MQPassword="guest"
MQHost="localhost"
MQPort=5670 (将此处端口改为5670端口保证负载均衡)
# mysql数据库配置
DBHost="localhost"
DBUser="root"
DBPassword="123" (此处为mysql密码)
[root@rabbitmq-1 rabbitmq-test]# nohup flask run --reload -p 80 -h.0.0.0 & (后台启动服务rebbitmq-test-main)
安装mysql,安装mysql完成后便可以进入192.168.203.158/read中发出货物,并会在数据库总自动生成表,进入192.168.203.158/haproxy查看rabbitmq,并结合192.168.203.158:15672查看分析