rabbitmq+haproxy+keepalive負荷等化を実現(高可用性)

10998 ワード

原文の住所、転載は出典を明記してください:http://blog.csdn.net/qq_34021712/article/details/72634167     ©王赛超
1.一般構築クラスタ:『rabbitmqクラスタ構築(マルチマシン)』『rabbitmq単機マルチインスタンスクラスタ構築』
2.一般クラスタを構成ポリシーによりミラークラスタに変更する:『rabbitmq構成ミラーモード』

3.Haproxyロードエージェント
1)haproxyを192.168.1.1101および192.168.1.1102ノードにインストールする
yum install haproxy

          
2)修正/etc/haproxy/haproxy.cfgは以下の内容で、すべて上書きして、対応するポート番号を修正します
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2
chroot /var/lib/haproxy              #         
pidfile /var/run/haproxy.pid         # haproxy pid    ,                 
maxconn 4000                         #      ,  4000
user haproxy                         #     
group haproxy                        #    
daemon                               #   1     deamon    。             daemon
stats socket /var/lib/haproxy/stats  #             
#---------------------------------------------------------------------
# defaults settings
#---------------------------------------------------------------------
#   :     tcp   ,    http       
defaults
mode http                            #      mode { tcp|http|health },tcp 4 ,http 7 ,health    OK
log global
# option httplog                     #   http    
option dontlognull                   #     ,           。                
# option http-server-close           #            http  
# option forwardfor except 127.0.0.0/8  #                 ip       ,   Http Header      ip
option redispatch                    # serverId         ,             
retries 3                            # 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                         #      
######################   haproxy     ###############################
listen status
bind 0.0.0.0:9188
mode http
stats enable
stats refresh 30s
stats uri /stats                     #  haproxy     http://localhost:9188/stats
stats auth admin:123456              #         
stats realm (Haproxy\ statistic)
stats admin if TRUE
######################  rabbitmq web    ############################
listen rabbitmq_admin 
    bind 0.0.0.0:15670
    server rabbit1 192.168.1.102:15672
    server rabbit2 192.168.1.101:15672
    server rabbit3 192.168.1.101:15673
#######################  rabbimq_cluster #################################
listen rabbitmq_cluster
bind 0.0.0.0:5670
mode tcp
balance roundrobin                   #      (#banlance roundrobin   ,balance source   session ,  static-rr,leastconn,first,uri   )
server rabbit1 192.168.1.102:5672 check inter 5000 rise 2 fall 2  #check inter 2000        
server rabbit2 192.168.1.101:5672 check inter 5000 rise 2 fall 2  #rise 2 2          
server rabbit3 192.168.1.101:5673 check inter 5000 rise 2 fall 2  #fall 2 2           

          
3)haproxyの起動
haproxy -f /etc/haproxy/haproxy.cfg
service haproxy restart#  
service haproxy stop#  

          
4)2つのipはそれぞれ以下のアドレスにアクセスし、以下のページ証明が間違いない
rabbitmq+haproxy+keepalive实现负载均衡(高可用)_第1张图片
rabbitmq+haproxy+keepalive实现负载均衡(高可用)_第2张图片
          
5)コードを使用してキューが使用可能かどうかをテストする
Producerクラス
package com.rabbitmq.test.T_helloworld;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.test.util.ConnectionUtil;


/**
 * helloworld
 * @author lenovo
 *
 */
public class Producer {


	private final static String QUEUE_NAME = "test_queue";


    public static void main(String[] argv) throws Exception {
    	//        
		ConnectionFactory factory=new ConnectionFactory();			
		//      ,IP,  ,      
		factory.setHost("192.168.1.101");
		factory.setPort(5670);
		factory.setUsername("admin");
		factory.setPassword("admin");
		//vhost:    ,  broker       vhost,           。
		//factory.setVirtualHost("/testrabbit");
		//        mq  
		Connection connection = factory.newConnection();
        
        //Connection connection = ConnectionUtil.getConnection();
        //         
        Channel channel = connection.createChannel();


        /*
         *   (  )  
         *   1:    
         *   2: true server        
         *   3:        ,   true     connection  ,            
         *   4:             (    ,          )
         *   5:          
         */
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        //     
        String message = "Hello World!";
        /*
         *  server      
         *   1:exchange  ,         exchange
         *   2:routing key
         *   3:     
         *   4:   
         * RabbitMQ     exchange, default exchange,          ,  direct exchange  ,
         *       exchange         routing key         ,         ,       
         */
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [   ] Sent '" + message + "'");


        //       
        channel.close();
        connection.close();
    }
}

