【发布时间】:2021-06-08 14:33:34
【问题描述】:
如何监控 Kafka 消费者延迟并生成电子邮件/警报?以下是我的要求
- 我想在主题超过 1 天的消息时触发电子邮件。
我正在使用 Spring Boot 微服务,Java 8
@Configuration
public class KafkaConsumerConfig
{
@Value(value = "${kafka.bootstrapAddress}")
private String bootstrapAddress;
@Value(value = "${general.topic.group.id}")
private String groupId;
@Value(value = "${user.topic.group.id}")
private String userGroupId;
// 1. Consume string data from Kafka
@Bean
public ConsumerFactory<Integer, String> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class);
props.put(JsonDeserializer.TRUSTED_PACKAGES, "*");
return new DefaultKafkaConsumerFactory<>(props);
}
@Bean
KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Integer, String>>
kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(3);
factory.getContainerProperties().setPollTimeout(3000);
return factory;
}
//not compiling
public KafkaMessageListenerContainer<Integer, String> m1()
{
ContainerProperties containerProps = new ContainerProperties("topic1", "topic2");
containerProps.setMessageListener(new MessageListener<Integer, String>() {
@Override
public void onMessage(ConsumerRecord<Integer, String> data) {
// TODO Auto-generated method stub
}
});
DefaultKafkaConsumerFactory<Integer, String> cf =
new DefaultKafkaConsumerFactory<>(consumerFactory()); //not compiling
KafkaMessageListenerContainer<Integer, String> container =
new KafkaMessageListenerContainer<>(cf, containerProps);
return container;
}
【问题讨论】:
-
@GaryRussell 你能举个例子吗.. 他是我试过的pastebin.com/NYUTik2J
-
@GaryRussell 我的要求是假设我没有读取偏移量 7 但读取了偏移量 8,9,,...等等,并且消息偏移量 7 在主题上存在 1 天,那么我该如何触发一个警报。我正在使用 PCF 和 Spring boot
-
对于初学者,我的问题是你怎么知道偏移量 7 没有被读取?如果它没有被读取,那么你为什么要提交该偏移量以使消费者能够继续使用 8 和 9?其次,您需要一些其他消费者进程来回溯您已由主要消费者读取的数据,以仔细检查它是否真的被读取并检查时间戳(超过一天)?
-
否;这是不正确的。 Kafka 为消费者组/分区维护 2 个值 - 提交的偏移量(如果重新启动,消费者将开始的位置)和位置 - 将在下一次轮询时返回哪个记录,无论是否提交了先前的偏移量。提交偏移量只影响提交的偏移量,而不影响位置。如果侦听器抛出异常,Spring 提供了一个
SeekToCurrentErrorHandler来重新定位。见docs.spring.io/spring-kafka/docs/current/reference/html/…
标签: java spring-boot apache-kafka spring-kafka