2025-04-09 14:33:20 +08:00
|
|
|
|
using Confluent.Kafka;
|
|
|
|
|
|
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();
|
|
|
|
|
|
var kafkaSection = configuration.GetSection("Kafka");
|
|
|
|
|
|
KafkaOptionConfig kafkaOptionConfig = new KafkaOptionConfig ();
|
|
|
|
|
|
kafkaSection.Bind(kafkaOptionConfig);
|
|
|
|
|
|
if (configuration["ServerTagName"] != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
kafkaOptionConfig.ServerTagName = configuration["ServerTagName"]!;
|
|
|
|
|
|
}
|
|
|
|
|
|
context.Services.AddSingleton(kafkaOptionConfig);
|
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
|
|
|
|
// 程序运行目录
|
|
|
|
|
|
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:42:35 +08:00
|
|
|
|
// 检查程序集是否包含 IKafkaSubscribe 的实现类
|
|
|
|
|
|
var hasSubscriber = assembly.GetTypes()
|
|
|
|
|
|
.Any(type =>
|
|
|
|
|
|
typeof(IKafkaSubscribe).IsAssignableFrom(type) && // 实现接口
|
|
|
|
|
|
!type.IsAbstract && !type.IsInterface); // 排除抽象类和接口本身
|
|
|
|
|
|
|
|
|
|
|
|
if (hasSubscriber)
|
|
|
|
|
|
{
|
|
|
|
|
|
kafkaSubscriberAssemblies.Add(assembly);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch{}
|
|
|
|
|
|
app.UseKafkaSubscribers(kafkaSubscriberAssemblies.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 获取程序集
|
|
|
|
|
|
//app.UseKafkaSubscribers(new[] { Assembly.Load("JiShe.CollectBus.Application")});
|
2025-04-15 11:15:22 +08:00
|
|
|
|
}
|
2025-04-02 17:54:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|