Consumerクラス
package com.rabbitmq.test.T_helloworld;


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.test.util.ConnectionUtil;


public class Consumer {


	private final static String QUEUE_NAME = "test_queue";


    public static void main(String[] argv) throws Exception {


    	//        
		ConnectionFactory factory=new ConnectionFactory();			
		//      ,IP,  ,      
		factory.setHost("192.168.1.102");
		factory.setPort(5670);
		factory.setUsername("admin");
		factory.setPassword("admin");
		//vhost:    ,  broker       vhost,           。
		//factory.setVirtualHost("/testrabbit");
		//        mq  
		Connection connection = factory.newConnection();
        //         
        Channel channel = connection.createChannel();


        //     (               ,             ,        ,                 ,         )
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);


        //         
        QueueingConsumer consumer = new QueueingConsumer(channel);
       /*
         *     
         *   1:    
         *   2:    ack ,   ack           ,    ack。      channel.basicAck    ack
         *   3:   
         */ 
        channel.basicConsume(QUEUE_NAME, true, consumer);


        //     
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [   ] Received '" + message + "'");
        }
    }
}

4.インストール構成keepalived
         
1)keepalivedのインストール
yum install keepalived
chkconfig --add keepalived#      ,      

2)修正/etc/keepalived/keepalived.confプロファイルは、ホストとバックアップが少し異なり、プロファイルに宣言されています.詳しくお読みください.
! Configuration File for keepalived
#     ,     
global_defs {
   notification_email {               ##    ,   
     [email protected]
   }
   notification_email_from [email protected] ####   
   smtp_server 192.168.1.102                     ##     
   smtp_connect_timeout 30                       ##         
   router_id LVS_DEVEL                           ##     
}
#       ,  track_script  
vrrp_script check_haproxy {
script "killall -0 haproxy"
interval 2
}
vrrp_instance HAPROXY_HA {
#           ,       ,    BACKUP
#      ,   :
# state BACKUP
state MASTER
#   HA      ,   ifconfig          
interface eth1
#       ,   VRRP          ,   
virtual_router_id 80
#        VRRP     ,        VRRP  
#         ,       ,            ,           MASTER,     VIP
#     ,   :
unicast_src_ip 192.168.1.101
 unicast_peer {
 192.168.1.102
}
#     ,   :
#unicast_src_ip 192.168.1.102
#unicast_peer {
#192.168.1.101
#}
#      ,               
#     ,   :
priority 100
#     ,   :
#priority 80
#                  
advert_int 2
#              ,   down ,    ,        ,     ,         ,            
nopreempt
#                  ,   VRRP      
authentication {
auth_type PASS
auth_pass 1234
}
#            ,        ,     shell
#  keepalived     MASTER ,    
#notify_master "/etc/keepalived/master.sh"
#  keepalived     BACKUP ,    
#notify_backup "/etc/keepalived/backup.sh"
#  keepalived     FAULT ,    
#notify_fault "/etc/keepalived/fault.sh"
#  keepalived     STOP ,    
#notify_fault "/etc/keepalived/stop.sh"
#       ,  vrrp_script  
track_script {
check_haproxy
}
#     IP  , keepalived     MASTER , IP         
#       BACKUP , IP         
#       ip add        
virtual_ipaddress {
192.168.1.106  #  ip          
}
}

          
3)サービス開始
       haproxy    ,         :
(1)        haproxy:
haproxy -f /etc/haproxy/haproxy.cfg
(2)   keeepalived:   master  ,   BACKUP  
keepalived start

         
4)テスト(192.168.1.1106 keepalivedで構成された仮想ip)
rabbitmq+haproxy+keepalive实现负载均衡(高可用)_第3张图片
rabbitmq+haproxy+keepalive实现负载均衡(高可用)_第4张图片
          
5)模擬メインノード故障テスト
rabbitmq+haproxy+keepalive实现负载均衡(高可用)_第5张图片