private void Subscriber() { //〇 客户端和服务器端建立连接时需要将自己的身份ID和服务器ID写到数据库,断开连接时删除记录 //〇 每个服务上线时,根据服务唯一ID在Redis上订阅对应的Topic //① 小明发送给小红的消息先被传输到服务A //② 服务A收到消息从数据库中查到小红所连接的服务器是服务B //③ 服务A将小明的消息转发到Redis中服务B的Topic //④ 服务B收到Redis发来的消息 //⑤ 服务B将消息发送给小红 var serverList = Commont.RedisCommont.StackExchangeRedisHelper.redisClient.GetStringKey <List <string> >("ServerList"); if (serverList == null) { serverList = new List <string>(); } ServerId = "722eb960-93f8-422d-b9bb-cd4b9fa78b59";// Guid.NewGuid().ToString(); if (!serverList.Contains(ServerId)) { serverList.Add(ServerId); foreach (var item in serverList) { RabbitMQProducterHelper.CreateInstance().SetExchangQueueName("ProjectDemo", $"Server_topic_{item}", "topic"); RabbitMQConsumerHelper.CreateInstance().ConsumerLogicDelegate += Commont.WebSocketCommont.WebSocketHelper.ConsumerLogicDel; RabbitMQConsumerHelper.CreateInstance().SetExchangQueueName("ProjectDemo", $"Server_topic_{item}", "topic"); } Commont.RedisCommont.StackExchangeRedisHelper.redisClient.SetStringKey("ServerList", JsonConvert.SerializeObject(serverList));//先用最简单的 } }
/// <summary> /// 消费者测试 /// </summary> /// <returns></returns> public ActionResult Index2(int i = 1) { switch (i) { case 1: RabbitMQConsumerHelper.接收队列消息(); break; case 2: RabbitMQConsumerHelper.接收Direct交换机(); break; case 3: RabbitMQConsumerHelper.接收Topic交换机(); break; case 4: RabbitMQConsumerHelper.接收Fanout交换机(); break; case 5: RabbitMQConsumerHelper.消费负载均衡(); break; case 6: RabbitMQConsumerHelper.接收交换机消息2(); break; } return(View()); }