1.RabbitMQ NUget .Net客户端4.1.1版本 对应的 .netframework版本是 4.5.1 以及以上
2.本地实现的效果图
3.实践代码:
P-生产者:
using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RabbitMQClient { /// <summary> /// RabbitMQ的Topic /// </summary> class Program { private readonly static ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "---", Password = "514600", Port = 5672 }; /// <summary> /// 路由名称 /// </summary> const string TopExchangeName = "topic.justin.exchange"; const string TopQueueName = "topic.justin.queue"; public static void TopicExchangeSendMsg() { using(IConnection conn=rabbitMqFactory.CreateConnection()) { using(IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(TopExchangeName,"topic",durable:true,autoDelete:false,arguments:null); channel.QueueDeclare(TopQueueName,durable:true,autoDelete:false,exclusive:false,arguments:null); channel.QueueBind(TopQueueName,TopExchangeName,routingKey:TopQueueName); Console.WriteLine("请输入要发送的消息!"); string vadata = Console.ReadLine(); while(vadata!="exit") { var msgBody = Encoding.UTF8.GetBytes(vadata); channel.BasicPublish(exchange:TopExchangeName,routingKey:TopQueueName,basicProperties:null,body:msgBody); Console.WriteLine(string.Format("**发送时间:{0},发送完成,输入exit退出消息发送",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } } static void Main(string[] args) { TopicExchangeSendMsg(); } } }
C-消费者代码:
using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RabbitMQClient_C { /// <summary> /// Customer接受消息代码 /// </summary> class Program { private readonly static ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName="127.0.0.1", UserName="---",Password="514600",Port=5672 }; /// <summary> /// 路由名称 /// </summary> const string TopExchangeName = "topic.justin.exchange"; /// <summary> /// 队列名称 /// </summary> const string TopQueueName = "topic.justin.queue"; public static void TopicAcceptExchange() { using(IConnection conn = rabbitMqFactory.CreateConnection()) { using(IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(TopExchangeName,"topic",durable:true,autoDelete:false,arguments:null); channel.QueueDeclare(TopQueueName,durable:true,autoDelete:false,exclusive:false,arguments:null); channel.BasicQos(prefetchSize:0,prefetchCount:1,global:false);//作用: channel.QueueBind(TopQueueName,TopExchangeName,routingKey:TopQueueName); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var msgBody = Encoding.UTF8.GetString(ea.Body); Console.WriteLine(string.Format("**接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); int dots = msgBody.Split(‘.‘).Length -1; System.Threading.Thread.Sleep(dots*1000); Console.WriteLine(" [x] Done"); channel.BasicAck(deliveryTag:ea.DeliveryTag,multiple:false); }; channel.BasicConsume(TopQueueName,noAck:false,consumer:consumer); Console.WriteLine("按任意键,退出程序"); Console.ReadLine(); } } } static void Main(string[] args) { TopicAcceptExchange(); } } }
时间: 2024-10-05 23:27:29