【rabbitmq知识体系】centos7下rabbitmq3.7.2集群配置

参考资料和更新历史

本文参考资料

http://www.cnblogs.com/luxiaoxun/p/3918054.html
https://www.jianshu.com/p/518a504fe2cc
https://www.rabbitmq.com/changelog.html
http://blog.didispace.com/spring-boot-rabbitmq/
https://blog.csdn.net/zyz511919766/article/details/41896747
https://www.rabbitmq.com/configure.html
https://www.jianshu.com/p/b6bdd6f56f9c

文章更新历史

2017-12-23 19:30:54 初稿
2019-04-29 20:30:54 因为erlang的rpm包被移走了,需要更新repo文件

部署规划

集群所有机器增加hosts

10.2.1.53 lxg53
10.2.1.54 lxg54
10.2.1.55 lxg55

目录规划

配置文件目录:/etc/rabbitmq/
主要工作目录:/var/lib/rabbitmq
日志文件目录:/data/rabbitmq/log
数据库目录:/data/rabbitmq/mnesia/rabbit@lxg53




ps:真的很讨厌程序的工作目录放在/var/lib目录下(这是系统盘下,如果系统盘符小,分分钟撑爆系统盘符)。
但是目前也没有找到有效的办法,修改工作目录。

安装erlang与rabbit-server集群

更新curl程序

yum update nss curl

更新erlang的repo

vim /etc/yum.repos.d/rabbitmq-erlang.repo

[rabbitmq-erlang]
name=rabbitmq-erlang
baseurl=https://dl.bintray.com/rabbitmq-erlang/rpm/erlang/20/el/7
gpgcheck=1
gpgkey=https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
repo_gpgcheck=0
enabled=1

安装erlang

yum install erlang

安装rabbitmq-server

wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.2/rabbitmq-server-3.7.2-1.el7.noarch.rpm
rpm --import https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc
yum install rabbitmq-server-3.7.2-1.el7.noarch.rpm

修改rabbitmq配置

配置文件位置


rabbit3.7之后,采用了rabbitmq.conf、rabbitmq-env.conf。(网上很多照抄的说,从3.7开始命名叫rabbitmq.config,看着蛋疼)

rabbitmq.conf

rabbitmq-env.conf

启动rabbitmq主节点和插件

创建目录及启动rabbit-server

chown -R rabbitmq:rabbitmq /var/lib/rabbitmq/
mkdir -p data/rabbitmq/log
mkdir -p /data/rabbitmq/mnesia
chown -R rabbitmq:rabbitmq /data/rabbitmq/
systemctl start rabbitmq-server

启用插件授权登录

rabbitmq-plugins enable rabbitmq_management
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
rabbitmqctl status

其他的插件

rabbitmq-plugins enable rabbitmq_mqtt
rabbitmq-plugins enable rabbitmq_stomp
rabbitmq-plugins enable rabbitmq_web_mqtt
rabbitmq-plugins enable rabbitmq_web_stomp

同步cookie和启动从节点

同步cookie到从节点

cat /var/lib/rabbitmq/.erlang.cookie

DSXSMAIWGQYIUAALJXQH

nc到从节点的/var/lib/rabbitmq/目录。

启动从节点

systemctl start rabbitmq-server
rabbitmq-plugins enable rabbitmq_management
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
rabbitmqctl stop_app
rabbitmqctl reset

从节点加入到集群

rabbitmqctl join_cluster --ram rabbit@lxg53
rabbitmqctl start_app

某个从节点重启

systemctl start rabbitmq-server
rabbitmqctl stop_app
rabbitmqctl start_app

镜像队列

负载均衡

负载均衡

#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local0 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

listen status
bind 10.2.1.30:18080 #监听的地址和端口,默认端口1080
mode http #模式
stats enable #启用状态监控
stats hide-version #隐藏软件版本号
stats auth admin:admin #登陆用户名和密码
stats realm HAproxy\ stats #提示信息,空格之前加\
stats admin if TRUE #当通过认证才可管理
stats uri /stats #访问路径,在域名后面添加/stats可以查看haproxy监控状态,默认为/haproxy?stats
stats refresh 5 #页面自动刷新间隔,每隔5s刷新

frontend rabbitmq_broker
bind 10.2.1.30:5672
default_backend rabbitmq_broker_backend

backend rabbitmq_broker_backend
balance roundrobin
server lxg53 10.2.1.53:5672 check
server lxg54 10.2.1.54:5672 check
server lxg55 10.2.1.55:5672 check

frontend rabbitmq_management
bind 10.2.1.30:15672
default_backend rabbitmq_management_backend

backend rabbitmq_management_backend
balance roundrobin
server lxg53 10.2.1.53:15672 check
server lxg54 10.2.1.54:15672 check
server lxg55 10.2.1.55:15672 check

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
#frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js

# use_backend static if url_static
# default_backend app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
# balance roundrobin
# server static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
#backend app
# balance roundrobin
# server app1 127.0.0.1:5001 check
# server app2 127.0.0.1:5002 check
# server app3 127.0.0.1:5003 check
# server app4 127.0.0.1:5004 check

最终效果