Galosys.Foundation.RabbitMQ.Client
26.7.20.2
dotnet add package Galosys.Foundation.RabbitMQ.Client --version 26.7.20.2
NuGet\Install-Package Galosys.Foundation.RabbitMQ.Client -Version 26.7.20.2
<PackageReference Include="Galosys.Foundation.RabbitMQ.Client" Version="26.7.20.2" />
<PackageVersion Include="Galosys.Foundation.RabbitMQ.Client" Version="26.7.20.2" />
<PackageReference Include="Galosys.Foundation.RabbitMQ.Client" />
paket add Galosys.Foundation.RabbitMQ.Client --version 26.7.20.2
#r "nuget: Galosys.Foundation.RabbitMQ.Client, 26.7.20.2"
#:package Galosys.Foundation.RabbitMQ.Client@26.7.20.2
#addin nuget:?package=Galosys.Foundation.RabbitMQ.Client&version=26.7.20.2
#tool nuget:?package=Galosys.Foundation.RabbitMQ.Client&version=26.7.20.2
Galosys.Foundation.RabbitMQ.Client
目标框架:
et8.0\ ·
et10.0\(#if NET10_0\ 条件编译)
定位: RabbitMQ 高性能客户端,连接池 + Publisher Confirms + OTel 可观测
基于 RabbitMQ.Client 的 RabbitMQ 实现,实现 Galosys.Foundation.Amqp 的 IAmqpPublisher / IAmqpConsumer 接口。提供共享连接管理、批量发布、Publisher Confirms、消费并发控制、DLX/DLQ、OpenTelemetry 追踪/指标、健康检查。
架构
IAmqpPublisher IAmqpConsumer
└─ ConvertAndSendAsync ├─ HandleAsync
└─ CancelAsync
RabbitPublisher : IAmqpPublisher RabbitConsumer : IAmqpConsumer
└─ 共享 IConnection + 临时通道 └─ 连接池 + 信号量并发控制
AmqpTemplate : IAmqpPublisher, IAmqpConsumer [密封门面]
└─ 组合 RabbitPublisher + RabbitConsumer
功能特性
- 统一发送 —
RabbitPublisher.ConvertAndSendAsync为框架唯一发送方法,内部 seq 追踪 + ACK/NACK + 超时 - WaitForConfirm 控制 — 默认等待 broker ACK(超时可配
PublishConfirmTimeoutMs),非核心可WaitForConfirm=false - 连接管理 — 发布端共享单
IConnection+ 临时通道,消费端ConnectionPool多连接 Round-Robin 通道分配 - 批量发布 — 同通道批量发送 + Properties 复用 + 逐条 Confirm 追踪
- 拓扑缓存 —
TopologyCache避免重复声明队列/交换机 - DLX/DLQ — 不可达消息转发到
{routingKey}_dlx+ 递归防护 - 消费重试 — x-delayed-message 指数退避 + DLQ fallback
- OTel 追踪 — W3C traceparent 注入,Producer/Consumer Span
- OTel 指标 — 覆盖发送、消费、延迟、批量大小、连接/通道活跃数
- 健康检查 —
RabbitMQHealthCheck聚合发布者/消费者连接池状态 - IMessageBus —
RabbitMessageBus实现 Core 的IMessageBus接口(Send + Publish)
前置条件
- RabbitMQ 3.x+
- 延迟消息需
rabbitmq_delayed_message_exchange插件 - 队列深度采集需开启
rabbitmq_management插件,且配置"RabbitMQ:Management:Enabled": true
安装
<PackageReference Include="Galosys.Foundation.RabbitMQ.Client" Version="x.x.x" />
快速开始
1. 最小配置
{
"RabbitMQ": {
"HostName": "localhost",
"Port": 5672,
"UserName": "guest",
"Password": "guest",
"VirtualHost": "/"
}
}
2. 注册服务
services.AddAmqp(configuration);
services.AddRabbitMQ(configuration);
3. 发送消息
using RabbitMQ.Client;
public class OrderService
{
private readonly AmqpTemplate _amqp;
public OrderService(AmqpTemplate amqp) => _amqp = amqp;
public async Task SendAsync() =>
await _amqp.SendAsync("order_created", new[] { new { OrderNo = "ORD001" } });
public async Task BroadcastAsync() =>
await _amqp.BroadcastAsync("order_events", new[] { new { OrderNo = "ORD001" } });
public async Task DelayAsync() =>
await _amqp.DelayAsync("order_created", new[] { new { OrderNo = "ORD001" } }, 5000);
}
4. 消费消息
using RabbitMQ.Client;
[Handler]
public class OrderHandler : IMessageHandler
{
[AmqpHandler("order_created")]
public async Task<bool> HandleAsync(IMessage msg)
{
// 处理消息
return true; // true = ACK, false = Nack 进入 DLQ
}
}
5. 缓冲聚批发送(日志/指标场景)
var batch = serviceProvider.GetRequiredService<BatchingAmqpTemplate>();
await batch.SendAsync(new PendingMessage
{
RoutingKey = "app.log",
Body = Encoding.UTF8.GetBytes("log entry"),
Properties = new AmqpProperties().Default()
});
详细配置
完整配置示例
{
"RabbitMQ": {
"HostName": "rabbit1, rabbit2, rabbit3",
"UserName": "user",
"Password": "pass",
"VirtualHost": "/",
"AutoCreateEnabled": true,
"NetworkRecoveryInterval": 10,
"RequestedHeartbeat": 45,
"Limit": {
"ProducerMaxConnections": 4,
"ProducerChannelMaxPerConnection": 10,
"ConsumerMaxConnections": 4,
"ConsumerChannelMaxPerConnection": 50,
"MaxMessageSize": 65536,
"MaxTps": 0,
"BatchSize": 100,
"BatchTimeoutMs": 50,
"PublishConfirmTimeoutMs": 3000,
"RetryCount": 3,
"PipelineCapacity": 5000
},
"Management": {
"Enabled": true,
"BaseUrl": "http://localhost:15672"
}
}
}
可观测性
健康检查
注册端点 /health 可查看 RabbitMQ 状态。检查标签 "rabbitmq" / ["rabbitmq", "messaging"]。
状态:Healthy(全部正常)/ Degraded(部分断连)/ Unhealthy(全部断开)。
OpenTelemetry 追踪
ActivitySource 名称:Galosys.Foundation.RabbitMQ
| Span | 触发时机 | 关键 Tag |
|---|---|---|
rabbitmq.publish |
ConvertAndSendAsync |
messaging.destination、RoutingKey、W3C traceparent |
rabbitmq.consume |
消费处理 | messaging.destination、Consumer Tag |
指标
Meter 名称:Galosys.Foundation.RabbitMQ
| 指标名 | 类型 | 说明 |
|---|---|---|
rabbitmq.messages.published |
Counter | 已发送消息数 |
rabbitmq.messages.consumed |
Counter | 已消费消息数 |
rabbitmq.messages.rejected |
Counter | 被拒绝消息数 |
rabbitmq.messages.retried |
Counter | 重试消息数 |
rabbitmq.messages.publish_latency |
Histogram | 发送延迟(ms) |
rabbitmq.messages.processing_duration |
Histogram | 消费处理耗时(ms) |
rabbitmq.batch.size |
Histogram | 批量发送大小 |
rabbitmq.connections.active |
ObservableGauge | 活跃连接数 |
rabbitmq.channels.active |
ObservableGauge | 活跃 Channel 数 |
组件列表
| 组件 | 说明 |
|---|---|
RabbitPublisher |
消息发布器,共享连接 + 临时通道 + Confirm 追踪 |
RabbitConsumer |
消息消费者,连接池 + 信号量并发控制 + DLX/DLQ |
RabbitBootstrapper |
启动器,预热连接池 + 扫描 Handler + 优雅关闭 |
ConnectionPool |
消费端连接池,Round-Robin 通道分配 + 健康检测 |
ChannelLease |
Channel 租约,Dispose 自动归还 |
BatchingAmqpTemplate |
缓冲聚批发送器,适合日志/指标场景 |
PendingMessage |
缓冲消息模型,含 TCS 确认追踪 |
TopologyCache |
拓扑缓存,ConcurrentDictionary + 重连时重建 |
RabbitMQMeter |
OTel 指标 |
RabbitMQTracing |
OTel 追踪,W3C traceparent 注入/提取 |
RabbitMQHealthCheck |
健康检查,聚合发布者/消费者连接池状态 |
RabbitMessageBus |
IMessageBus 实现,发送/广播 |
依赖
Galosys.Foundation.Amqp:抽象层Galosys.Foundation.Core:IMessageBus、IObjectSerializerRabbitMQ.Client:NuGet
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.0
- Galosys.Foundation.Amqp (>= 26.7.20.2)
- Galosys.Foundation.Core (>= 26.7.20.2)
- Galosys.Foundation.OpenTelemetry (>= 26.7.20.2)
- rabbitmq.client (>= 7.1.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Galosys.Foundation.RabbitMQ.Client:
| Package | Downloads |
|---|---|
|
Galosys.Foundation.RabbitMQ.Benchmarks
Galosys.Foundation快速开发库 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.7.20.2 | 29 | 7/20/2026 |
| 26.7.20.1 | 35 | 7/20/2026 |
| 26.7.19.3 | 34 | 7/19/2026 |
| 26.7.19.2 | 39 | 7/19/2026 |
| 26.7.19.1 | 33 | 7/19/2026 |
| 26.7.18.2 | 37 | 7/18/2026 |
| 26.7.18.1 | 41 | 7/18/2026 |
| 26.7.14.1 | 87 | 7/14/2026 |
| 26.7.13.1 | 89 | 7/13/2026 |
| 26.7.12.1 | 94 | 7/12/2026 |
| 26.7.11.1 | 100 | 7/11/2026 |
| 26.7.10.2 | 90 | 7/10/2026 |
| 26.7.10.1 | 98 | 7/10/2026 |
| 26.7.7.2 | 106 | 7/7/2026 |
| 26.7.7.1 | 98 | 7/7/2026 |
| 26.7.2.1 | 106 | 7/2/2026 |
| 26.7.1.2 | 87 | 7/1/2026 |
| 26.5.20.1 | 102 | 5/20/2026 |
| 26.5.19.1 | 104 | 5/19/2026 |
| 1.0.0 | 94 | 7/7/2026 |