繁体   English   中英

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

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

我正在尝试构建一个简单的 spring 引导 Kafka Consumer 以使用来自 kafka 主题的消息,但是由于 KafkaListener 方法没有被触发,因此没有消息被消耗。

我在其他答案中看到,以确保 AUTO_OFFSET_RESET_CONFIG 设置为“最早”并且 GROUP_ID_CONFIG 是唯一的,我这样做了,但是 KafkaListenerMethod 仍然没有触发。 该应用程序只是启动并且不执行任何操作:

Application Started

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

这是另一个 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 类:

开始.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:

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

@Service
public class KafkaConsumer {

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

KAFKA_SERVER_ADDRESS 和 TOPIC_NAME 已在我的代码中正确提供。 我还检查了该主题实际上是否已经包含消息。

关于为什么这不会消耗来自 kafka 主题的任何消息的任何想法?

Spring boot 和 spring kafka 具有自动配置功能。 对于简单的消费者,删除您的配置类,并将此属性添加到您的 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

问题是 kafka 连接问题。 从 kafka 服务器消费的机器不允许从 kafka 服务器消费。 将 kafka 服务器节点中的消费机器列入白名单解决了这个问题。

如果 kafka 服务器 url 可以解析但由于权限不足而无法连接,则 kafka 消费者不会记录。 这不是 spring kafka 特有的,但即使是普通的 kafka 客户消费者也没有记录这一点。

在您的侦听器中添加组-id。 @KafkaListener(topics = "<topic-name", groupId = ""。您还必须在 Consumer 属性中加载它。

暂无
暂无

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

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