通过随机数Random 和 redis的incr每次增加一的原子性来动态绑定队列
通过随机数(1,4)和redis的incr每次增加一的原子性来动态绑定队列
队列:3个;
pod:40个:
1. 通过随机数 Random().Next(1, 4)轮询每次返回 1,2,3 来达到一个消费端服务POD每次动态的绑定不同的队列
,解决按开发者账号对应的渠道推送消息到queue1,queue2,queue3,消费端一套代码动态的绑定不同的队列消费,
缺点:到pod少时不够均衡
//int i = new Random().Next(1, 4);
//string queue = "DirectExchangeQueue_"+ i;
//string key = "key" + i;
2.通过redis inrc 类型来解决按开发者账号对应的渠道推送消息到queue1,queue2,queue3,消费端一套代码动态轮询均衡的绑定不同的队列消费
demo : -----------------------------------------
01.象通一个交换机的多个对了发消息:
public static void Send()
{
var factory = new ConnectionFactory();
factory.HostName = "10.15.15.32";//RabbitMQ服务在本地运行
factory.UserName = "admin";//用户名
factory.Password = "xxx2021";//密码
using (var connection = factory.CreateConnection())
{
using (IModel channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "DirectExchangeQueue_1", durable: true, exclusive: false, autoDelete: false, arguments: null);
channel.QueueDeclare(queue: "DirectExchangeQueue_2", durable: true, exclusive: false, autoDelete: false, arguments: null);
channel.QueueDeclare(queue: "DirectExchangeQueue_3", durable: true, exclusive: false, autoDelete: false, arguments: null);
channel.ExchangeDeclare(exchange: "DirectExChange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
var key1 = "key1"; var key2 = "key2"; var key3 = "key3";
channel.QueueBind(queue: "DirectExchangeQueue_1",
exchange: "DirectExChange",
routingKey: key1);
channel.QueueBind(queue: "DirectExchangeQueue_2",
exchange: "DirectExChange",
routingKey: key2);
channel.QueueBind(queue: "DirectExchangeQueue_3",
exchange: "DirectExChange",
routingKey: key3);
for (int i = 1; i < 30000; i++)
{
//开发者1--发消息到-队列1
channel.BasicPublish(exchange: "DirectExChange",
routingKey: key1,
basicProperties: null,
body: Encoding.UTF8.GetBytes("Queue_1:" + i));
//开发者2--发消息到-队列2
channel.BasicPublish(exchange: "DirectExChange",
routingKey: key2,
basicProperties: null,
body: Encoding.UTF8.GetBytes("Queue_2:" + i));
//开发者3--发消息到-队列3
channel.BasicPublish(exchange: "DirectExChange",
routingKey: key3,
basicProperties: null,
body: Encoding.UTF8.GetBytes("Queue_3:" + i));
Console.WriteLine($"已发送消息:~~" + i);
}
}
}
}
02 消费端 1--------------------------
//测试 动态均衡的绑定3个队列,消费消息
int queue_no = 0;
string rdisKey = CSRedisHelper.Get("meicardolare_incr_numer");
if (!string.IsNullOrEmpty(rdisKey)) queue_no = int.Parse(rdisKey);
if (queue_no > 200000000) CSRedisHelper.Del(new string[] { "meicardolare_incr_numer" });
string queue = "";
string key = "";
if (queue_no % 3 == 0)
{
queue = "DirectExchangeQueue_1";
key = "key1";
}
else if (queue_no % 3 == 1)
{
queue = "DirectExchangeQueue_2";
key = "key2";
}
else
{
queue = "DirectExchangeQueue_3";
key = "key3";
}
Console.WriteLine("rdisKey:" + queue_no + ";queue:" + queue + ";key:" + key);
CSRedisHelper.SetIncr("meicardolare_incr_numer"); //每次向 key = meicardolare_incr_numer 增加1
DirectExchangeConsumerLogAll.TestManyQueue(key, queue);
02-2消费端 2---------------------
public static void TestManyQueue(string key,string queue)
{
var factory = new ConnectionFactory();
factory.HostName = "10.15.15.32";//RabbitMQ服务在本地运行
factory.UserName = "admin";//用户名
factory.Password = "xxx2021";//密码
factory.Port = 5672;
using (var connection = factory.CreateConnection())
{
using (IModel channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
channel.ExchangeDeclare(exchange: "DirectExChange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
channel.QueueBind(queue: queue,
exchange: "DirectExChange",
routingKey: key);
//消费队列中的所有消息;
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body.ToArray());
Console.WriteLine($"【{message}】,消费消息~~");
};
//处理消息
channel.BasicConsume(queue: queue,
autoAck: true,
consumer: consumer);
Console.ReadLine();
}
}
}