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.www58058.com/78877

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

相關推薦

  • N26-第二周博客作業

    一、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 一) 目錄管理命令 1. cd :用于切換目錄。命令格式為:cd dirname 命令使用技巧: ~]# cd 進入用戶主目錄 ~]# cd – 進入上一次所在目錄 ~]# cd / 進入根目錄 ~]# cd ~ 進入用戶主目錄 ~]# cd .. 返回上一級…

    2017-03-01
  • Linux基礎-用戶管理相關操作-week 4

    1.復制/etc/skel 目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限  cp /etc/skel /home/tuser1 -rf chmod og=  /home/tuser1 -R 2.編輯/etc/group文件添加組hadoop echo hadoop:x:503 …

    Linux干貨 2016-11-21
  • N24 第6周作業

    請詳細總結vim編輯器的使用并完成以下練習題 1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#; 2、復制/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符; 3、刪除/tmp/rc.sysinit文件…

    Linux干貨 2016-11-30
  • 安裝LNMP + 搭建WordPress個人博客

    安裝LNMP + 搭建Wordpress個人博客 前言 來到馬哥教育也有幾個月了,學了很多知識?,F在想要把這些知識能夠存儲在一個地方,隨時隨地的都能看到,于是乎我就想到了博客,以下我搭建Wordpress的過程。 安裝LNMP 一、關掉防火墻 # chkconfig iptables off 二、安裝開發包和庫文件 #&nbsp…

    Linux干貨 2016-05-31
  • RPM軟件包管理

                                                RPM 軟件包管理 1、rpm軟件包管理: &nbs…

    系統運維 2016-09-01
  • find命令詳解

    find命令詳解 實時查找工具,通過遍歷指定起始路徑下文件系統層級結構完成文件查找; 一、工作特性: 查找速度略慢; 精確查找,只查找文件路徑的基名而非整個路徑; 實時查找; 可能只搜索用戶具備讀取和執行權限的目錄 二、 用法: find [查找起始路徑] [OPTIONS] [查找條件] [處理動作] 查找起始路徑:指定具體搜索目標起始路徑;默認為當前目錄…

    Linux干貨 2017-03-19
欧美性久久久久