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

相關推薦

  • Python基礎篇之過程型程序設計

    一、Python過程型程序設計 面向過程 以指令為中心,由指令處理數據 如何組織代碼解碼問題 面向對象 以數據為中心,所有的處理代碼都圍繞數據展開 如何設計數據結構組織數據,并提供對此類數據所允許處理操作 簡單方法: 1)、編譯安裝新版本至某特定路徑 # yum install readline-devel # tar xf Python-2.7.6.tar…

    2018-01-11
  • KeepAlived高可用集群詳解及拓撲實驗搭建配置

    Linux Cluster:KeepAlive 1.集群類型:LB(負載均衡集群),HA(高可用集群),HP(高性能集群)     LB:均衡負載的實現LVS     HA:高可用的實現KeepAlived 2.RS:健康狀態檢測方式: (1)網絡層:icmp ping (2)傳…

    Linux干貨 2016-11-01
  • Linux下常用的日志收集命令(Redhat&SuSe)

       Linux下日志的采集和分析是一個非常重要的工作,一般廠商在你需要技術支持的時候,都需要你通過對應指令收集系統的信息,我這邊列舉下常用的兩個Linux廠商的收集命令(Redhat Linux以及SuSe Linux),便于收集后,對系統進行全面分析。   sosreport是一個類型于supportconfig …

    系統運維 2016-07-07
  • 網絡知識

    網絡知識 一、網絡初識 1.網絡概念  在計算機領域中,網絡是信息傳輸、接收、共享的虛擬平臺,通過它把各個點、面、體的信息聯系到一起,從而實現這些資源的共享。 2.分類 按覆蓋范圍分: 局域網LAN(作用范圍一般為幾米到幾十公里)。 城域網MAN(界于WAN與LAN之間)。 廣域網WAN(作用范圍一般為幾十到幾千公里)。 按拓撲結構分類: 總線型 …

    Linux干貨 2016-09-01
  • Bash的基礎特性之命令執行狀態返回值和命令行展開

    Bash的基礎特性之命令的執行狀態 Linux的命令執行結果狀態有兩種,分別為:1、成功2、失敗bash使用特殊變量 $? 保存最近一條命令的執行狀態結果使用echo $? 命令來查看命令執行狀態返回值:0:成功1-255:失敗 示例:         [root@localho…

    Linux干貨 2016-11-04
  • 基于NFS服務的wordpress站點

    實驗要求:             (1) nfs server導出/data/web,在目錄中提供wordpress;     (2) nfs client掛載nfs server導出的文件系統至/data/web; …

    2017-06-11
欧美性久久久久