RabbitMQ:搭建Haproxy+RabbitMQ集群方式(完成)

当前的版本:rabbitmq 3.8.3haproxy 1.5.18

1.声明

当前内容用于本人学习和复习之用,内容包括haproxy的编译、配置rabbitmq以及其中发现的问题(基于本地的VM 中的Linux、CentOS7)

2.安装haproxy

yum install -y haproxy

此时使用haproxy

在这里插入图片描述

3.启动集群

节点节点ip
node2192.168.1.104
node3192.168.1.107

此时的页面为
在这里插入图片描述
此时集群的状态是成功的

4.配置Haproxy到RabbitMQ集群

Haproxy搭建的地址在:192.168.1.100

1.直接编辑(使用yum安装后的haproxy的配置文件在/etc/haproxy/haproxy.cfg)

vi /etc/haproxy/haproxy.cfg

2.开始编写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
    mode                    http
    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

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
# 开启前端可以通过 ip地址:15672方式访问app中的地址(fronted主要用于前端的显示)
frontend  main *:15672
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js

    use_backend 	static          if url_static
    default_backend     app


#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
    balance     roundrobin
    server      static 127.0.0.1:4331 check

##
#  haproxy 的管理页面
#
listen admin_stats *:8090     #为haproxy访问状态监控页面配置,取名为admin_stats
    stats enable         #启用监听端口
    mode http         #http的7层模式
    log global         # 继承global中log的定义
    stats uri  /stats     #监控页面的url访问路径,即http://ip/stats访问监控页面
    stats realm Haproxy\ Statistics     #监控页面的密码框提示信息
    stats auth  admin:admin          #监控页面的用户和密码admin,可以设置多个用户名
    #stats hide-version   #隐藏统计页面上HAProxy的版本信息
    stats admin if TRUE    #当通过认证才可管理
    stats refresh 30s    #页面自动刷新时间30s

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
# 配置前端的负载均衡
backend app
    balance     roundrobin
    server  app1 192.168.1.104:15672 check inter 2000 fall 5
    server  app2 192.168.1.107:15672 check inter 2000 fall 5
    #server  app3 127.0.0.1:5003 check
    #server  app4 127.0.0.1:5004 check

# 开启一个端口本地ip:5672方式用于访问后面的节点对应的地址(注意对于tcp需要使用listen)
listen rabbit_servers *:5672
   mode tcp
   balance roundrobin
   server rabbit_node2 192.168.1.104:5672 check inter 2000 fall 5
   server rabbit_node3 192.168.1.107:5672 check inter 2000 fall 5


3.启动当前的haproxy

haproxy -f haproxy.cfg

注意前面配置文件中的端口的开放

查看界面结果:http://192.168.1.108:8090/stats
在这里插入图片描述

此时可以发现当前的haproxy是启动成功的,并且可以访问两个节点,当前的节点是成功的

5.测试15672是否可以访问

直接访问:http://192.168.1.100:15672/api/index.html
在这里插入图片描述
此时发现当前的15672界面是可以访问到其他页面的

测试当前的5672端口是否可以访问

通过ui界面在node3节点中添加node3Queue的queue

创建消费者:

package com.hy.rabbitmq.connection.cluster.normal;

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

import com.hy.rabbitmq.connection.utils.RabbitMqUtils;
import com.hy.rabbitmq.connection.utils.StringUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.Exchange.DeclareOk;

/**
 * @description 消费者专门消费node2中的消息
 * @author admin
 *
 */
public class ClusterNode2MsgComsumer {
	private static String host = "192.168.1.100";
	private static int port = 5672;
	private static String username = "root";
	private static String password = "root";

	public static void main(String[] args) throws IOException {
		RabbitMqUtils mqUtils = new RabbitMqUtils(host, port , username, password );
		String queueName = "node3Queue";
		Connection connection = mqUtils.getConnection();
		Channel channel = connection.createChannel();
		channel.basicConsume(queueName, true, new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("consumerTag==>" + consumerTag);
				System.out.println("envelope==>" + envelope);
				System.out.println("props==>" + properties);
				System.out.println("消费者node2接收消息==>【" + StringUtils.toString(body) + "】");
			}

		});

	}
}

启动消费者
在这里插入图片描述
启动成功,此时再次查看当前的ui界面的Connection
在这里插入图片描述
停止消费者然后再次启动发现实现了负载均衡
在这里插入图片描述

配置成功!

6.存在的问题

  1. Cannot create pidfile /run/haproxy.pid,表示该文件已近存在了,直接使用rm /run/haproxy.pid即可删除,然后重新启动即可
  2. 启动过程容易出现Starting proxy admin_stats: cannot bind socket [0.0.0.0:8090],只要使用haproxy -f /etc/haproxy/haproxy.cfg,只要页面显示了就表示显示成功
    在这里插入图片描述
  3. 注意在使用5672端口的时候需要使用listen来绑定*:5672,不能使用backend,backend表示的是前端的,注意使用mode为tcp

7.总结

1.安装Haproxy是非常简单的,只需要执行yum install -y haproxy即可完成安装

2.主要就是需要编写当前的haproxy.cfg中的内容,小心5672的端口的编写必须是listen

3.对于使用servie haproxy start不能启动,需要分析日志文件,如果是bind端口的问题只需要使用haproxy - f /etc/haproxy/haproxy.cfg进行启动即可

4.弄了两天终于完成了,感概许多

以上纯属个人见解,如有问题请联本人!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值