.Net Core中使用RabbitMQ
(1)、引入依赖
RabbitMQ.Client
(2)、编写发布者代码
1 var connectionFactory = new ConnectionFactory() { HostName="192.168.205.128",UserName="guest",Password="guest"}; //创建连接工厂 2 var connection = connectionFactory.CreateConnection(); //创建connection 3 var channel = connection.CreateModel(); //创建channel 4 //声明交换机 5 //String exchange, 交换机名称 6 //String type, 交换机类型 7 //Boolean durable, 是否持久化 8 //Boolean autoDelete, 是否自动删除 9 //IDictionary< String, Object > arguments 交换机参数 10 channel.ExchangeDeclare("netExchange", ExchangeType.Direct, true, false, null); 11 //声明队列 12 //String queue, 队列名称 13 //Boolean durable, 是否持久化 14 //Boolean exclusive, 是否专有的(排外) 15 //Boolean autoDelete, 是否自动删除 16 //IDictionary<String, Object> arguments 队列参数 17 channel.QueueDeclare("netQueue", true, false, false, null); 18 //将队列绑定到交换机上 19 //String queue, 队列名称 20 //String exchange, 交换机名称 21 //String routingKey, routingKey 22 //IDictionary< String, Object > arguments 绑定参数 23 channel.QueueBind("netQueue", "netExchange", "netMessage", null); 24 //发布消息 25 //String exchange, 交换机名称 26 //String routingKey, routingKey 27 //IBasicProperties basicProperties, 发布属性 28 //Byte[] body 消息内容 29 channel.BasicPublish("netExchange", "netMessage", null, Encoding.UTF8.GetBytes("来自.net的问候")); 30 Console.ReadKey();
(3)、编写消费者代码
1 var connectionFactory = new ConnectionFactory() { HostName = "192.168.205.128", UserName = "guest", Password = "guest" }; //创建连接工厂 2 var connection = connectionFactory.CreateConnection(); //创建connection 3 var channel = connection.CreateModel(); //创建channel 4 //声明交换机 5 //String exchange, 交换机名称 6 //String type, 交换机类型 7 //Boolean durable, 是否持久化 8 //Boolean autoDelete, 是否自动删除 9 //IDictionary< String, Object > arguments 交换机参数 10 channel.ExchangeDeclare("netExchange", ExchangeType.Direct, true, false, null); 11 //声明队列 12 //String queue, 队列名称 13 //Boolean durable, 是否持久化 14 //Boolean exclusive, 是否专有的(排外) 15 //Boolean autoDelete, 是否自动删除 16 //IDictionary<String, Object> arguments 队列参数 17 channel.QueueDeclare("netQueue", true, false, false, null); 18 //将队列绑定到交换机上 19 //String queue, 队列名称 20 //String exchange, 交换机名称 21 //String routingKey, routingKey 22 //IDictionary< String, Object > arguments 绑定参数 23 channel.QueueBind("netQueue", "netExchange", "netMessage", null); 24 //1.直接获取消息 25 //var result = channel.BasicGet("netQueue", true); 26 //Console.WriteLine(Encoding.UTF8.GetString(result.Body)); 27 //2.使用事件机制获取消息 28 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); 29 consumer.Received += (sender, e) => 30 { 31 Console.WriteLine(Encoding.UTF8.GetString(e.Body)); 32 }; 33 channel.BasicConsume("netQueue", true, consumer); 34 Console.ReadKey();
作者:奇
出处:https://www.cnblogs.com/fanqisoft/p/10388793.html
版权:本作品采用「本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!