😎优化rabbitmq相关代码
This commit is contained in:
parent
efe5388be1
commit
d1021dde55
@ -1,18 +1,18 @@
|
||||
{
|
||||
"$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
|
||||
|
||||
// 事件源存储器配置
|
||||
"EventBus": {
|
||||
// 事件源存储器类型,默认内存存储(Redis则需要配合缓存相关配置)
|
||||
"EventSourceType": "Memory" // Memory、Redis、RabbitMQ、Kafka
|
||||
},
|
||||
|
||||
// RabbitMQ 配置
|
||||
"RabbitMQ": {
|
||||
"UserName": "adminnet",
|
||||
"Password": "adminnet++123456",
|
||||
"HostName": "127.0.0.1",
|
||||
"Port": 5672,
|
||||
"VirtualHost": "/"
|
||||
},
|
||||
|
||||
"EventBus": {
|
||||
// 事件源存储器类型,默认内存存储(Redis则需要配合缓存相关配置)
|
||||
"EventSourceType": "Memory", // Memory、Redis、RabbitMQ、Kafka
|
||||
"Kafka": {
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,12 +8,15 @@ namespace Admin.NET.Application;
|
||||
|
||||
public class RabbitMqHandler : IMessageHandler
|
||||
{
|
||||
public string QueueName => "admin.net.rabbitmq";//队列名称
|
||||
public string QueueName => "admin.net.rabbitmq"; // 队列名称
|
||||
|
||||
//消息处理函数
|
||||
/// <summary>
|
||||
/// 消息处理
|
||||
/// </summary>
|
||||
/// <param name="message"></param>
|
||||
/// <returns></returns>
|
||||
public async Task HandleMessageAsync(string message)
|
||||
{
|
||||
//Todo
|
||||
Console.WriteLine(message);
|
||||
|
||||
await Task.CompletedTask;
|
||||
|
||||
@ -93,6 +93,11 @@ public class TestService : IDynamicApiController
|
||||
return await App.GetRequiredService<SysFileService>().UploadFile(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生产消息测试
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[ApiDescriptionSettings(Name = "SendMessageToRabbitMQ", Description = "测试")]
|
||||
public async Task SendMessageToRabbitMQ([FromQuery] string input)
|
||||
|
||||
@ -1,54 +0,0 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 事件总线配置选项
|
||||
/// </summary>
|
||||
public sealed class EventBusOptions : IConfigurableOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 事件源存储器类型
|
||||
/// </summary>
|
||||
public string EventSourceType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ
|
||||
/// </summary>
|
||||
public RabbitMQSettings RabbitMQ { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ
|
||||
/// </summary>
|
||||
public sealed class RabbitMQSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// 账号
|
||||
/// </summary>
|
||||
public string UserName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 密码
|
||||
/// </summary>
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 主机
|
||||
/// </summary>
|
||||
public string HostName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 端口
|
||||
/// </summary>
|
||||
public int Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 虚拟主机
|
||||
/// </summary>
|
||||
public string VirtualHost { get; set; } = "/";
|
||||
}
|
||||
@ -6,23 +6,21 @@
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
public static class RabbitMQExtention
|
||||
public static class RabbitMQSetup
|
||||
{
|
||||
/// <summary>
|
||||
/// 注册RabbitMQ
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddRabbitMQ<THandler>(this IServiceCollection services)
|
||||
public static void AddRabbitMQ<THandler>(this IServiceCollection services)
|
||||
where THandler : class, IMessageHandler
|
||||
{
|
||||
if (App.GetConfig<string>("EventBus:EventSourceType", true) == "RabbitMQ")
|
||||
{
|
||||
services.AddSingleton<IMessageHandler, THandler>();
|
||||
services.AddSingleton<RabbitMqConnection>();
|
||||
services.AddSingleton<RabbitMqProducer>();
|
||||
services.AddHostedService<RabbitMqConsumer>();
|
||||
}
|
||||
return services;
|
||||
if (App.GetConfig<string>("EventBus:EventSourceType", true) != "RabbitMQ") return;
|
||||
|
||||
services.AddSingleton<IMessageHandler, THandler>();
|
||||
services.AddSingleton<RabbitMqConnection>();
|
||||
services.AddSingleton<RabbitMqProducer>();
|
||||
services.AddHostedService<RabbitMqConsumer>();
|
||||
}
|
||||
}
|
||||
@ -9,6 +9,9 @@ using RabbitMQ.Client.Events;
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 消费者
|
||||
/// </summary>
|
||||
public class RabbitMqConsumer : BackgroundService
|
||||
{
|
||||
private readonly RabbitMqConnection _connection;
|
||||
|
||||
@ -6,6 +6,9 @@
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMq 配置选项
|
||||
/// </summary>
|
||||
public class RabbitMqOptions : IConfigurableOptions
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@ -8,6 +8,9 @@ using RabbitMQ.Client;
|
||||
|
||||
namespace Admin.NET.Core;
|
||||
|
||||
/// <summary>
|
||||
/// 生产者
|
||||
/// </summary>
|
||||
public class RabbitMqProducer
|
||||
{
|
||||
private readonly RabbitMqConnection _connection;
|
||||
|
||||
@ -35,7 +35,7 @@ public static class ProjectOptions
|
||||
services.AddConfigurableOptions<EmailOptions>();
|
||||
services.AddConfigurableOptions<OAuthOptions>();
|
||||
services.AddConfigurableOptions<SMSOptions>();
|
||||
services.AddConfigurableOptions<EventBusOptions>();
|
||||
services.AddConfigurableOptions<RabbitMqOptions>();
|
||||
services.AddConfigurableOptions<AlipayOptions>();
|
||||
services.AddConfigurableOptions<MqttOptions>();
|
||||
services.AddConfigurableOptions<HttpRemotesOptions>();
|
||||
|
||||
@ -13,7 +13,6 @@ using Furion.SpecificationDocument;
|
||||
using Furion.VirtualFileServer;
|
||||
using IGeekFan.AspNetCore.Knife4jUI;
|
||||
using IPTools.Core;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@ -216,10 +215,10 @@ public class Startup : AppStartup
|
||||
//options.AddMonitor<EventHandlerMonitor>();
|
||||
|
||||
// 自定义事件源存储器
|
||||
var eventBusOpt = App.GetConfig<EventBusOptions>("EventBus", true);
|
||||
if (eventBusOpt.EventSourceType == "Redis")
|
||||
var eventSourceType = App.GetConfig<string>("EventBus:EventSourceType", true);
|
||||
// Redis
|
||||
if (eventSourceType == "Redis")
|
||||
{
|
||||
// Redis消息队列
|
||||
if (App.GetConfig<CacheOptions>("Cache", true).CacheType == CacheTypeEnum.Redis.ToString())
|
||||
{
|
||||
options.ReplaceStorerOrFallback(serviceProvider =>
|
||||
@ -229,19 +228,17 @@ public class Startup : AppStartup
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (eventBusOpt.EventSourceType == "RabbitMQ")
|
||||
// RabbitMQ
|
||||
else if (eventSourceType == "RabbitMQ")
|
||||
{
|
||||
var rmq = App.GetConfig<RabbitMqOptions>("RabbitMQ");
|
||||
eventBusOpt.RabbitMQ = rmq.Adapt<RabbitMQSettings>();
|
||||
|
||||
// RabbitMQ消息队列
|
||||
var rabbitMqOption = App.GetConfig<RabbitMqOptions>("RabbitMQ");
|
||||
var rbmqEventSourceStorer = new RabbitMQEventSourceStore(new ConnectionFactory
|
||||
{
|
||||
UserName = eventBusOpt.RabbitMQ.UserName,
|
||||
Password = eventBusOpt.RabbitMQ.Password,
|
||||
HostName = eventBusOpt.RabbitMQ.HostName,
|
||||
Port = eventBusOpt.RabbitMQ.Port,
|
||||
VirtualHost = eventBusOpt.RabbitMQ.VirtualHost,
|
||||
UserName = rabbitMqOption.UserName,
|
||||
Password = rabbitMqOption.Password,
|
||||
HostName = rabbitMqOption.HostName,
|
||||
Port = rabbitMqOption.Port,
|
||||
VirtualHost = rabbitMqOption.VirtualHost,
|
||||
}, "adminnet_eventsource_queue", 3000);
|
||||
options.ReplaceStorerOrFallback(serviceProvider => { return rbmqEventSourceStorer; });
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user