RabblitMQ Cluster + HAProxy(负载均衡)

        在本节中,我们主要来学习软件负载均衡器HAProxy在RabbitMQ集群中的使用。

        软件负载均衡器HAProxy
        HAProxy的特点是:
        1、HAProxy是支持虚拟主机的,,并能支持上万级别的连接;
        2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作;
        3、支持url检测后端的服务器出问题的检测会有很好的帮助;
        4、它跟LVS一样,本身仅仅就只是一款负载均衡软件;单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的;
        5、HAProxy可以对Mysql读进行负载均衡,对后端的MySQL节点进行检测和负载均衡,不过在后端的MySQL slaves数量超过10台时性能不如LVS,所以我向大家推荐LVS+Keepalived;
        6、能够提供4层,7层代理。HAProxy支持两种主要的代理模式:" tcp"也即4层(大多用于邮件服务器、内部协议通信服务器等),和7层( HTTP)。在4层模式 下,HAProxy仅在客户端和服务器之间转发双向流量,7层模式下,HAProxy会分析协议,并且能通过允许、拒绝、交换、增加、修改或者删除请求 (request)或者回应(response)里指定内容来控制协议,这种操作要基于特定规则;
        7、HAProxy的算法现在也越来越多了,具体有如下8种:
             ①roundrobin,表示简单的轮询,这个不多说,这个是负载均衡基本都具备的;
             ②static-rr,表示根据权重,建议关注;
             ③leastconn,表示最少连接者先处理,建议关注;
             ④source,表示根据请求源IP,这个跟Nginx的IP_hash机制类似,我们用其作为解决session问题的一种方法,建议关注;
             ⑤ri,表示根据请求的URI;
             ⑥rl_param,表示根据请求的URl参数'balance url_param' requires an URL parameter name;
             ⑦hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
             ⑧rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。

         好了,下面我们来开始安装HAProxy:
引用

         # wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz
         # chmod +x haproxy-1.4.24.tar.gz
         # tar xzvf haproxy-1.4.24.tar.gz
         # cd haproxy-1.4.24
         //linux2.6 kenel的epoll功能可以提高HAProxy的性能,故在编译是要加上TARGET=linux26或TARGET=generic
         # make TARGET=generic
         # make install

        


         配置RabbitMQ集群的HAProxy
         下面我们开始先来创建rabbitmq集群,详情参看 《在单节点上构建built-in内置集群》,这里不再赘述。
         接下来我们编辑集群的配置文件:
引用

         # mkdir conf
         # touch haproxy-rabbitmq.conf
         # chmod +x haproxy-rabbitmq.conf
         # vi haproxy-rabbitmq.conf

        

         haproxy-rabbitmq.conf配置文件内容如下:
引用

# this config needs haproxy-1.4.24

#logging options
global
        log 127.0.0.1 local0 info
        maxconn 5120
        chroot /opt/haproxy-1.4.24
        uid 99
        gid 99
        daemon
        quiet
        nbproc  2
        pidfile /opt/haproxy-1.4.24/haproxy.pid

#load balancing defaults
defaults
       log        global
       #使用4层代理模式,"mode   http"为7层代理模式
       mode       tcp
       #if you set mode to tcp,then you nust change tcplog into httplog
       option     tcplog
       option     dontlognull
       retries    3
       option redispatch
       maxconn 2000
       contimeout      5s
       clitimeout      120s
       srvtimeout      120s

#front-end IP for consumers and producters
listen rabbitmq_local_cluster 127.0.0.1:5670
       #配置TCP模式
       mode      tcp
       #balance url_param userid
       #balance url_param session_id check_post 64
       #balance hdr(User-Agent)
       #balance hdr(host)
       #balance hdr(Host) use_domain_only
       #balance rdp-cookie
       #balance leastconn
       #balance source  //ip
       #简单的轮询
       balance roundrobin
       #rabbitmq集群节点配置
       server rabbit  127.0.0.1:5672 check inter 5000 rise 2 fall 2
       server rabbit2 127.0.0.1:5674 check inter 5000 rise 2 fall 2
       server rabbit3 127.0.0.1:5676 check inter 5000 rise 2 fall 2

#配置haproxy web监控,查看统计信息
listen private_monitoring :8100
       mode    http
       option  httplog
       stats   enable
       #设置haproxy监控地址为http://localhost:8100/rabbitmq-stats
       stats   uri  /rabbitmq-stats
       stats   refresh 5s
        

       好了到这里就将rabbitmq集群的haproxy配置文件写好了,下面我们来启动haproxy:
引用

       # /opt/haproxy-1.4.24/haproxy -f /opt/haproxy-1.4.24/conf/haproxy-rabbitmq.conf
      

       最后我们打开http://localhost:8100/rabbitmq-stats,看一下监控页面,如果显示出正常就表明已经将HAProxy负载均衡配置好了!

       我们做一个测试,如果将rabbit3这个rabbitmq停掉,那么在HAProxy监控页面就会江浙一变化显示出来,rabbit3这一行就会显示“棕红色”:


      
        
       

猜你喜欢

转载自flyingdutchman.iteye.com/blog/1912690
今日推荐