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)
上一篇 2017-06-29 22:18
下一篇 2017-06-30 09:30

相关推荐

  • N25-第十周

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情) 一、内核空间详细过程如下: POST –> BootSequence(BIOS) –> Bootloader(MBR) –> kernel(ramdisk) –> rootfs(只读) –> /sbin/init(systemd) 1.POST:(pow…

    Linux干货 2017-05-30
  • history

    history命令详解

    Linux干货 2018-02-28
  • Linux基础知识之WMware Tools的安装

    该博文以CentOS6.8_x86_64系统为基础,tty终端登录CentOS6.8系统,以root身份登录系统。 为什么要安装WMware Tools?                    VMware Tools是VMware虚拟机中自带的一种增强工具,…

    Linux干货 2016-07-29
  • linux命令 kill命令详则

    kill命令 kill用来删除执行中的程序或工作。kill可将指定的信息送至程序。预设的信息(默认)为SIGTERM(15),可经指定程序终止。若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除程序。程序或工作的编号可利用ps指令或job指令查看。 语法 kill(选项)(参数) 选项 -a:当处理当前进程时,不限制命令名和进程号的对应关系; &…

    2017-08-21
  • N25–第十二周作业

    1、  请描述一次完整的http请求处理过程; (1)建立和处理连接:接收请求或者拒绝请求; (2)接收请求:接收来自于网络上的主机请求报文中对某特定的资源的一次请求的过程; (3)处理请求:对请求报文进行解析,获取客户端请求的资源及请求方法等相关信息 (4)访问资源:获取请求报文中请求的资源 (5)构建响应报文; (6)发送响应报文; (7)记录…

    2017-03-28
  • Linux学习小结 1

    一、描述计算机的组成及其功能 计算机由硬件和软件组成: 1、硬件组成又分为: 中央处理器(CPU):功能主要是解释计算机指令以及处理计算机软件中的数据, 中央处理器主要包括运算器(算术逻辑运算单元,ALU,Arithmetic Logic Unit)和高速缓冲存储器(Cache)及实现它们之间联系的数据(Data)、控制及状态的总线(Bus)内部存储器(Me…

    Linux干货 2017-06-27