.net core 平台下的RabbitMQ封装:RabbitMQ.Helper
-
初衷:RabbitMQ官方提供的RabbitMQ.Client用起来感觉有点麻烦,遂略微封装一番,希望能提升效率。
-
说明:RabbitMQ.Helper没有做到像EasyNetQ那样的高级封装,仅对RabbitMQ的基于Exchange/RoutingKey/Queue进行的一次封装,延迟队列也实现了封装(不过当前版本延迟队列还在内测中)
-
安装:nuget搜索RabbitMQ.Helper 或 RabbitMQ.Helper.Injection (作者zed)
-
版本:截止发文,1.0.2
RabbitMQ.Helper
- 生产者
//定义连接信息提供器
RabbitMQProvider provider = new RabbitMQProvider("host", 5672, "guest", "guest", "/");
//定义交换器参数
ProducterOptions options = new ProducterOptions()
{
Exchange = "test.exchange",
Mode = ExchangeMode.Direct,
Durable = true,
AutoDelete = false,
Args = null
};
//定义生产者
Producter producter = new Producter(provider, options);
//发布消息
producter.Publish(body: "HelloRabbit", routingKey: "test.exchange.routing");
- 事件消费
ConsumerOptions options = new ConsumerOptions()
{
SubscribeExchange = "test.exchange",
SubscribeRouting = "test.exchange.routing",
SubscribeQueue = "test.exchange.queue"
};
EventConsumer consumer = new EventConsumer(provider, options);
consumer.Subscribe(msg =>
{
Log($"[接收] {msg}");
return true;
});
- 手动拉取消费
ConsumerOptions options = new ConsumerOptions()
{
SubscribeExchange = "test.exchange",
SubscribeRouting = "test.exchange.routing",
SubscribeQueue = "test.exchange.queue"
};
PullConsumer puller = new PullConsumer(provider, options);
string msg = puller.Pull();
Asp.net Core 扩展 :RabbitMQ.Helper.Injection
- 注入
//注入RabbitMQHelper
services.AddRabbitMQHelper(x =>
{
//增加连接信息提供器
x.AddProvider(new RabbitMQProvider("host", 5672, "guest", "guest", "/"));
//通过exchange注入生产者,可选参数option允许自定义配置
x.AddProducter("r.h.asp.text.direct.exchange",option => option.Durable = false);
// [推荐] 通过创建生产者类,泛型注入,与exchange名称注入二选一,或混用
x.AddProducter<DemoProducter>();
});
//注入消费者
services.AddSingleton<DemoConsumer>();
//别忘了,public void Configure(IApplicationBuilder app)中的使用 !!!
app.UseRabbitSubcribes(Assembly.GetExecutingAssembly());
- 通过exchange注入时,在controller/service中获取生产者实例
public class DemoService : IDemoService
{
//RabbitMQ工厂,获取生产者
//该类里维护了一个ConcurrentDictionary,根据exchange名获取Producter实例
private readonly RabbitMQFactory factory;
public DemoService(RabbitMQFactory factory)
{
this.factory = factory;
}
public void TestSend()
{
Producter producter = factory.GetProducter("r.h.asp.text.direct.exchange");
producter.Publish(body: "HelloRabbit", routingKey: "test.exchange.routing");
}
}
- [推荐] 创建生产者类,泛型注入时,在controller/service中获取生产者实例
public class Demo2Service : IDemoService
{
private readonly DemoProducter producter;
public Demo2Service(DemoProducter producter)
{
this.producter = producter;
}
public void TestSend()
{
producter.Publish(body: "HelloRabbit", routingKey: "test.exchange.routing");
}
}
/// <summary>
/// 生产者实例,继承自ProducterProxy
/// </summary>
public class DemoProducter : ProducterProxy
{
public override ProducterOptions Options => new ProducterOptions()
{
Exchange = "r.h.asp.text.direct.exchange",
Durable = false
};
//provider由ioc注入
public DemoProducter(RabbitMQProvider provider) : base(provider)
{
}
}
- 消费者实例
/// <summary>
/// 实现IConsumer(空接口,起到标注作用)
/// </summary>
public class DemoConsumer : IConsumer
{
/// <summary>
/// 特性标注该方法监听某条队列的消息
/// </summary>
/// <param name="user">第一个参数,作为要接收的实体类</param>
[RabbitSubscribe("exchange","routing","queue")]
public void Test(User user)
{
Logger.Info($"[接收] Id={user.Id} Time={user.Time}");
}
/// <summary>
/// 也可写全队列参数
/// </summary>
/// <param name="user"></param>
[RabbitSubscribe(
Exchange = "exchange",
Routing = "routing",
Queue = "queue",
Durable = false,
AutoDelete = false,
Exclusive = false)]
public void Test2(User user)
{
Logger.Info($"[接收] Id={user.Id} Time={user.Time}");
}
}