haproxy实现rabbitmq负载均衡

RabbitMQ简介:

1、是实现AMQP(高级消息队列协议)的消息中间件的一种。
2、主要是为了实现系统之间的双向解耦而实现的。当生产者大量产生数据时,消费者无法快速消费,那么需要一个中间层。保存这个数据。

一般提到 RabbitMQ 和消息,都会用到以下一些专有名词:
(1)生产(Producing)意思就是发送。发送消息的程序就是一个生产者(producer)。我们一般用 “P” 来表示。
(2)队列(queue)就是邮箱的名称。消息通过你的应用程序和 RabbitMQ 进行传输,它们能够只存储在一个队列(queue)中。 队列(queue)没有任何限制,你要存储多少消息都可以——基本上是一个无限的缓冲。多个生产者(producers)能够把消息发送给同一个队列,同样,多个消费者(consumers)也能够从同一个队列(queue)中获取数据。
(3)消费(Consuming)和获取消息是一样的意思。一个消费者(consumer)就是一个等待获取消息的程序。

那么开始了解一下 RabbitMQ 在centos7下的安装于运用吧
1.准备机器,做好域名解析,主机名解析,时间同步

192.168.42.151 node2 [haproxy]
192.168.42.152 node3 [rabbitmq master]
192.168.42.153 node4 [rabbitmq slave]

node2上操作:

vim /etc/hosts
192.168.42.151 node2
192.168.42.152 node3
192.168.42.153 node4

hostnamectl set-hostname node2

rabbitmq依赖短格式的主机名,因此我们需要配置主机名并加入hosts解析
每个节点都要配置哦,因此我们把node2上的hosts推送到node3,node4上

scp /etc/hosts 192.168.42.152:/etc/
scp /etc/hosts 192.168.42.153:/etc/

node3上操作:

hostnamectl set-hostname node3

node4上操作:

hostnamectl set-hostname node4

2.安装rabbitmq

node3上操作:

(1).安装rabbitmq

yum install rabbitmq-server -y

[root@node4 ~]# cd /etc/rabbitmq/
[root@node4 rabbitmq]# ls
rabbitmq.config

默认只有rabbitmq.config的配置文件

(3).启动rabbitmq-management管理

我们启用WEB管理。

rabbitmq-plugins enable rabbitmq_management

[root@node3 rabbitmq]# rabbitmq-plugins enable rabbitmq_management
The following plugins have been enabled:
  mochiweb
  webmachine
  rabbitmq_web_dispatch
  amqp_client
  rabbitmq_management_agent
  rabbitmq_management
Plugin configuration has changed. Restart RabbitMQ for changes to take effect.

cd /etc/rabbitmq/
[root@node3 rabbitmq]# ls
enabled_plugins  rabbitmq.config

再次查看/etc/rabbitmq/,可以看到生成了一个enabled_pluginsd的配置文件

(3).启动rabbitmq

systemctl start rabbitmq-server

查看一下状态

[root@node3 ~]# ss -tnl
State      Recv-Q Send-Q   Local Address:Port      Peer Address:Port              
LISTEN     0      128                  *:4369                 *:*                  
LISTEN     0      128                  *:22                   *:*                  
LISTEN     0      128                  *:15672                *:*                  
LISTEN     0      100          127.0.0.1:25                   *:*                  
LISTEN     0      128                  *:25672                *:*                  
LISTEN     0      128                 :::4369                :::*                  
LISTEN     0      128                 :::22                  :::*                  
LISTEN     0      100                ::1:25                  :::*                  
LISTEN     0      128                 :::5672                :::*

3.去浏览器中访问测试一把:
默认账户密码 guest/guest
http://192.168.42.152:15672/ 可以进入管理界面了

node4 同上

4.把node3,node4组成集群
我们先来查看一下这两个节点的集群状态

node3:

[root@node3 rabbitmq]# rabbitmqctl cluster_status
Cluster status of node rabbit@node3 ...
[{nodes,[{disc,[rabbit@node3]}]},
 {running_nodes,[rabbit@node3]},
 {cluster_name,<<"rabbit@node3">>},
 {partitions,[]}]
...done.

node4:

[root@node4 ~]# rabbitmqctl cluster_status
Cluster status of node rabbit@node4 ...
[{nodes,[{disc,[rabbit@node4]}]},
 {running_nodes,[rabbit@node4]},
 {cluster_name,<<"rabbit@node4">>},
 {partitions,[]}]
...done.

我们让node3作为主节点 (1).停掉主节点的应用

[root@node3 rabbitmq]# rabbitmqctl stop_app
Stopping node rabbit@node3 ...
...done.

(2).添加集群节点(指cluster_name)

rabbitmqctl join_cluster rabbit@node4

Clustering node rabbit@node3 with rabbit@node4 ...
Error: unable to connect to nodes [rabbit@node4]: nodedown

DIAGNOSTICS
===========

attempted to contact: [rabbit@node4]

rabbit@node4:
  * connected to epmd (port 4369) on node4
  * epmd reports node 'rabbit' running on port 25672
  * TCP connection succeeded but Erlang distribution failed
  * suggestion: hostname mismatch?
  * suggestion: is the cookie set correctly?

current node details:
- node name: rabbitmqctl7692@node3
- home dir: /var/lib/rabbitmq
- cookie hash: BbXCvnYxz/Q/iwctrCP/bQ==

会发现报错,告诉我们cookie信息不一致,因此我们需要把作为主节点的cookie信息推送到各个从节点上
cookie信息放在/var/lib/rabbitmq/下,我们cd进去,查看一下子

cd /var/lib/rabbitmq/

