简体   繁体   English

Spring 引导 Kafka Consumer 未消费,Kafka Listener 未触发

[英]Spring Boot Kafka Consumer not consuming, Kafka Listener not triggering

I am trying to build a simple spring boot Kafka Consumer to consume messages from a kafka topic, however no messages get consumed as the KafkaListener method is not getting triggered.我正在尝试构建一个简单的 spring 引导 Kafka Consumer 以使用来自 kafka 主题的消息,但是由于 KafkaListener 方法没有被触发,因此没有消息被消耗。

I saw in other answers to make sure that AUTO_OFFSET_RESET_CONFIG is set to "earliest" and that the GROUP_ID_CONFIG is unique which I did, however still the KafkaListenerMethod is not triggering.我在其他答案中看到,以确保 AUTO_OFFSET_RESET_CONFIG 设置为“最早”并且 GROUP_ID_CONFIG 是唯一的,我这样做了,但是 KafkaListenerMethod 仍然没有触发。 The application simply starts and doesn't do anything:该应用程序只是启动并且不执行任何操作:

Application Started

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

This is a sub project of another gradle project and build.gradle for this subproject is as below (MAIN_CLASS_PATH has been correctly provided in the code):这是另一个 gradle 项目的子项目,该子项目的 build.gradle 如下(代码中已正确提供了 MAIN_CLASS_PATH):

apply plugin: 'application'

mainClassName = <MAIN_CLASS_PATH>

dependencies {
    compile "org.springframework.kafka:spring-kafka:${SpringKafkaVersion}"
    compile "org.springframework.boot:spring-boot-starter:${SpringBootVersion}"
    compile group: 'org.springframework', name: 'spring-tx', version: '5.2.2.RELEASE'
}

Java classes: Java 类:

Start.java:开始.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        try {
            System.out.println("Application Started");
            SpringApplication.run(Start.class, args);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

KafkaConsumerConfig.java: KafkaConsumerConfig.java:

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;

import java.util.HashMap;
import java.util.UUID;

@EnableKafka
@Configuration
public class KafkaConsumerConfig {

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        HashMap<String, Object> props = new HashMap<>();
        props.put(
                ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
                <KAFKA_SERVER_ADDRESS>);
        props.put(
                ConsumerConfig.GROUP_ID_CONFIG,
                UUID.randomUUID().toString());
        props.put(
                ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class);
        props.put(
                ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
                StringDeserializer.class);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        return new DefaultKafkaConsumerFactory<>(props);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String>
    kafkaListenerContainerFactory() {

        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }

KafkaConsumer.java KafkaConsumer.java

@Service
public class KafkaConsumer {

    @KafkaListener(topics = <TOPIC_NAME>)
    public void consume(@Payload ConsumerRecord<String, String> message) {
        System.out.println("Consumed message: " + message);
    }
}

The KAFKA_SERVER_ADDRESS and TOPIC_NAME have been correctly provided within my code. KAFKA_SERVER_ADDRESS 和 TOPIC_NAME 已在我的代码中正确提供。 Also I have checked that the topic actually contains messages in it already.我还检查了该主题实际上是否已经包含消息。

Any ideas as to why this doesn't consume any messages from a kafka topic?关于为什么这不会消耗来自 kafka 主题的任何消息的任何想法?

Spring boot and spring kafka has autoconfiguration. Spring boot 和 spring kafka 具有自动配置功能。 For simple consumer, remove your configuration class, and add this properties to your application.yml (or .properties):对于简单的消费者,删除您的配置类,并将此属性添加到您的 application.yml(或 .properties):

 spring.application.name: your-app-name
 spring.kafka.consumer.group-id: your-group-id
 spring.kafka.bootstrap-servers: server1:port1,server2:port2,server3:port3

The issue was a kafka connectivity issue.问题是 kafka 连接问题。 The machine consuming from the kafka server was not permitted to consume from the kafka server.从 kafka 服务器消费的机器不允许从 kafka 服务器消费。 WhiteListing the consuming machine in the kafka server nodes solved the issue.将 kafka 服务器节点中的消费机器列入白名单解决了这个问题。

If the kafka server url can be resolved but can not be connected to due to insufficient permissions, the kafka consumer doesn't log that.如果 kafka 服务器 url 可以解析但由于权限不足而无法连接,则 kafka 消费者不会记录。 This is not specific to spring kafka, but even the normal kafka clients consumer wasn't logging this.这不是 spring kafka 特有的,但即使是普通的 kafka 客户消费者也没有记录这一点。

In your listener add group -id.在您的侦听器中添加组-id。 @KafkaListener(topics = "<topic-name", groupId = "". You have to load this in Consumer properties also. @KafkaListener(topics = "<topic-name", groupId = ""。您还必须在 Consumer 属性中加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM