Browse Source

调整消费

develop
H Vs 8 months ago
parent
commit
3fc5356f2b
1 changed files with 13 additions and 8 deletions
  1. +13
    -8
      NearCardAttendance.TcpServer/Server.cs

+ 13
- 8
NearCardAttendance.TcpServer/Server.cs View File

@@ -29,7 +29,7 @@ namespace NearCardAttendance.TcpServer

private readonly ServiceConfig _configService;
private readonly HttpHelper _httpHelper = default!;
private int _messageCount = 0;
//private int _messageCount = 0;


public Server(
@@ -87,16 +87,20 @@ namespace NearCardAttendance.TcpServer
var consumeResult = kafkaConsumer.Consume(stoppingToken);
if (consumeResult != null)
{
_messageCount++;
//_messageCount++;
consumeBatchResult.Add(consumeResult);
//// 30条消息为一批
#if DEBUG
if (!await ProcessBatchMessageAsync(consumeBatchResult, kafkaConsumer))
{ // 返回结果错误暂停5分钟
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
if (consumeBatchResult.Count % 2 == 0)
{
if (!await ProcessBatchMessageAsync(consumeBatchResult, kafkaConsumer))
{ // 返回结果错误暂停5分钟
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
}
//// 30条消息为一批
#else
if (_messageCount % 30 == 0)
if (consumeBatchResult.Count % 30 == 0)
{
if (!await ProcessBatchMessageAsync(consumeBatchResult, kafkaConsumer))
{ // 返回结果错误暂停5分钟
@@ -174,6 +178,7 @@ namespace NearCardAttendance.TcpServer
kafkaConsumer.Commit(x);
_logger.LogInformation($"完成消费:{JsonConvert.SerializeObject(x.Message.Value)}");
});
consumeBatchResult.Clear();
return true;
}
}
@@ -197,7 +202,7 @@ namespace NearCardAttendance.TcpServer
BootstrapServers = _configService.KafkaServerAddress,
AutoOffsetReset = AutoOffsetReset.Earliest,
EnableAutoCommit = false, // 关闭自动提交偏移量
CancellationDelayMaxMs = 1//set CancellationDelayMaxMs
//CancellationDelayMaxMs = 1//set CancellationDelayMaxMs
};

return new ConsumerBuilder<Ignore, string>(consumerConfig)


Loading…
Cancel
Save