消费者 Confluent.Kafka 的交易
Transaction in Confluent.Kafka at Consumer
我正在使用 Apache Kafka 作为消息处理器,并在 Asp.Net Core 中使用 Confluent.Kafka 作为消费者。
我想消费消息并保存在数据库中,显然,我需要在队列中提交或回滚消息的事务
我正在使用这个库的示例,代码如下:
public static void Main(string[] args)
{
var conf = new ConsumerConfig
{
GroupId = "test-consumer-group",
BootstrapServers = "127.0.0.1:9092",
// Note: The AutoOffsetReset property determines the start offset in the event
// there are not yet any committed offsets for the consumer group for the
// topic/partitions of interest. By default, offsets are committed
// automatically, so in this example, consumption will only start from the
// earliest message in the topic 'my-topic' the first time you run the program.
AutoOffsetReset = AutoOffsetReset.Earliest
};
using (var c = new ConsumerBuilder<Ignore, string>(conf).Build())
{
c.Subscribe("testtopic");
CancellationTokenSource cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) => {
e.Cancel = true; // prevent the process from terminating.
cts.Cancel();
};
try
{
while (true)
{
try
{
var cr = c.Consume(cts.Token); // I NEED TRANSACTION HERE...
Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
}
catch (ConsumeException e)
{
Console.WriteLine($"Error occured: {e.Error.Reason}");
}
}
}
catch (OperationCanceledException)
{
c.Close();
}
}
}
我该怎么办?
默认 Kafka-consumer "automatically and periodically commit offsets in the background" - 此行为由两个配置参数定义:EnableAutoCommit and AutoCommitIntervalMs.
在你的情况下需要手动提交:
var conf = new ConsumerConfig
{
// ..
EnableAutoCommit = false // <-----
};
// ..
try
{
var cr = c.Consume(cts.Token);
// .. save data to database ..
c.Commit(); // <-----
Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
}
catch (ConsumeException e)
{
Console.WriteLine($"Error occured: {e.Error.Reason}");
}
我正在使用 Apache Kafka 作为消息处理器,并在 Asp.Net Core 中使用 Confluent.Kafka 作为消费者。
我想消费消息并保存在数据库中,显然,我需要在队列中提交或回滚消息的事务
我正在使用这个库的示例,代码如下:
public static void Main(string[] args)
{
var conf = new ConsumerConfig
{
GroupId = "test-consumer-group",
BootstrapServers = "127.0.0.1:9092",
// Note: The AutoOffsetReset property determines the start offset in the event
// there are not yet any committed offsets for the consumer group for the
// topic/partitions of interest. By default, offsets are committed
// automatically, so in this example, consumption will only start from the
// earliest message in the topic 'my-topic' the first time you run the program.
AutoOffsetReset = AutoOffsetReset.Earliest
};
using (var c = new ConsumerBuilder<Ignore, string>(conf).Build())
{
c.Subscribe("testtopic");
CancellationTokenSource cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) => {
e.Cancel = true; // prevent the process from terminating.
cts.Cancel();
};
try
{
while (true)
{
try
{
var cr = c.Consume(cts.Token); // I NEED TRANSACTION HERE...
Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
}
catch (ConsumeException e)
{
Console.WriteLine($"Error occured: {e.Error.Reason}");
}
}
}
catch (OperationCanceledException)
{
c.Close();
}
}
}
我该怎么办?
默认 Kafka-consumer "automatically and periodically commit offsets in the background" - 此行为由两个配置参数定义:EnableAutoCommit and AutoCommitIntervalMs.
在你的情况下需要手动提交:
var conf = new ConsumerConfig
{
// ..
EnableAutoCommit = false // <-----
};
// ..
try
{
var cr = c.Consume(cts.Token);
// .. save data to database ..
c.Commit(); // <-----
Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
}
catch (ConsumeException e)
{
Console.WriteLine($"Error occured: {e.Error.Reason}");
}