RabbitMq集群使用Nginx做负载均衡

1.配置rabbitmq集群(可以参考前一篇RabbitMq之部署集群

2.Nginx做负载均衡

注意:Nginx1.90版本后 新增了stream 模块用于一般的 TCP 代理和负载均衡,之前版本不支持

修改Nginx配置文件nginx.conf添加如下配置,监听12345端口

stream { 
    upstream rabbitmqserver { 
        server 192.168.191.20:5672; 
        server 192.168.191.131:5672;
        server 192.168.191.132:5672;
    }

    server { 
        listen 12345; 
        proxy_pass rabbitmqserver; 
    } 
 

修改后重启Nginx,使之生效。

3.测试

获取连接的工具类

package com.sky.study;

import java.io.IOException;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
 * 生成连接
 * @author 86940
 *
 */
public class ConnectionUtil {
    public static Connection getConnection() throws IOException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.191.132");//做Nginx负载均衡的服务器地址
        factory.setPort(12345);//使用Nginx做了tcp负载均衡,监听的是12345端口
        factory.setUsername("admin");
        factory.setPassword("123456");
        factory.setVirtualHost("hello");
        Connection connection = factory.newConnection();
        return connection;
    }
}

生产者

package com.sky.study.helloWorld;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.sky.study.ConnectionUtil;

public class Send {
    private static final String QUEUE_NAME = "q.test.01";

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            // 获取连接
            connection = ConnectionUtil.getConnection();
            // 创建通过
            channel = connection.createChannel();
            // 声明(创建)队列
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            // 消息
            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println("发送成功");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
消费者

package com.sky.study.helloWorld;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.sky.study.ConnectionUtil;
import com.rabbitmq.client.ShutdownSignalException;

public class Recv {
    private static final String QUEUE_NAME = "q.test.01";

    public static void main(String[] args) {
        Connection connection = null;
        Channel channel = null;
        try {
            connection = ConnectionUtil.getConnection();
            channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            // 定义队列的消费者
            QueueingConsumer consumer = new QueueingConsumer(channel);
            // 监听队列
            channel.basicConsume(QUEUE_NAME, true, consumer);
            // 获取消息
            while (true) {
                Delivery nextDelivery = consumer.nextDelivery();
                String message = new String(nextDelivery.getBody());
                System.out.println(message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ShutdownSignalException e) {
            e.printStackTrace();
        } catch (ConsumerCancelledException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 

结束,谢谢支持

 

 

 

转载于:https://my.oschina.net/sky2008/blog/2248918

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值