QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 3375|回复: 0

[ASP/.NET] EasyNetQ操作RabbitMQ

[复制链接]

等级头衔

积分成就    金币 : 2806
   泡泡 : 1516
   精华 : 6
   在线时间 : 1244 小时
   最后登录 : 2024-5-5

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-12-7 11:52:51 | 显示全部楼层 |阅读模式
       EasyNetQ 是一个容易使用,专门针对RabbitMQ的 .NET API。EasyNetQ是为了提供一个尽可能简洁的适用与RabbitMQ的.NET类库,下面看下怎么集成。
4 U* g; a) T% L, h) ]1 {1、nuget 安装
0 @9 `0 Z) v( q2 c4 {) F 1.jpg " R& M+ ]/ {& y- q! y5 Q
2、配置连接串9 W$ {. ]4 E: N5 n8 r( h
  1.    public static IBus CreateMessageBus()
  2.         {
  3.             // 消息服务器连接字符串
  4.             var connectionString = ConfigurationManager.ConnectionStrings["RabbitMQConnString"];
  5.                  
  6.             if (connectionString == null || connectionString.ConnectionString == string.Empty)
  7.             {
  8.                 throw new Exception("messageserver connection string is missing or empty");
  9.             }
  10.             return RabbitHutch.CreateBus(connectionString.ConnectionString);
  11.         }
3、这边我们构建一个消息体# @7 X6 {/ o9 h; w9 A1 H
  1. /// <summary>
  2.     /// 消息类实体
  3.     /// </summary>
  4.     [Serializable]
  5.     public class RabbitMQ_Message
  6.     {
  7.         public RabbitMQ_Message()
  8.         {
  9.             MessageID = DateTime.Now.Ticks.ToString();
  10.         }
  11.         /// <summary>
  12.         /// 消息id
  13.         /// </summary>
  14.         public string MessageID { get; set; }
  15.         /// <summary>
  16.         /// 消息标题
  17.         /// </summary>
  18.         public string MessageTitle { get; set; }
  19.         /// <summary>
  20.         /// 消息内容
  21.         /// </summary>
  22.         public string MessageBody { get; set; }
  23.         /// <summary>
  24.         /// 消息管道
  25.         /// </summary>
  26.         public RabbitMessageRouterEnum MessageRouter { get; set; }
  27.         /// <summary>
  28.         /// 游客id
  29.         /// </summary>
  30.         public int customerId { get; set; }
  31.         /// <summary>
  32.         /// 标示代码 0:正确
  33.         /// </summary>
  34.         public ResponseStatus result { get; set; }
  35.         /// <summary>
  36.         /// 消息类型
  37.         /// </summary>
  38.         public SuperSocketMessageTypeEnum superSocketMessageType { get; set; }
  39.         /// <summary>
  40.         /// 消息过期时间(毫秒)
  41.         /// </summary>
  42.         public int expiredMillSeconds { get; set; }
  43.     }
4、发送消息0 v9 z! G7 V* N
  1.   /// <summary>
  2.         /// 发送消息
  3.         /// </summary>
  4.         public static void Publish(RabbitMQ_Message msg)
  5.         {
  6.             //// 创建消息bus
  7.             IBus bus = null;
  8.             try
  9.             {
  10.                 //// 创建消息bus
  11.                 bus = BusBuilder.CreateMessageBus();
  12.                 bus.Publish(msg, x =>
  13.                 {
  14.                     x.WithTopic($"{msg.MessageRouter.ToDescription()}.{msg.customerId}");
  15.                     if (msg.expiredMillSeconds > 0)
  16.                     {
  17.                         x.WithExpires(msg.expiredMillSeconds);
  18.                     }
  19.                 });//通过管道发送消息               
  20.                 LogExtention.getInstance().WriteCustomLogAsync(msg, "RabbitMQ消息发送", "MQHelperPublish");
  21.             }
  22.             catch (EasyNetQException ex)
  23.             {
  24.                 LogExtention.getInstance().ErrorAsync(ex, "RabbitMQ--MQHelper--Publish发布消息时出错");
  25.                 //处理连接消息服务器异常
  26.             }
  27.             finally
  28.             {
  29.                 if (bus != null)
  30.                 {
  31.                     bus.Dispose();//与数据库connection类似,使用后记得销毁bus对象
  32.                 }               
  33.             }
  34.         }
4、接收消息
' h7 N8 J5 n) a' S
  1.     /// <summary>
  2.         /// 接收消息
  3.         /// </summary>
  4.         /// <param name="msg"></param>
  5.         public static ISubscriptionResult Subscribe(RabbitMQ_Message msg, IProcessMessage ipro)
  6.         {
  7.             //// 创建消息bus
  8.             IBus bus = null;
  9.             try
  10.             {
  11.                 bus = BusBuilder.CreateMessageBus();
  12.                 //subscriptionId设置不同的话,每一个subscriptionId都会收到相同的消息,下面的写法只会有一个接收者
  13.                 var subscriptionResult = bus.Subscribe<RabbitMQ_Message>(msg.MessageRouter.ToDescription(), message => ipro.ProcessMsg(message),
  14.                     x => x.WithQueueName(msg.customerId.ToString()).WithTopic($"{msg.MessageRouter.ToDescription()}.{msg.customerId}"));
  15.                 //subscriptionResult.Dispose();//取消订阅
  16.                 return subscriptionResult;
  17.             }
  18.             catch (EasyNetQException ex)
  19.             {
  20.                 LogExtention.getInstance().ErrorAsync(ex, "RabbitMQ--MQHelper--Subscribe订阅消息时出错");
  21.                 //处理连接消息服务器异常
  22.             }
  23.             finally
  24.             {
  25.                 if (bus != null)
  26.                 {
  27.                     bus.Dispose();//与数据库connection类似,使用后记得销毁bus对象
  28.                 }
  29.             }
  30.             return null;
  31.         }
2.jpg
+ I+ P9 L: h8 ]7 U: c, u. |
       在EasyNetQ中如果需要生产者确认功能,则需要在Rabbitmq的连接配置中设置publisherConfirms=true,这将会开启自动确认。在使用高级api定义交换机和队列时可以自己定义多种参数,比如消息是否持久化,消息最大长度等等。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-9 06:49

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表