133 lines
4.7 KiB
C#
133 lines
4.7 KiB
C#
using Confluent.Kafka;
|
|
using JiShe.CollectBus.Kafka.Attributes;
|
|
using JiShe.CollectBus.Kafka.Consumer;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Primitives;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static Confluent.Kafka.ConfigPropertyNames;
|
|
|
|
namespace JiShe.CollectBus.Kafka
|
|
{
|
|
public static class KafkaSubcribesExtensions
|
|
{
|
|
/// <summary>
|
|
/// 添加Kafka订阅
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="assembly"></param>
|
|
public static void UseKafkaSubscribers(this IApplicationBuilder app, Assembly assembly)
|
|
{
|
|
var subscribeTypes = assembly.GetTypes()
|
|
.Where(t => typeof(IKafkaSubscribe).IsAssignableFrom(t))
|
|
.ToList();
|
|
|
|
if (subscribeTypes.Count == 0) return;
|
|
|
|
var provider = app.ApplicationServices;
|
|
var lifetime = provider.GetRequiredService<IHostApplicationLifetime>();
|
|
|
|
lifetime.ApplicationStarted.Register(() =>
|
|
{
|
|
foreach (var subscribeType in subscribeTypes)
|
|
{
|
|
var subscribes = provider.GetServices(subscribeType).ToList();
|
|
subscribes.ForEach(subscribe => {
|
|
|
|
if(subscribe is IKafkaSubscribe)
|
|
{
|
|
BuildKafkaSubscriber(subscribe, provider);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 构建Kafka订阅
|
|
/// </summary>
|
|
/// <param name="subscribe"></param>
|
|
/// <param name="provider"></param>
|
|
private static void BuildKafkaSubscriber(object subscribe, IServiceProvider provider)
|
|
{
|
|
var subscribedMethods = subscribe.GetType().GetMethods()
|
|
.Select(m => new { Method = m, Attribute = m.GetCustomAttribute<KafkaSubscribeAttribute>() })
|
|
.Where(x => x.Attribute != null)
|
|
.ToArray();
|
|
foreach (var sub in subscribedMethods)
|
|
{
|
|
Task.Run(() => StartConsumerAsync(provider, sub.Attribute!, sub.Method, subscribe));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 启动后台消费线程
|
|
/// </summary>
|
|
/// <param name="config"></param>
|
|
/// <param name="attr"></param>
|
|
/// <param name="method"></param>
|
|
/// <param name="consumerInstance"></param>
|
|
/// <returns></returns>
|
|
private static async Task StartConsumerAsync(IServiceProvider provider, KafkaSubscribeAttribute attr,MethodInfo method, object subscribe)
|
|
{
|
|
var consumerService = provider.GetRequiredService<IConsumerService>();
|
|
var logger = provider.GetRequiredService<ILogger<CollectBusKafkaModule>>();
|
|
await consumerService.SubscribeAsync<string>(attr.Topics, async (message) =>
|
|
{
|
|
try
|
|
{
|
|
// 处理消息
|
|
return await ProcessMessageAsync(message, method, subscribe);
|
|
}
|
|
catch (ConsumeException ex)
|
|
{
|
|
// 处理消费错误
|
|
logger.LogError($"kafka消费异常:{ex.Message}");
|
|
}
|
|
return await Task.FromResult(false);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理消息
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="method"></param>
|
|
/// <param name="subscribe"></param>
|
|
/// <returns></returns>
|
|
private static async Task<bool> ProcessMessageAsync(string message, MethodInfo method, object subscribe)
|
|
{
|
|
var parameters = method.GetParameters();
|
|
if (parameters.Length != 1)
|
|
return true;
|
|
|
|
var paramType = parameters[0].ParameterType;
|
|
var messageObj = paramType == typeof(string)? message: JsonConvert.DeserializeObject(message, paramType);
|
|
|
|
if (method.ReturnType == typeof(Task))
|
|
{
|
|
object? result = await (Task<bool>)method.Invoke(subscribe, new[] { messageObj })!;
|
|
if (result is bool success)
|
|
return success;
|
|
}
|
|
else
|
|
{
|
|
object? result = method.Invoke(subscribe, new[] { messageObj });
|
|
if (result is bool success)
|
|
return success;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|