前言
最近项目要使用RabbitMQ,网上已经有很多优秀的文章了,百度百科对RabbitMQ阐述也非常明确,建议去看下,还有amqp协议。必须一提的是rabbitmq是由LShift提供的一个消息队列协议(AMQP)的开源实现,由以高性能、健壮以及可伸缩性出名的Erlang写成(因此也是继承了这些优点)。
最近参考大神们的博客,自己做了一个RabbitMQ即时发消息的Demo。下面话不多说了,来一起看看详细的介绍吧。
步骤如下:
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.程序结果:
注:在第一步之前,你需要安装RabbitMQ客户端,可从http://www.rabbitmq.com/download.html下载,
但是RabbitMQ又是依赖于Erlang OTP平台,所以,安装RabbitMQ之前,需要先从http://www.erlang.org/download.html下载安装erlang
关于这部分的内容,推荐阅读:https://www.jb51.net/article/143499.htm
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 杨泰瑞《City Boy》[320K/MP3][28.71MB]
- 胡彦斌《失业情歌》首版[WAV+CUE]
- 杨泰瑞《City Boy》[FLAC/分轨][159.66MB]
- APM亚流新世代《一起冒险》[320K/MP3][68.66MB]
- FIM《天伦乐》(DaydreamsAndLullabies)24K金碟[WAV+CUE]
- 【雨果唱片】中国管弦乐《鹿回头》
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]