canal 占用了生產者
.net core端 使用消費者獲取canal 消息
安裝 Confluent.Kafka demo使用 1.3.0
public static void Consumer() { var conf = new ConsumerConfig { GroupId = "canal-group", BootstrapServers = "192.168.1.26: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, EnableAutoCommit = true //是否自動提交 默認為true }; using (var c = new ConsumerBuilder<Ignore, string>(conf).Build()) { //指定 消費那個分區 //c.Assign(new TopicPartition ("flexmall13",new Partition ())) c.Subscribe("flexmall13"); 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); Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'."); //如果配置 自動提交為 否 需要手動提交 //c.Commit(); //c.Commit(new List<TopicPartitionOffset>() { cr.TopicPartitionOffset }); } catch (ConsumeException e) { Console.WriteLine($"Error occured: {e.Error.Reason}"); } } } catch (OperationCanceledException e) { // Ensure the consumer leaves the group cleanly and final offsets are committed. Console.WriteLine($"OperationCanceledException occured: {e.StackTrace}"); c.Close(); } } } }
注意:
1. 在config項 中 有 EnableAutoCommit 設置是否自動應答提交,默認是 true,如果設置為否,需要在消費后,手動 commit。
2.消費者指定訂閱 分區,如果不指定使用Subscribe,指定需要使用Assign