Hyz.RabbitMQ.Client
0.0.7
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
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.7
NuGet\Install-Package Hyz.RabbitMQ.Client -Version 0.0.7
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.7" />
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.7" />
<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.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Hyz.RabbitMQ.Client, 0.0.7"
#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.7
#: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.7
#tool nuget:?package=Hyz.RabbitMQ.Client&version=0.0.7
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 Framework 4.6.1+, .NET Core 2.0+, .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 秒过期
};
序列化
库内置 SystemTextJsonSerializer(默认)和 MessagePackSerializer,并可自定义序列化器。
using Hyz.RabbitMQ.Serialization;
// 对象 → MessageBody(默认 JSON)
var body = myOrder.ToMessageBody();
// 对象 → MessageBody(使用 MessagePack)
var msgPackSerializer = new MessagePackSerializer();
var body = myOrder.ToMessageBody(msgPackSerializer);
// MessageBody → 对象
var order = body.FromMessageBody<Order>();
// 字节数组 → 对象
var order = bytes.FromMessageBody<Order>();
// 字符串 ↔ MessageBody
var body = "hello".ToMessageBodyFromString();
var text = body.ToStringContent();
自定义序列化器
实现 IMessageSerializer 接口即可:
public class ProtobufSerializer : IMessageSerializer
{
public string ContentType => "application/x-protobuf";
public ReadOnlyMemory<byte> Serialize<T>(T obj) where T : class => /* ... */;
public T? Deserialize<T>(ReadOnlyMemory<byte> bytes) where T : class => /* ... */;
}
消息处理
ReceivedMessageContext
消费者回调中接收的消息上下文,包含完整的消息元数据:
| 属性 | 类型 | 说明 |
|---|---|---|
Body |
ReadOnlyMemory<byte> |
消息体原始字节 |
MessageId |
string? |
消息唯一标识 |
RoutingKey |
string |
消息路由键 |
ExchangeName |
string |
来源交换机 |
QueueName |
string |
来源队列 |
DeliveryTag |
ulong |
RabbitMQ 投递序号(用于 Ack/Nack) |
Headers |
IDictionary<string, object?>? |
消息头 |
ContentType |
string? |
内容类型 |
Redelivered |
bool |
是否为重新投递 |
CorrelationId |
string? |
关联 ID(RPC 模式) |
ReplyTo |
string? |
回复队列(RPC 模式) |
Priority |
byte? |
消息优先级 |
Timestamp |
AmqpTimestamp? |
消息时间戳 |
Expiration |
string? |
消息过期时间 |
消息确认
await foreach (var msg in consumer.ConsumeAsync("orders"))
{
try
{
ProcessMessage(msg);
await msg.AckAsync(); // 确认处理成功
}
catch
{
await msg.NackAsync(true); // 拒绝并重新入队
}
}
消息处理器接口
// 单条消息处理
public class OrderHandler : IMessageHandler
{
public Task<HandleResult> HandleAsync(ReceivedMessageContext ctx, CancellationToken ct)
{
var order = ctx.Body.FromMessageBody<Order>();
// 处理订单...
return Task.FromResult(HandleResult.SuccessResult);
}
}
// 批量消息处理
public class BatchOrderHandler : IBatchMessageHandler
{
public Task<BatchHandleResult> HandleBatchAsync(
IReadOnlyList<ReceivedMessageContext> messages, CancellationToken ct)
{
var orders = messages.Select(m => m.Body.FromMessageBody<Order>()).ToList();
// 批量处理...
return Task.FromResult(BatchHandleResult.AllSuccess(orders.Count));
}
}
HandleResult 结果类型
| 静态方法 | 说明 |
|---|---|
HandleResult.SuccessResult |
处理成功,消息确认 |
HandleResult.Reject(error) |
处理失败,消息丢弃(不进死信则可能丢失) |
HandleResult.Retry(error, count) |
处理失败,消息重新入队重试 |
连接配置详解
RabbitMqConnectionOptions 支持所有常用 RabbitMQ 连接参数:
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
Name |
string? |
null |
连接名称(用于多连接场景) |
HostName |
string |
"localhost" |
RabbitMQ 服务器地址 |
Port |
int |
5672 |
端口号 |
UserName |
string |
"guest" |
登录用户名 |
Password |
string |
"guest" |
登录密码 |
VirtualHost |
string |
"/" |
虚拟主机 |
Heartbeat |
ushort |
60 |
心跳间隔(秒) |
ConnectionTimeout |
int |
30000 |
连接超时(毫秒) |
AutoReconnect |
bool |
true |
是否自动重连 |
MaxRetryCount |
int |
3 |
最大重试次数 |
RetryDelayMs |
int |
5000 |
重试基础间隔(毫秒) |
BackoffStrategy |
RetryBackoffStrategy |
Exponential |
重试退避策略 |
MinRetryDelayMs |
int |
1000 |
指数退避最小间隔 |
MaxRetryDelayMs |
int |
30000 |
指数退避最大间隔 |
EnableTls |
bool |
false |
是否启用 TLS 加密 |
TlsOptions |
TlsOptions? |
null |
TLS 配置(证书路径等) |
重连策略
| 策略 | 说明 |
|---|---|
Fixed |
固定间隔重试(每次等待 RetryDelayMs 毫秒) |
Linear |
线性递增(第 N 次重试等待 N × RetryDelayMs 毫秒) |
Exponential |
指数退避(每次间隔翻倍,受 MinRetryDelayMs / MaxRetryDelayMs 限制) |
TLS 配置
services.AddRabbitMq(options =>
{
options.HostName = "rabbitmq.example.com";
options.Port = 5671;
options.EnableTls = true;
options.TlsOptions = new TlsOptions
{
CertPath = "/path/to/client.p12",
CertPassphrase = "your-password",
CheckCertificateRevocation = true
};
});
多连接管理
// 注册多个命名连接
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();
源码生成器
在包含生成代码的 partial 类上通过特性声明交换机、队列和绑定关系,编译器自动生成注册代码。
声明交换机、队列和绑定
using Hyz.RabbitMQ.Abstractions.Attributes;
[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);
}
特性详解
[RabbitMqExchange] — 声明交换机
| 属性 | 默认值 | 说明 |
|---|---|---|
Name |
(required) | 交换机名称 |
Type |
"Direct" |
交换机类型(Direct / Fanout / Topic / Headers) |
Durable |
true |
是否持久化 |
AutoDelete |
false |
是否自动删除 |
Arguments |
null |
额外参数(JSON 格式) |
[RabbitMqQueue] — 声明队列
| 属性 | 默认值 | 说明 |
|---|---|---|
Name |
(required) | 队列名称 |
Durable |
true |
是否持久化 |
Exclusive |
false |
是否独占 |
AutoDelete |
false |
是否自动删除 |
MessageTtl |
null |
消息 TTL(毫秒) |
MaxLength |
null |
最大队列长度 |
DeadLetterExchange |
null |
死信交换机 |
DeadLetterRoutingKey |
null |
死信路由键 |
[RabbitMqBinding] — 声明绑定
| 属性 | 默认值 | 说明 |
|---|---|---|
Exchange |
(required) | 交换机名称 |
RoutingKey |
(required) | 路由键(支持通配符 * #) |
QueueName |
null |
队列名(不填则使用类上声明的队列) |
[RabbitMqSubscribe] — 单条订阅方法
| 属性 | 默认值 | 说明 |
|---|---|---|
Queue |
(required) | 队列名称 |
Exchange |
null |
交换机名称 |
RoutingKey |
null |
路由键 |
ConnectionName |
null |
连接名称(默认连接) |
AutoAck |
false |
自动确认 |
PrefetchCount |
10 |
预取数量 |
Durable |
true |
持久化 |
MaxRetryCount |
3 |
最大重试次数 |
DeadLetterExchange |
null |
死信交换机 |
DeadLetterRoutingKey |
null |
死信路由键 |
UseDedicatedThread |
true |
使用独立线程 |
ThreadName |
null |
线程名称 |
StartupPriority |
0 |
启动优先级 |
[RabbitMqBatchSubscribe] — 批量订阅方法
| 属性 | 默认值 | 说明 |
|---|---|---|
Queue |
(required) | 队列名称 |
BatchSize |
10 |
批量大小 |
BatchTimeoutMs |
1000 |
批次超时(毫秒) |
Exchange |
null |
交换机名称 |
RoutingKey |
null |
路由键 |
ConnectionName |
null |
连接名称 |
PrefetchCount |
50 |
预取数量 |
高级特性
死信队列 (DLX)
当消息处理失败(达到最大重试次数)或被拒绝(requeue = false)时,可自动路由到死信队列:
// 订阅者扫描方式
[RabbitMqConsumer(
Queue = "orders",
MaxRetryCount = 3,
DeadLetterExchange = "shop.dlx",
DeadLetterRoutingKey = "order.failed")]
public class OrderHandler : IMessageHandler { /* ... */ }
// 源码生成器方式
[RabbitMqQueue(
Name = "orders",
DeadLetterExchange = "shop.dlx",
DeadLetterRoutingKey = "order.failed")]
[RabbitMqExchange(Name = "shop.dlx", Type = "topic")]
[RabbitMqBinding(Exchange = "shop.dlx", RoutingKey = "order.failed")]
public static partial class ShopDlxSubscriptions { /* ... */ }
RPC 请求/响应模式
利用 CorrelationId 和 ReplyTo 实现远程过程调用:
// 客户端 — 发送 RPC 请求
var options = new PublishOptions
{
CorrelationId = Guid.NewGuid().ToString(),
ReplyTo = "rpc.reply.queue"
};
await publisher.PublishToExchangeAsync("rpc.exchange", "rpc.method", request, options);
// 服务端 — 处理并回复
await foreach (var msg in consumer.ConsumeAsync("rpc.queue"))
{
var response = HandleRequest(msg);
await publisher.PublishAsync(msg.ReplyTo!, response, new PublishOptions
{
CorrelationId = msg.CorrelationId
});
await msg.AckAsync();
}
依赖项
安装本包时会自动引入以下依赖:
| 包名 | 版本 |
|---|---|
| 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 | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.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)
- System.Text.Json (>= 8.0.0)
-
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.