😎优化自定义事件源存储

This commit is contained in:
zuohuaijun 2025-01-08 01:22:32 +08:00
parent 57dbfd3e01
commit 0bc75bb7d5
3 changed files with 35 additions and 31 deletions

View File

@ -2,11 +2,15 @@
"$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json", "$schema": "https://gitee.com/dotnetchina/Furion/raw/v4/schemas/v4/furion-schema.json",
"EventBus": { "EventBus": {
// Redis
"EventSourceType": "Memory", // MemoryRedisRabbitMQKafka
"RabbitMQ": { "RabbitMQ": {
"UserName": "adminnet", "UserName": "adminnet",
"Password": "adminnet++123456", "Password": "adminnet++123456",
"HostName": "127.0.0.1", "HostName": "127.0.0.1",
"Port": 5672 "Port": 5672
},
"Kafka": {
} }
} }
} }

View File

@ -11,6 +11,11 @@ namespace Admin.NET.Core;
/// </summary> /// </summary>
public sealed class EventBusOptions : IConfigurableOptions public sealed class EventBusOptions : IConfigurableOptions
{ {
/// <summary>
/// 事件源存储器类型
/// </summary>
public string EventSourceType { get; set; }
/// <summary> /// <summary>
/// RabbitMQ /// RabbitMQ
/// </summary> /// </summary>

View File

@ -24,6 +24,7 @@ using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Newtonsoft.Json; using Newtonsoft.Json;
using OnceMi.AspNetCore.OSS; using OnceMi.AspNetCore.OSS;
using RabbitMQ.Client;
using SixLabors.ImageSharp.Web.DependencyInjection; using SixLabors.ImageSharp.Web.DependencyInjection;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -150,46 +151,40 @@ public class Startup : AppStartup
options.UnobservedTaskExceptionHandler = (obj, args) => options.UnobservedTaskExceptionHandler = (obj, args) =>
{ {
if (args.Exception?.Message != null) if (args.Exception?.Message != null)
Log.Error($"EeventBus 有未处理异常 {args.Exception?.Message} ", args.Exception); Log.Error($"事件总线未处理异常 {args.Exception?.Message} ", args.Exception);
}; };
//// 事件执行监视器(执行之前、执行之后、执行异常) //// 事件执行监视器(执行之前、执行之后、执行异常)
//options.AddMonitor<EventHandlerMonitor>(); //options.AddMonitor<EventHandlerMonitor>();
#region Redis消息队列 // 自定义事件源存储器
var eventBusOpt = App.GetConfig<EventBusOptions>("EventBus", true);
// 替换事件源存储器为Redis if (eventBusOpt.EventSourceType == "Redis")
var cacheOptions = App.GetConfig<CacheOptions>("Cache", true);
if (cacheOptions.CacheType == CacheTypeEnum.Redis.ToString())
{ {
// Redis消息队列
if (App.GetConfig<CacheOptions>("Cache", true).CacheType == CacheTypeEnum.Redis.ToString())
{
options.ReplaceStorerOrFallback(serviceProvider =>
{
var cacheProvider = serviceProvider.GetRequiredService<NewLife.Caching.ICacheProvider>();
return new RedisEventSourceStorer(cacheProvider, "adminnet_eventsource_queue", 3000);
});
}
}
else if (eventBusOpt.EventSourceType == "RabbitMQ")
{
// RabbitMQ消息队列
var rbmqEventSourceStorer = new RabbitMQEventSourceStore(new ConnectionFactory
{
UserName = eventBusOpt.RabbitMQ.UserName,
Password = eventBusOpt.RabbitMQ.Password,
HostName = eventBusOpt.RabbitMQ.HostName,
Port = eventBusOpt.RabbitMQ.Port
}, "adminnet_eventsource_queue", 3000);
options.ReplaceStorerOrFallback(serviceProvider => options.ReplaceStorerOrFallback(serviceProvider =>
{ {
var cacheProvider = serviceProvider.GetRequiredService<NewLife.Caching.ICacheProvider>(); return rbmqEventSourceStorer;
// 创建默认内存通道事件源对象可自定义队列路由keyadminnet_eventsource_queue
return new RedisEventSourceStorer(cacheProvider, "adminnet_eventsource_queue", 3000);
}); });
} }
#endregion Redis消息队列
#region RabbitMQ消息队列
//// 创建默认内存通道事件源对象可自定义队列路由keyadminnet
//var eventBusOpt = App.GetConfig<EventBusOptions>("EventBus", true);
//var rbmqEventSourceStorer = new RabbitMQEventSourceStore(new ConnectionFactory
//{
// UserName = eventBusOpt.RabbitMQ.UserName,
// Password = eventBusOpt.RabbitMQ.Password,
// HostName = eventBusOpt.RabbitMQ.HostName,
// Port = eventBusOpt.RabbitMQ.Port
//}, "adminnet", 3000);
//// 替换默认事件总线存储器
//options.ReplaceStorer(serviceProvider =>
//{
// return rbmqEventSourceStorer;
//});
#endregion RabbitMQ消息队列
}); });
// 图像处理 // 图像处理