Quick.RabbitMQPlus
1.0.2
See the version list below for details.
dotnet add package Quick.RabbitMQPlus --version 1.0.2
NuGet\Install-Package Quick.RabbitMQPlus -Version 1.0.2
<PackageReference Include="Quick.RabbitMQPlus" Version="1.0.2" />
paket add Quick.RabbitMQPlus --version 1.0.2
#r "nuget: Quick.RabbitMQPlus, 1.0.2"
// Install Quick.RabbitMQPlus as a Cake Addin #addin nuget:?package=Quick.RabbitMQPlus&version=1.0.2 // Install Quick.RabbitMQPlus as a Cake Tool #tool nuget:?package=Quick.RabbitMQPlus&version=1.0.2
1、🍟Quick.RabbitMQPlus使用说明
该组件是基于RabbitMQ.Client
和Furion
组件进行封装使用的,目的是为了结合.Net Core更简单的使用RabbitMQ!!!
功能说明:
- 可根据配置文件读取RabbitMQ连接的各个配置等;
- 可根据实体定义的特性发布和订阅消息等。
2、🍖安装
安装命令如下所示:
Install-Package Quick.RabbitMQPlus
3、🧀生产端
3.1、🥞配置appsettings.json
在appsettings.json
配置文件中创建节点QuickRabbitMQPlus
>QuickRabbitMQPlusConfigs
,QuickRabbitMQPlusConfigs为数组类型(即可配置多个RabbitMQ服务地址),具体配置如下所示:
{
"QuickRabbitMQPlus": {
"QuickRabbitMQPlusConfigs": [
{
"ConnId": 1,
"UserName": "admin",
"Password": "123456",
"HostName": "192.168.3.1",
"Port": 5672,
"ExchangeName": "TestExchangeName",
"QueueNames": [ "TestRabbitMQName1", "TestRabbitMQName2" ],
"RouteKey": "TestRouteKey"
}
]
}
}
配置说明:
属性名称 | 属性说明 | 是否必填 | 备注 |
---|---|---|---|
ConnId | 连接配置Id | √ | 需要和QuickRabbitMQPlusQueueName特性中的ConnId一致 |
UserName | RabbitMQ连接账户 | √ | |
Password | RabbitMQ连接密码 | √ | |
HostName | RabbitMQ连接IP | √ | |
Port | RabbitMQ连接端口 | ||
ExchangeName | 交换机名称 | √ | |
QueueNames | 队列名称集合(与交换机ExchangeName进行绑定),如果同时设置了实体特性的队列名称集合,那么会优先采用实体的队列集合 | 此处为集合,目的是在发布消息时将消息存储到该队列集合中去 | |
RouteKey | 路由名称 |
3.2、🍞配置Program.cs
由于我们使用的是Furion,因此,我们可在程序启动文件中配置如下代码(具体可参考Furion入门指南):
Serve.Run(RunOptions.DefaultSilence.ConfigureBuilder(builder =>
{
//注册RabbitMQ连接配置对象
builder.Services.AddConfigurableOptions<QuickRabbitMQPlusOptions>();
}).Configure(app =>
{
}));
3.3、🧀定义发送消息实体
如下所示我们可以定义一个消息实体:
namespace Quick.RabbitMQPlus.Publisher
{
//[QuickRabbitMQPlusQueueName(1, "TestExchangeName", "TestRabbitMQName1")]
[QuickRabbitMQPlusQueueName(connId: 1)]
public class TestRabbitMQModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public int UserAge { get; set; }
public DateTime CreateTime { get; set; }
}
}
实体特性配置说明:
属性名称 | 属性说明 | 是否必填 | 备注 |
---|---|---|---|
ConnId | 连接配置Id | √ | 需要和配置文件中的ConnId一致 |
ExchangeName | 交换机名称 | √ | |
QueueName | 队列名称(多个队列名称请使用逗号,分隔),如果同时设置了实体特性的队列名称和配置中的_QueueNames_属性,那么会优先采用实体的队列名称 | ||
RouteKey | 路由名称 |
3.4、🥐发送消息Demo
如下所示为具体的发送消息代码:
//定义发送对象
var sendInstance = QuickRabbitMQPlusInstance<TestRabbitMQModel>.Instance();
//发送10条数据
for (int i = 0; i < 10; i++)
{
var msgModel = new TestRabbitMQModel
{
UserId = rand.Next(1, 9999),
UserName = "Quick" + (i + 1),
UserAge = rand.Next(20, 80),
CreateTime = DateTime.Now
};
var sendRet = await sendInstance.Send(msgModel);
if (sendRet.Item1)
{
//发送成功
}
else
{
//发送失败
var errMsg = $"失败原因:{sendRet.Item2}";
}
//间隔2秒发送一次
await Task.Delay(2000);
}
4、🥪消费端
4.1、🍝配置appsettings.json
在appsettings.json
配置文件中创建节点QuickRabbitMQPlus
>QuickRabbitMQPlusConfigs
,QuickRabbitMQPlusConfigs为数组类型(即可配置多个RabbitMQ服务地址),具体配置如下所示:
{
"QuickRabbitMQPlus": {
"QuickRabbitMQPlusConfigs": [
{
"ConnId": 1,
"UserName": "admin",
"Password": "123456",
"HostName": "192.168.3.1",
"Port": 5672,
"ExchangeName": "TestExchangeName",
//"QueueNames": [ "TestRabbitMQName1", "TestRabbitMQName2" ],
"RouteKey": "TestRouteKey"
}
]
}
}
4.2、🌮配置Program.cs
由于我们使用的是Furion,因此,我们可在程序启动文件中配置如下代码(具体可参考Furion入门指南):
Serve.Run(RunOptions.DefaultSilence.ConfigureBuilder(builder =>
{
//注册RabbitMQ连接配置对象
builder.Services.AddConfigurableOptions<QuickRabbitMQPlusOptions>();
}).Configure(app =>
{
}));
4.3、🧆定义接收消息实体
如下所示我们可以定义2个消息实体(第一个用于接收队列TestRabbitMQName1
的消息,第二个用于接收队列TestRabbitMQName2
的消息):
namespace Quick.RabbitMQPlus.Consumer
{
[QuickRabbitMQPlusQueueName(connId: 2, queueName: "TestRabbitMQName1")]
public class TestRabbitMQModel1
{
public int UserId { get; set; }
public string UserName { get; set; }
public int UserAge { get; set; }
public DateTime CreateTime { get; set; }
}
}
namespace Quick.RabbitMQPlus.Consumer
{
[QuickRabbitMQPlusQueueName(connId: 2, queueName: "TestRabbitMQName2")]
public class TestRabbitMQModel2
{
public int UserId { get; set; }
public string UserName { get; set; }
public int UserAge { get; set; }
public DateTime CreateTime { get; set; }
}
}
4.4、🍨接收消息Demo
如下所示为具体的接收消息代码:
//接收队列1的消息
var retRec1 = await QuickRabbitMQPlusInstance<TestRabbitMQModel1>.Instance().Receive((data, msg) =>
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\r\n队列1消息:{msg}");
});
if (!retRec1.Item1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\r\n队列1接收失败:{retRec1.Item2}");
}
//接收队列2的消息
var retRec2 = await QuickRabbitMQPlusInstance<TestRabbitMQModel2>.Instance().Receive((data, msg) =>
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($"\r\n队列2消息:{msg}");
});
if (!retRec2.Item1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\r\n队列2接收失败:{retRec2.Item2}");
}
4.5、🍱预览效果
<div style="background:#000;padding:10px;"> <div><font color="DarkCyan">####消费端已启动####</div></br> <div><font color="Green">队列1消息:{"UserId":8739,"UserName":"CCCC1","UserAge":40,"CreateTime":"2022-09-06 10:44:37"}</div></br> <div><font color="Magenta">队列2消息:{"UserId":8739,"UserName":"CCCC1","UserAge":40,"CreateTime":"2022-09-06 10:44:37"}</div></br> <div><font color="Green">队列1消息:{"UserId":8646,"UserName":"CCCC2","UserAge":54,"CreateTime":"2022-09-06 10:44:39"}</div></br> <div><font color="Magenta">队列2消息:{"UserId":8646,"UserName":"CCCC2","UserAge":54,"CreateTime":"2022-09-06 10:44:39"}</div></br> <div><font color="Magenta">队列2消息:{"UserId":1966,"UserName":"CCCC3","UserAge":21,"CreateTime":"2022-09-06 10:44:41"}</div></br> <div><font color="Green">队列1消息:{"UserId":1966,"UserName":"CCCC3","UserAge":21,"CreateTime":"2022-09-06 10:44:41"}</div></br> <div><font color="Magenta">队列2消息:{"UserId":8349,"UserName":"CCCC4","UserAge":25,"CreateTime":"2022-09-06 10:44:43"}</div></br> <div><font color="Green">队列1消息:{"UserId":8349,"UserName":"CCCC4","UserAge":25,"CreateTime":"2022-09-06 10:44:43"}</div></br> <div><font color="Magenta">队列2消息:{"UserId":3770,"UserName":"CCCC5","UserAge":31,"CreateTime":"2022-09-06 10:44:45"}</div></br> <div><font color="Green">队列1消息:{"UserId":3770,"UserName":"CCCC5","UserAge":31,"CreateTime":"2022-09-06 10:44:45"}</div></br> </div>
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Furion (>= 4.4.0)
- Newtonsoft.Json (>= 13.0.1)
- RabbitMQ.Client (>= 6.4.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.