RabbitMq集群使用Nginx做负载均衡
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1.配置rabbitmq集群(可以參考前一篇RabbitMq之部署集群)
2.Nginx做負(fù)載均衡
注意:Nginx1.90版本后?新增了stream 模塊用于一般的 TCP 代理和負(fù)載均衡,之前版本不支持
修改Nginx配置文件nginx.conf添加如下配置,監(jiān)聽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負(fù)載均衡的服務(wù)器地址
?? ??? ?factory.setPort(12345);//使用Nginx做了tcp負(fù)載均衡,監(jiān)聽的是12345端口
?? ??? ?factory.setUsername("admin");
?? ??? ?factory.setPassword("123456");
?? ??? ?factory.setVirtualHost("hello");
?? ??? ?Connection connection = factory.newConnection();
?? ??? ?return connection;
?? ?}
}
生產(chǎn)者
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();
?? ??? ??? ?// 創(chuàng)建通過
?? ??? ??? ?channel = connection.createChannel();
?? ??? ??? ?// 聲明(創(chuàng)建)隊(duì)列
?? ??? ??? ?channel.queueDeclare(QUEUE_NAME, false, false, false, null);
?? ??? ??? ?// 消息
?? ??? ??? ?String message = "Hello World!";
?? ??? ??? ?channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
?? ??? ??? ?System.out.println("發(fā)送成功");
?? ??? ?} 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();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
}
消費(fèi)者
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);
?? ??? ??? ?// 定義隊(duì)列的消費(fèi)者
?? ??? ??? ?QueueingConsumer consumer = new QueueingConsumer(channel);
?? ??? ??? ?// 監(jiān)聽隊(duì)列
?? ??? ??? ?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();
?? ??? ?}
?? ?}
}
?
結(jié)束,謝謝支持
?
?
?
轉(zhuǎn)載于:https://my.oschina.net/sky2008/blog/2248918
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的RabbitMq集群使用Nginx做负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql学习-初识mysql
- 下一篇: 自动化运维之 安装部署 Ansible