[root@node3 rabbitmq]# ll -a
total 8
drwxr-x---   3 rabbitmq rabbitmq   42 Jun 29 16:24 .
drwxr-xr-x. 27 root     root     4096 Jun 29 16:23 ..
-r--------   1 rabbitmq rabbitmq   20 Jun 29 00:00 .erlang.cookie
drwxr-xr-x   4 rabbitmq rabbitmq   85 Jun 29 17:13 mnesia

(3).推送cookie信息,并保证权限400及属主属组是rabbitmq 如下:

-r--------   1 rabbitmq rabbitmq     20 Jun 29 21:14 .erlang.cookie

使用scp命令推送cookie

scp -rp .erlang.cookie 192.168.42.153:/var/lib/rabbitmq/

然后,我们再执行

rabbitmqctl join_cluster rabbit@node4

[root@node3 rabbitmq]# rabbitmqctl join_cluster rabbit@node4
Clustering node rabbit@node3 with rabbit@node4 ...
...done.

可见我们的集群成功了

查看一下状态:

[root@node3 rabbitmq]# rabbitmqctl cluster_status
Cluster status of node rabbit@node3 ...
[{nodes,[{disc,[rabbit@node3,rabbit@node4]}]}]
...done.

可以看到有两个节点添加进去了

(4).最后启动我们关闭的节点应用

[root@node3 rabbitmq]# rabbitmqctl start_app
Starting node rabbit@node3 ...
...done.

(5).去浏览器访问两个

http://192.168.42.152:15672
http://192.168.42.153:15672

都可以看到有两个node3,node4的信息 至此我们的rabbitmq集群,高可用已经做好了

5.用haproxy实现node3,node4的rabbitmq 负载均衡

在node2上操作:

(1).haproxy的安装

yum install haproxy -y

(2).配置haproxy
配置文件如下:

#---------------------------------------------------------------------
# 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 local2

    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                    tcp
    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 http_front
        bind   *:1080           #监听端口 
        mode    http   
        stats refresh 30s           #统计页面自动刷新时间  
        stats uri /haproxy?status            #统计页面url  
        stats realm Haproxy Manager #统计页面密码框上提示文本  
        stats auth admin:admin      #统计页面用户名和密码设置  
        #stats hide-version         #隐藏统计页面上HAProxy的版本信息

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  rabbitmq_web
    bind    *:15672
    default_backend        websrvs

frontend  rabbitmq_server
    bind    *:5672
    default_backend        servers

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvs
    balance     roundrobin
    server      node3 192.168.42.152:15672 check
    server      node4 192.168.42.153:15672 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend servers
    balance     roundrobin
    server  node3  192.168.42.152:5672 check
    server  node4  192.168.42.153:5672 check

(3)启动haproxy

systemctl start haproxy
[root@node2 haproxy]# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128               *:22                            *:*                  
LISTEN     0      3000              *:15672                         *:*                  
LISTEN     0      3000              *:1080                          *:*                  
LISTEN     0      100       127.0.0.1:25                            *:*                  
LISTEN     0      3000              *:5672                          *:*                  
LISTEN     0      128              :::22                           :::*                  
LISTEN     0      100             ::1:25                           :::*

(4).去浏览器访问
http://192.168.42.151:15672/ 可以看到rabbitmq的web界面
http://192.168.42.151:1080/haproxy?status 可以看到haproxy的监控界面

原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/78877

(3)
sraybansrayban
上一篇 2017-06-29
下一篇 2017-06-30

相关推荐

  • Puppet基于Master/Agent模式实现LNMP平台部署

    前言 随着IT行业的迅猛发展,传统的运维方式靠大量人力比较吃力,运维人员面对日益增长的服务器和运维工作,不得不把很多重复的、繁琐的工作利用自动化处理。前期我们介绍了运维自动化工具ansible的简单应用,本期带来的是运维自动化神器puppet基于Master/Agent模式实现LNMP平台部署。 Puppet 简介 Puppet是基于ruby语言开发的一种L…

    Linux干货 2015-07-13
  • Linux Cluster之keepalived及keepalived + LVS DR的实现

      一、HA Cluster基础 系统可用性A=MTBF/(MTBF+MTTR) MTBF:平均无故障时间 MTTR:平均修复时间 降低MTTR的方式:冗余(redundent) 衡量标准:几个9 90%、99%、99.9%… 提升系统可用性的办法之一:降低MTTR 通过冗余(redundant)的方式能够避免单点故障(SPoF),从而…

    2016-11-02
  • Centos7下的systemd管理

    systemd简介 Systemd是由红帽公司的一名叫做Lennart Poettering的员工开发,systemd是Linux系统中最新的初始化系统(init),它主要的设计目的是克服Sys V 固有的缺点,提高系统的启动速度,systemd和upstart是竞争对手,ubantu上使用的是upstart的启动方式,centos7上使用systemd替换…

    Linux干货 2016-09-23
  • 马哥教育网络班21期-第六周课程练习

    请详细总结vim编辑器的使用并完成以下练习题1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; #cp /etc/rc.d/rc.sysinit /tmp #vim /tmp/rc.sysinit :%s/^[[:space:]]/#…

    Linux干货 2016-08-15
  • Bash Shell语法之三函数简介

     Bash Shell语法之三函数简介 一、什么是Bash函数 像其他编程语言一样,Shell 也支持函数。Shell函数必须先定义后使用。函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。 二、Bash函数定义格式 [ function ] fun () {     &nb…

    Linux干货 2015-05-11
  • 路由实验

    实验需求 使用两台Linux主机充当路由器,配置路由条目,让两台PC机互通 1.将两台Linux主机添加两块虚拟网卡 2.配置R1路由器网络属性     3.配置R1路由器网络属性     4.配置PC1主机IP地址(Linux主机) 5.配置PC2主机的IP地址(Windows) 6.测试,PC1和PC2可以互通,之…

    Linux干货 2016-09-09