您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
怎么在.Net中使用RabbitMQ发送即时消息?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
步骤如下:
1.使用VS的NuGet安装包管理工具安装RabbitMQ.Client:
2.生产者端代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RabbitMQ.Client; namespace RabbitMQ.Producter { class Program { /// <summary> /// 连接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName="localhost", UserName = "guest", Password = "guest", Port = 5672, //VirtualHost = "JentVirtualHost" }; /// <summary> /// 路由名称 /// </summary> const string ExchangeName = "Jent.Exchange"; /// <summary> /// 队列名称 /// </summary> const string QueueName = "Jent.Queue"; static void Main(string[] args) { DirectExchangeSendMsg(); Console.WriteLine("按任意键退出程序!"); Console.ReadKey(); } /// <summary> /// 单点精确路由模式 /// </summary> private static void DirectExchangeSendMsg() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); var props = channel.CreateBasicProperties(); props.Persistent = true; Console.WriteLine("请输入需要发送的消息:"); string vadata = Console.ReadLine(); while (vadata != "exit") { var msgBody = Encoding.UTF8.GetBytes(vadata); channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: msgBody); Console.WriteLine(string.Format("发送时间:{0},发送完毕,输入exit退出消息发送", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); vadata = Console.ReadLine(); } } } } } }
3.消费者端代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using RabbitMQ.Client; namespace RabbitMQ.Consumer { class Program { /// <summary> /// 连接配置 /// </summary> private static readonly ConnectionFactory rabbitMqFactory = new ConnectionFactory() { HostName = "127.0.0.1", UserName = "guest", Password = "guest", Port = 5672, //VirtualHost = "JentVirtualHost" }; /// <summary> /// 路由名称 /// </summary> const string ExchangeName = "Jent.Exchange"; /// <summary> /// 队列名称 /// </summary> const string QueueName = "Jent.Queue"; static void Main(string[] args) { DirectAcceptExchange(); Console.WriteLine("输入任意值退出程序!"); Console.ReadKey(); } private static void DirectAcceptExchange() { using (IConnection conn = rabbitMqFactory.CreateConnection()) { using (IModel channel = conn.CreateModel()) { channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); while (true) { BasicGetResult msgResponse = channel.BasicGet(QueueName, autoAck: false); if (msgResponse != null) { var msgBody = Encoding.UTF8.GetString(msgResponse.Body); Console.WriteLine(string.Format("接收时间:{0},消息内容:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody)); } //System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); } } } } } }
4.程序结果:
看完上述内容,你们掌握怎么在.Net中使用RabbitMQ发送即时消息的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。