日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

RabbitMq集群使用Nginx做负载均衡

發布時間:2024/4/14 Nginx 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RabbitMq集群使用Nginx做负载均衡 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

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

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的RabbitMq集群使用Nginx做负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。