浏览代码

调整消费

develop
H Vs 8 个月前
父节点
当前提交
3fc5356f2b
共有 1 个文件被更改,包括 13 次插入8 次删除
  1. +13
    -8
      NearCardAttendance.TcpServer/Server.cs

+ 13
- 8
NearCardAttendance.TcpServer/Server.cs 查看文件

@@ -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)


正在加载...
取消
保存