我有两个Kafka组团。我需要使用kafkaspring实现它们之间的某种同步。
[cluster A, topic A] <-- [spring app] --> [cluster B, topic B]
我创建了注解为@transactional的listener,它用kafkatemplate发布消息。当两个集群都有连接时,这一点非常有效。当与目标集群的连接丢失时,侦听器似乎仍然确认新消息,但它们不会被发布。我尝试了手动黑客监听,禁用自动提交等,但他们似乎没有工作,因为我认为他们应该。。当连接重新联机时,消息永远无法传递。我需要帮助。
@KafkaListener(topics = "A", containerFactory = "syncLocalListenerFactory")
public void consumeLocal(@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key, @Payload SyncEvent message, Acknowledgment ack) {
kafkaSyncRemoteTemplate.send("B", key, message);
ack.acknowledge();
}
我正在获取日志:
2019-04-26 12:11:40.808 WARN 21304 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node 1001 could not be established. Broker may not be available.
2019-04-26 12:11:40.828 WARN 21304 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-2, groupId=app-sync] Connection to node 1001 could not be established. Broker may not be available.
2019-04-26 12:11:47.829 ERROR 21304 --- [ad | producer-1] o.s.k.support.LoggingProducerListener : Exception thrown when sending a message with key='...' and payload='...' to topic B:
org.apache.kafka.common.errors.TimeoutException: Expiring 2 record(s) for sync-2: 30002 ms has passed since batch creation plus linger time
2019-04-26 12:11:47.829 ERROR 21304 --- [ad | producer-1] o.s.k.support.LoggingProducerListener : Exception thrown when sending a message with key='...' and payload='...' to topic B:
org.apache.kafka.common.errors.TimeoutException: Expiring 2 record(s) for sync-2: 30002 ms has passed since batch creation plus linger time
---编辑---
这里的kafkaproperties是从application.properties文件读取的默认kafka spring属性,但在本例中它们都是默认的
@Bean
public ConsumerFactory<String, SyncEvent> syncLocalConsumerFactory() {
Map<String, Object> config = kafkaProperties.buildConsumerProperties();
config.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaProperties.getStreams().getApplicationId() + "-sync");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
config.put(JsonDeserializer.VALUE_DEFAULT_TYPE, SyncEvent.class);
config.put(JsonDeserializer.TRUSTED_PACKAGES, "app.structures");
config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
DefaultKafkaConsumerFactory<String, SyncEvent> cf = new DefaultKafkaConsumerFactory<>(config);
cf.setValueDeserializer(new JsonDeserializer<>(SyncEvent.class, objectMapper));
return cf;
}
@Bean(name = "syncLocalListenerFactory")
public ConcurrentKafkaListenerContainerFactory<String, SyncEvent> kafkaSyncLocalListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, SyncEvent> factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(syncLocalConsumerFactory());
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
factory.getContainerProperties().setAckOnError(false);
factory.setErrorHandler(new SeekToCurrentErrorHandler(0));
return factory;
}
2条答案
按热度按时间6yt4nkrj1#
本网站介绍如何设置错误处理程序(使用
SeekToCurrentErrorHandler
),它可能会帮助你。从spring文档中:seekTocurInterrorHandler:一个错误处理程序,用于查找剩余记录中每个主题的当前偏移量。用于在消息失败后回放分区,以便可以重放。
dsf9zpds2#
这是因为kafka事务不能跨越集群。您的@transactional注解没有任何意义,因此不管发布到集群b是否成功,偏移量都会提交到集群a。
对于跨集群流,您当前可以实现的最佳保证是“至少一次”处理,并且您可以通过确保只有在集群b的目标代理确认消息之后,才将偏移提交到集群a来实现它。
更多信息请看我的博客https://medium.com/@harelopler/kafka-cross-cluster-stream-reaching-least-once-semantics-c74ed0eb1a54跨集群访问