RabbitMQ负载均衡HAProxy

在{{RabbitMQ 集群部署}} 的基础是配置负载均衡

HAProxy 是一款开源免费,并提供高可用性、负载均衡以及基于 TCP 和 HTTP 协议的代理软件,可以 支持四层、七层负载均衡,经过测试单节点可以支持 10W 左右并发连接。

安装 HAProxy​

安装方式 一

yum install gcc -y 
tar -zxf haproxy-2.1.0.tar.gz 
cd haproxy-2.1.0 
make TARGET=linux-glibc 
make install 
mkdir /etc/haproxy

#赋权 
groupadd -r -g 149 haproxy 
# 添加用户 
useradd -g haproxy -r -s /sbin/nologin -u 149 haproxy

#创建haproxy配置文件 
touch /etc/haproxy/haproxy.cfg

安装方式 二

yum -y install haproxy
#那么haproxy默认在
#/usr/sbin/haproxy
#且会自动创建配置文 件
#/etc/haproxy/haproxy.cfg
#自动创建用户haproxy
/etc/haproxy/haproxy.cfg

配置 HAProxy

haproxy.cfg 配置文件替换修改

#---------------------------------------------------------------------
# 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
    log global
# tcp:实例运行于纯 TCP 模式,第 4 层代理模式,在客户端和服务器端之间将建立一个全双工的连接,
# 且不会
# 通常用于 SSL、SSH、SMTP 等应用;  
    mode tcp
    option tcplog
    option dontlognull
    retries 3
    option redispatch
    maxconn 2000
# contimeout 5s
    timeout connect 5s
# 客户端空闲超时时间为 60 秒则 HA 发起重连机制
    timeout client 60000
# 服务器端链接超时时间为 15 秒则 HA 发起重连机制
    timeout server 15000

listen rabbit_cluster
# VIP,反向代理到下面定义的三台 Real Server
    bind 172.16.94.13:5672
#配置 TCP 模式
    mode tcp
#简单的轮询
    balance roundrobin
# rabbitmq 集群节点配置
# inter 每隔五秒对 mq 集群做健康检查,2 次正确证明服务器可用,2 次失败证明服务器不可用,并且配置主备机制
server rabbitmqNode1 172.16.94.22:5672 check inter 5000 rise 2 fall 2
server rabbitmqNode2 172.16.94.24:5672 check inter 5000 rise 2 fall 2
server rabbitmqNode3 172.16.94.23:5672 check inter 5000 rise 2 fall 2
#配置 haproxy web 监控,查看统计信息
listen stats
    bind 172.16.94.13:9000
    mode http
    option httplog
# 启用基于程序编译时默认设置的统计报告
    stats enable
#设置 haproxy 监控地址为 http://node1:9000/rabbitmq-stats
    stats uri /rabbitmq-stats
# 每5s刷新一次页面
    stats refresh 5s

启动 HAProxy:

haproxy -f /etc/haproxy/haproxy.cfg

关闭

➜  ~ haproxy -f /etc/haproxy/haproxy.cfg
➜  ~ ps aux | grep haproxy
haproxy    6387  0.1  0.1  48604  1688 ?        Ss   12:30   0:00 haproxy -f /etc/haproxy/haproxy.cfg
➜  ~ kill -9 6387
➜  ~ 

检查进程状态

可以通过访问 http://172.16.94.13:9000/rabbitmq-stats​ 查看状态

image.png

测试

可以在代码中直接测试,代码配置直接连接到 HAProxy 和监听端口上。

新建 Springboot 项目选择 rabbitmq 和 mvc


spring.application.name=haproxy_demo
spring.rabbitmq.host=mha
spring.rabbitmq.virtual-host=/
spring.rabbitmq.username=root
spring.rabbitmq.password=1234
spring.rabbitmq.port=5672

config 类

package com.galaxy.mqdemospringbootproxy.config;

/**
 * @author lane
 * @date 2021年09月16日 下午1:16
 */
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    @Bean
    public Queue queue() {
        return new Queue("queue.haproxy",
                true,
                false,
                false,
                null);
    }

    @Bean
    public Exchange exchange() {
        return new DirectExchange("ex.haproxy",
                true,
                false,
                null);
    }

    @Bean
    public Binding binding() {
        return new Binding("queue.haproxy",
                Binding.DestinationType.QUEUE,
                "ex.haproxy",
                "key.haproxy", null);
    }

    @Bean
    @Autowired
    public RabbitAdmin rabbitAdmin(ConnectionFactory factory) {
        return new RabbitAdmin(factory);
    }

}

controller 类

package com.galaxy.mqdemospringbootproxy.controller;

/**
 * @author lane
 * @date 2021年09月16日 下午1:17
 */
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
public class BizController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping("/biz/{hello}")
    public String doBiz(@PathVariable String hello) throws UnsupportedEncodingException {

        final MessageProperties properties = MessagePropertiesBuilder.newInstance()
                .setContentType("text/plain")
                .setContentEncoding("utf-8")
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT)
                .setHeader("mykey", "myvalue")
                .build();

        final Message message = MessageBuilder
                .withBody(hello.getBytes("utf-8"))
                .andProperties(properties)
                .build();

        amqpTemplate.send("ex.haproxy", "key.haproxy", message);

        return "ok";
    }

}

访问 localhost:8080/biz/hello 之后

image.png

访问 http://172.16.94.13:9000/rabbitmq-stats

image.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值