HMEFramework.RabbitMQ
2.7.0
dotnet add package HMEFramework.RabbitMQ --version 2.7.0
NuGet\Install-Package HMEFramework.RabbitMQ -Version 2.7.0
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="HMEFramework.RabbitMQ" Version="2.7.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HMEFramework.RabbitMQ" Version="2.7.0" />
<PackageReference Include="HMEFramework.RabbitMQ" />
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 HMEFramework.RabbitMQ --version 2.7.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HMEFramework.RabbitMQ, 2.7.0"
#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 HMEFramework.RabbitMQ@2.7.0
#: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=HMEFramework.RabbitMQ&version=2.7.0
#tool nuget:?package=HMEFramework.RabbitMQ&version=2.7.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HMEFramework.RabbitMQ
简介
HMEFramework.RabbitMQ 基于 RabbitMQ .NET 客户端的封装库,提供了更简单易用的 API 来处理 RabbitMQ 的连接、生产者和消费者操作。
功能特性
- 自动连接管理,支持断线重连
- 多主机支持,自动故障转移
- 线程安全的连接和通道管理
- 支持多种消息模式:普通模式/Work模式、发布/订阅模式、路由模式、Topic模式
- 消息确认和拒绝机制
- 消息限流控制
运行环境
- 跨平台支持: Supports .NET Standard 2.1, .NET 6+.
安装
dotnet add package HMEFramework.RabbitMQ
配置连接
添加配置到 appsettings.json:
{
"RabbitMQConfig": {
"Hosts": [ "rabbitmq1", "rabbitmq2", "rabbitmq3" ],
"Port": 5672,
"UserName": "guest",
"Password": "guest",
"VirtualHost": "/"
}
}
服务注册
var rabbitMQConfig = Configuration.GetSection("RabbitMQConfig").Get<RabbitMQConfig>();
builder.Services.AddRabbitMQSetup(rabbitMQConfig);
//另一种方式
builder.Services.AddRabbitMQSetup(config =>
{
config.Hosts = new[] { "rabbitmq1", "rabbitmq2", "rabbitmq3" };
config.Port = 5672;
config.UserName = "guest";
config.Password = "guest";
config.VirtualHost = "/";
});
生产者示例
public class MessageService
{
private readonly RabbitMQProducer _producer;
public MessageService(RabbitMQProducer producer)
{
_producer = producer;
}
public async Task SendMessageAsync(string queueName, string message)
{
await _producer.PublishAsync(queueName, message, options =>
{
options.Durable = true; // 持久化队列
});
}
public async Task SendToExchangeAsync(string exchange, string routingKey, string message)
{
await _producer.PublishAsync(exchange, routingKey, message, options =>
{
options.Type = RabbitMQExchangeType.Direct;
options.Durable = true;
});
}
}
消费者示例
public class MessageProcessor : IHostedService, IDisposable
{
private readonly RabbitMQConsumer _consumer;
private readonly ILogger<MessageProcessor> _logger;
private ListenResult _listenResult;
public MessageProcessor(RabbitMQConsumer consumer, ILogger<MessageProcessor> logger)
{
_consumer = consumer;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
_consumer.Received += OnMessageReceived;
// 开始监听队列
_listenResult = await _consumer.ListenAsync("my_queue", options =>
{
options.AutoAck = false; // 手动确认
options.Durable = true;
options.FetchCount = 10; // 每次获取10条消息
});
}
private void OnMessageReceived(RecieveResult result)
{
try
{
_logger.LogInformation($"Received message: {result.Body}");
// 处理消息...
// 确认消息处理完成
result.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to process message");
// 不调用Commit(),消息会被重新入队
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
_listenResult?.Stop();
_consumer.Received -= OnMessageReceived;
return Task.CompletedTask;
}
public void Dispose()
{
_listenResult?.Stop();
}
}
其他用法
//批量获取消息
var message = await _consumer.ReceiveGetAsync("my_queue", new ConsumeQueueOptions
{
FetchCount = 5
});
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine($"Received: {message}");
}
//获取队列消息数量
var count = await _consumer.GetMQMessageCountAsync("my_queue");
Console.WriteLine($"Messages in queue: {count}");
//使用交换机和路由键
// 生产者
await _producer.PublishAsync("my_exchange", "my.routing.key", "Hello RabbitMQ", options =>
{
options.Type = RabbitMQExchangeType.Topic;
options.Durable = true;
});
// 消费者
await _consumer.ListenAsync("my_exchange", "my_queue", options =>
{
options.RoutingKeys = new[] { "my.*" }; // 订阅所有以my.开头的路由键
options.AutoAck = false;
});
| 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 was computed. 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 was computed. 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 was computed. 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
- HMEFramework (>= 2.7.0)
- Microsoft.Extensions.Options (>= 9.0.10)
- Polly (>= 8.6.4)
- RabbitMQ.Client (>= 7.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.