欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.Net使用RabbitMQ即时发消息Demo

程序员文章站 2022-06-24 23:12:50
前言 最近项目要使用rabbitmq,网上已经有很多优秀的文章了,百度百科对rabbitmq阐述也非常明确,建议去看下,还有amqp协议。必须一提的是rabbitmq是由...

前言

最近项目要使用rabbitmq,网上已经有很多优秀的文章了,百度百科对rabbitmq阐述也非常明确,建议去看下,还有amqp协议。必须一提的是rabbitmq是由lshift提供的一个消息队列协议(amqp)的开源实现,由以高性能、健壮以及可伸缩性出名的erlang写成(因此也是继承了这些优点)。

最近参考大神们的博客,自己做了一个rabbitmq即时发消息的demo。下面话不多说了,来一起看看详细的介绍吧。

步骤如下: 

1.使用vs的nuget安装包管理工具安装rabbitmq.client:

.Net使用RabbitMQ即时发消息Demo

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即时发消息Demo

注:在第一步之前,你需要安装rabbitmq客户端,可从http://www.rabbitmq.com/download.html下载,

        但是rabbitmq又是依赖于erlang otp平台,所以,安装rabbitmq之前,需要先从http://www.erlang.org/download.html下载安装erlang

        关于这部分的内容,推荐阅读:

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。