Rabbitmq系列之整合Haproxy实现负载均衡

概述

  HAProxy 提供高可用性、负载均衡及基于 TCP 和 HTTP 应用的代理,支持虚拟主机 ,它是免费、快速并且可靠的一种解决方案,包括 Twitter、Reddit、StackOverflow、GitHub 在内的多家知名互联网公司在使用。 HAProxy 实现了一种事件驱动 、单一进程模型 ,此模型支持非常大的井发连接数。摘自《RabbitMQ实战指南》

安装
  • 使用阿里云的镜像

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

  • 使用yum安装

yum -y install haproxy

  • 备份默认配置(此处可以省略)

cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak

  • 配置自定义配置

vim /etc/haproxy/haproxy.cfg

  • 自定义配置内容
global
	#日志输出配置,所有日志都记录在本机,通过 localO 输出
    log 127.0.0.1 local0 info
    maxconn 4096
    stats socket /tmp/haproxy.socket uid haproxy mode 770 level admin
    #以守护进程方式运行 haproxy #debug #quiet
    daemon
defaults
	#应用全局的日志配置
    log global
    #默认的模式 mode{tcp l http l health}
    #TCP是 4层, HTTP是 7层, health只返回 OK
    mode    tcp
    #日志类别 tcplog
    option  tcplog
    #不记录健康检查日志信息
    option  dontlognull  
    retries 3 #3 次失败则认为服务不可用
    option  redispatch
    maxconn 2000 #每个进程可用的最大连接数
    timeout connect 5s #连接超时
    timeout client 120s #客户端超时
    timeout server 120s #服务端超时
listen rabbitmq_local_cluster 0.0.0.0:5670 #配置 TCP 模式
    mode tcp
    balance roundrobin #简单轮询
    #RabbitMQ 集群节点配置
    server node1 10.211.55.8:5672 check inter 5000 rise 2 fall 3 
    server node2 10.211.55.9:5672 check inter 5000 rise 2 fall 3
    server node3 10.211.55.10:5672 check inter 5000 rise 2 fall 3
listen private_monitoring :8100 #监控页面地址
    mode http
    option httplog
    stats enable
    stats uri   /stats
    stats refresh 5s #界面自动刷新时间
  • 启动

haproxy -f /etc/haproxy/haproxy.cfg

  • 访问(由于虚拟机有限,node1服务器既安装了rabbitmq又安装了Haproxy)

http://10.211.55.8:8100/stats

在这里插入图片描述

测试
  • 客户端测试代码
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class testRabbitmq {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Config.HOST);
        connectionFactory.setPort(5670);
        connectionFactory.setUsername(Config.USER);
        connectionFactory.setPassword(Config.PWD);
        connectionFactory.setVirtualHost("/");
        Channel channel = null;
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            String queueName = "test2";
            channel.queueDeclare(queueName, true, false, false, null);
            String msg = "hello,Rabbitmq";
            for (int i = 0; i < 300; i++) {
                channel.basicPublish("", queueName, null, msg.getBytes("UTF-8"));
                Thread.sleep(500);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            channel.close();
            connection.close();
        }

    }
}

class Config {
    static String HOST = "10.211.55.8";
    static String PWD = "123456";
    static String USER = "admin";

}

  • 第1次连接node1队列
    在这里插入图片描述

  • 第2次连接node2队列
    在这里插入图片描述

  • 第3次连接node3队列
    在这里插入图片描述

感谢

《RabbitMQ实战指南》
Centos7搭建RabbitMQ集群及单机多节点部署和rabbitmq理论解析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值