Hyz.RabbitMQ.Client
0.0.5
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Hyz.RabbitMQ.Client --version 0.0.5
NuGet\Install-Package Hyz.RabbitMQ.Client -Version 0.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hyz.RabbitMQ.Client" Version="0.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hyz.RabbitMQ.Client" Version="0.0.5" />
<PackageReference Include="Hyz.RabbitMQ.Client" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hyz.RabbitMQ.Client --version 0.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hyz.RabbitMQ.Client, 0.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hyz.RabbitMQ.Client@0.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hyz.RabbitMQ.Client&version=0.0.5
#tool nuget:?package=Hyz.RabbitMQ.Client&version=0.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Hyz.RabbitMQ.Client
一款统一、优雅的 RabbitMQ 客户端库,专为 .NET 打造。
一个包,零模板代码。
特性
- 🚀 一行注册 —
AddRabbitMq()即可完成所有配置 - 📮 发布与消费 —
IPublisherService/IConsumerService开箱即用 - 🔁 批量处理 —
PublishBatchAsync/ConsumeBatchAsync,支持自定义批次大小和超时 - 🌐 多连接管理 — 命名连接,轻松接入多节点 RabbitMQ 集群
- 🧩 源码生成器 — 基于特性声明队列/交换机/绑定(编译时生成)
- 🔍 订阅者扫描 — 自动发现程序集中标记了
[RabbitMqConsumer]的处理器 - 🎯 IAsyncEnumerable — 现代化
await foreach消费方式,告别回调地狱 - 🔄 自动重连 — 内置指数/线性/固定退避策略
- ✅ 发布确认 —
PublishWithConfirmationAsync确保消息可靠投递
安装
dotnet add package Hyz.RabbitMQ.Client
需要 .NET 8.0 或更高版本。
快速开始
1. 注册服务
using Hyz.RabbitMQ.Extensions;
builder.Services.AddRabbitMq(options =>
{
options.HostName = "localhost";
options.Port = 5672;
options.UserName = "guest";
options.Password = "guest";
options.AutoReconnect = true;
});
2. 发布消息
var publisher = sp.GetRequiredService<IPublisherService>();
var message = new MessageBody(Encoding.UTF8.GetBytes("Hello RabbitMQ!"));
await publisher.PublishAsync("my-queue", message);
3. 消费消息
var consumer = sp.GetRequiredService<IConsumerService>();
await foreach (var msg in consumer.ConsumeAsync("my-queue"))
{
var text = Encoding.UTF8.GetString(msg.Body);
Console.WriteLine($"收到消息: {text}");
await msg.AckAsync();
}
核心概念
发布者 (Publisher)
| API | 说明 |
|---|---|
PublishAsync(queue, message) |
发布消息到队列 |
PublishToExchangeAsync(exchange, routingKey, message) |
发布消息到交换机 |
PublishBatchAsync(exchange, routingKey, messages) |
批量发布(优化性能) |
PublishWithConfirmationAsync(...) |
带 Broker 确认的发布 |
消费者 (Consumer)
| API | 说明 |
|---|---|
ConsumeAsync(queue) → IAsyncEnumerable |
异步流式消费 |
ConsumeBatchAsync(queue, batchSize, timeoutMs) |
批量消费 |
StartConsumingAsync(queue, handler) |
回调方式消费 |
StartBatchConsumingAsync(queue, ...) |
回调方式批量消费 |
MessageBody
var body1 = new MessageBody(bytes);
var body2 = "text".ToMessageBody();
var body3 = new MessageBody(myObject, serializer);
ConsumerOptions
| 属性 | 默认值 | 说明 |
|---|---|---|
ConsumerTag |
null |
消费者标识 |
AutoAck |
false |
是否自动确认 |
PrefetchCount |
10 |
预取数量 |
Exclusive |
false |
独占消费者 |
Priority |
0 |
消费者优先级 |
PublishOptions
var options = new PublishOptions
{
DeliveryMode = DeliveryModes.Persistent,
ContentType = "application/json",
CorrelationId = Guid.NewGuid().ToString(),
Priority = 5,
Expiration = "60000" // 60 秒过期
};
多连接管理
// 注册多个命名连接
services.AddRabbitMq("Conn1", opts => opts.HostName = "rabbit1.local");
services.AddRabbitMq("Conn2", opts => opts.HostName = "rabbit2.local");
// 按名称获取服务
var pub1 = sp.GetRequiredKeyedService<IPublisherService>("Conn1");
var pub2 = sp.GetRequiredKeyedService<IPublisherService>("Conn2");
订阅者扫描
[RabbitMqConsumer(Queue = "orders", PrefetchCount = 5)]
public class OrderHandler : IMessageHandler
{
public Task<HandleResult> HandleAsync(ReceivedMessageContext ctx)
{
var text = Encoding.UTF8.GetString(ctx.Body);
Console.WriteLine(text);
return Task.FromResult(HandleResult.Success);
}
}
// 扫描并启动
var host = new RabbitMqSubscriberHost(logger, connectionManager);
host.ScanAndRegister(typeof(OrderHandler).Assembly);
await host.StartAsync();
源码生成器
[RabbitMqExchange(Name = "shop", Type = "direct")]
[RabbitMqQueue(Name = "orders", Durable = true)]
[RabbitMqBinding(Exchange = "shop", RoutingKey = "order.created")]
public static partial class ShopSubscriptions
{
[RabbitMqSubscribe(Queue = "orders")]
public static partial Task OnOrderCreatedAsync(ReceivedMessageContext ctx);
[RabbitMqBatchSubscribe(Queue = "batch-orders", BatchSize = 50)]
public static partial Task OnBatchOrdersAsync(IList<ReceivedMessageContext> batch);
}
依赖项
安装本包时会自动引入以下依赖:
| 包名 | 版本 |
|---|---|
| RabbitMQ.Client | ≥ 7.2.1 |
| MessagePack | ≥ 2.5.187 |
| Microsoft.Extensions.* | ≥ 8.0.0 |
许可证
MIT License - 详见 LICENSE 文件
如果这个项目对你有帮助,请给它一个 ⭐️
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- MessagePack (>= 2.5.187)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- RabbitMQ.Client (>= 7.2.1)
-
net8.0
- MessagePack (>= 2.5.187)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- RabbitMQ.Client (>= 7.2.1)
-
net9.0
- MessagePack (>= 2.5.187)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- RabbitMQ.Client (>= 7.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.