调整代码

This commit is contained in:
zenghongyao 2025-05-23 14:01:33 +08:00
parent 87bf7feff2
commit 179b9a2e91
3 changed files with 21 additions and 17 deletions

View File

@ -135,6 +135,8 @@ namespace JiShe.CollectBus.Kafka.Consumer
{ {
try try
{ {
// 扩展独立线程,避免阻塞
_kafkaTaskScheduler.WorkerThreadExpansion();
await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token => await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token =>
{ {
var consumerKey = $"{groupId}_{string.Join("_", topics)}_{typeof(TKey).Name}_{typeof(TValue).Name}"; var consumerKey = $"{groupId}_{string.Join("_", topics)}_{typeof(TKey).Name}_{typeof(TValue).Name}";
@ -239,6 +241,8 @@ namespace JiShe.CollectBus.Kafka.Consumer
{ {
try try
{ {
// 扩展独立线程,避免阻塞
_kafkaTaskScheduler.WorkerThreadExpansion();
await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token => await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token =>
{ {
var consumerKey = $"{groupId}_{string.Join("_", topics)}_{typeof(Ignore).Name}_{typeof(TValue).Name}"; var consumerKey = $"{groupId}_{string.Join("_", topics)}_{typeof(Ignore).Name}_{typeof(TValue).Name}";
@ -367,6 +371,8 @@ namespace JiShe.CollectBus.Kafka.Consumer
{ {
try try
{ {
// 扩展独立线程,避免阻塞
_kafkaTaskScheduler.WorkerThreadExpansion();
await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token => await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token =>
{ {
@ -538,6 +544,8 @@ namespace JiShe.CollectBus.Kafka.Consumer
{ {
try try
{ {
// 扩展独立线程,避免阻塞
_kafkaTaskScheduler.WorkerThreadExpansion();
await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token => await _kafkaPollyPipeline.KafkaPipeline.ExecuteAsync(async token =>
{ {

View File

@ -108,6 +108,8 @@ namespace JiShe.CollectBus.Kafka
{ {
logger.LogError(ex, "Kafka任务调度异常"); logger.LogError(ex, "Kafka任务调度异常");
}; };
//logger.LogWarning($"kafka订阅工作线程数{kafkaTaskScheduler.WorkerThreads},队列任务:{kafkaTaskScheduler.QueuedTasks}数");
//
// 订阅调度监控测试可打开 // 订阅调度监控测试可打开
//_ = Task.Factory.StartNew(async () => //_ = Task.Factory.StartNew(async () =>
// { // {
@ -235,15 +237,11 @@ namespace JiShe.CollectBus.Kafka
private static async Task StartConsumerAsync(IServiceProvider provider, KafkaSubscribeAttribute attr, MethodInfo method, object subscribe, ILogger<CollectBusKafkaModule> logger) private static async Task StartConsumerAsync(IServiceProvider provider, KafkaSubscribeAttribute attr, MethodInfo method, object subscribe, ILogger<CollectBusKafkaModule> logger)
{ {
var consumerService = provider.GetRequiredService<IConsumerService>(); var consumerService = provider.GetRequiredService<IConsumerService>();
var kafkaTaskScheduler = provider.GetRequiredService<KafkaTaskScheduler>();
if (attr.EnableBatch) if (attr.EnableBatch)
{ {
Interlocked.Increment(ref _threadStartCount); Interlocked.Increment(ref _threadStartCount);
logger.LogInformation($"kafka开启线程消费:{_threadStartCount}"); logger.LogInformation($"kafka开启线程消费:{_threadStartCount}");
// 扩展独立线程,避免阻塞
kafkaTaskScheduler.WorkerThreadExpansion();
await consumerService.SubscribeBatchAsync<dynamic>(attr.Topic, async (message) => await consumerService.SubscribeBatchAsync<dynamic>(attr.Topic, async (message) =>
{ {
try try
@ -271,8 +269,6 @@ namespace JiShe.CollectBus.Kafka
{ {
Interlocked.Increment(ref _threadStartCount); Interlocked.Increment(ref _threadStartCount);
logger.LogInformation($"kafka开启线程消费:{_threadStartCount}"); logger.LogInformation($"kafka开启线程消费:{_threadStartCount}");
// 扩展独立线程,避免阻塞
kafkaTaskScheduler.WorkerThreadExpansion();
await consumerService.SubscribeAsync<dynamic>(attr.Topic, async (message) => await consumerService.SubscribeAsync<dynamic>(attr.Topic, async (message) =>
{ {
try try