2025-04-09 14:33:20 +08:00
|
|
|
|
using Confluent.Kafka;
|
2025-04-17 13:35:08 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
2025-04-09 14:33:20 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Consumer;
|
2025-04-14 19:10:27 +08:00
|
|
|
|
using JiShe.CollectBus.Kafka.Producer;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2025-04-09 14:33:20 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using Volo.Abp;
|
|
|
|
|
|
using Volo.Abp.DependencyInjection;
|
2025-04-09 14:33:20 +08:00
|
|
|
|
using Volo.Abp.Modularity;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
using static Confluent.Kafka.ConfigPropertyNames;
|
2025-04-02 17:54:12 +08:00
|
|
|
|
|
2025-04-03 18:05:17 +08:00
|
|
|
|
namespace JiShe.CollectBus.Kafka
|
2025-04-02 17:54:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
public class CollectBusKafkaModule : AbpModule
|
|
|
|
|
|
{
|
|
|
|
|
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
|
|
|
|
|
{
|
2025-04-17 11:42:35 +08:00
|
|
|
|
var configuration = context.Services.GetConfiguration();
|
2025-04-17 13:35:08 +08:00
|
|
|
|
//var kafkaSection = configuration.GetSection(CommonConst.Kafka);
|
|
|
|
|
|
//KafkaOptionConfig kafkaOptionConfig = new KafkaOptionConfig ();
|
|
|
|
|
|
//kafkaSection.Bind(kafkaOptionConfig);
|
|
|
|
|
|
//if (configuration[CommonConst.ServerTagName] != null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// kafkaOptionConfig.ServerTagName = configuration[CommonConst.ServerTagName]!;
|
|
|
|
|
|
//}
|
|
|
|
|
|
//context.Services.AddSingleton(kafkaOptionConfig);
|
|
|
|
|
|
|
|
|
|
|
|
context.Services.Configure<KafkaOptionConfig>(context.Services.GetConfiguration().GetSection(CommonConst.Kafka));
|
|
|
|
|
|
|
|
|
|
|
|
Configure<KafkaOptionConfig>(options =>
|
2025-04-17 11:42:35 +08:00
|
|
|
|
{
|
2025-04-17 13:35:08 +08:00
|
|
|
|
configuration.GetSection(CommonConst.Kafka).Bind(options);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-14 19:10:27 +08:00
|
|
|
|
// 注册Producer
|
2025-04-16 09:54:21 +08:00
|
|
|
|
context.Services.AddSingleton<IProducerService, ProducerService>();
|
2025-04-14 19:10:27 +08:00
|
|
|
|
// 注册Consumer
|
2025-04-16 09:54:21 +08:00
|
|
|
|
context.Services.AddSingleton<IConsumerService, ConsumerService>();
|
2025-04-02 17:54:12 +08:00
|
|
|
|
}
|
2025-04-15 11:15:22 +08:00
|
|
|
|
|
|
|
|
|
|
public override void OnApplicationInitialization(ApplicationInitializationContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var app = context.GetApplicationBuilder();
|
2025-04-17 11:42:35 +08:00
|
|
|
|
// 程序运行目录
|
2025-04-17 11:53:29 +08:00
|
|
|
|
//var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
|
|
|
|
|
|
//if (!string.IsNullOrWhiteSpace(assemblyPath))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// var dllFiles = Directory.GetFiles(assemblyPath, "*.dll");
|
|
|
|
|
|
// var kafkaSubscriberAssemblies = new List<Assembly>();
|
|
|
|
|
|
// foreach (var file in dllFiles)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// try
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // 跳过已加载的程序集
|
|
|
|
|
|
// var assemblyName = AssemblyName.GetAssemblyName(file);
|
|
|
|
|
|
// var existingAssembly = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
|
// .FirstOrDefault(a => a.GetName().FullName == assemblyName.FullName);
|
|
|
|
|
|
// var assembly = existingAssembly ?? Assembly.LoadFrom(file);
|
2025-04-15 11:15:22 +08:00
|
|
|
|
|
2025-04-17 11:53:29 +08:00
|
|
|
|
// // 检查程序集是否包含 IKafkaSubscribe 的实现类
|
|
|
|
|
|
// var hasSubscriber = assembly.GetTypes()
|
|
|
|
|
|
// .Any(type =>
|
|
|
|
|
|
// typeof(IKafkaSubscribe).IsAssignableFrom(type) && // 实现接口
|
|
|
|
|
|
// !type.IsAbstract && !type.IsInterface); // 排除抽象类和接口本身
|
2025-04-17 11:42:35 +08:00
|
|
|
|
|
2025-04-17 11:53:29 +08:00
|
|
|
|
// if (hasSubscriber)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// kafkaSubscriberAssemblies.Add(assembly);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// catch{}
|
|
|
|
|
|
// app.UseKafkaSubscribers(kafkaSubscriberAssemblies.ToArray());
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2025-04-17 11:42:35 +08:00
|
|
|
|
// 获取程序集
|
2025-04-17 11:53:29 +08:00
|
|
|
|
app.UseKafkaSubscribers(Assembly.Load("JiShe.CollectBus.Application"));
|
2025-04-15 11:15:22 +08:00
|
|
|
|
}
|
2025-04-02 17:54:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|