简体   繁体   English

Spring引导Kafka不起作用 - 消费者没有收到消息

[英]Spring boot Kafka doesn't work - consumer not receiving messages

I'm trying to run a simple Spring Boot Kafka application but I can't make it work. 我正在尝试运行一个简单的Spring Boot Kafka应用程序,但我无法使其工作。 I've followed various tutorials, now I'm implementing this one , but when I start the application this is what happens: 我已经按照各种教程,现在我正在实现这个 ,但是当我启动应用程序时,会发生以下情况:

在此输入图像描述

I can write in the console, but the consumer doesn't receive any message. 我可以在控制台中写,但消费者不会收到任何消息。
This is my SpringApplication class: 这是我的SpringApplication类:

@SpringBootApplication(scanBasePackages = "com.springmiddleware")
@ComponentScan("com.springmiddleware")
@EnableAutoConfiguration
@EntityScan("com.springmiddleware")
public class SpringMiddlewareApplication implements CommandLineRunner{



    public static void main(String[] args) throws Exception {

        SpringApplication.run(SpringMiddlewareApplication.class, args);

    }

    @Autowired
    private Producer sender;

    @Override 
    public void run (String... strings) {
        sender.send("Hello world");
    }

}

application.yml: application.yml:

spring:
  kafka:
    bootstrap-servers: localhost:8080

app:
  topic:
    foo: foo.t

logging:
  level:
    root: ERROR
    org.springframework.web: ERROR
    com.memorynotfound: DEBUG

Consumer class, Producer Class and their configurations' class are the same as written in the tutorial. 使用者类,生产者类及其配置类与本教程中编写的相同。
In my server.properties file I have: 在我的server.properties文件中,我有:

zookeeper.connect=localhost:8080

and in zookeeper.properties: 并在zookeeper.properties中:

clientPort=8080

Same port specified in application.yml. application.yml中指定的相同端口。 Before starting the application, I run 在开始申请之前,我跑了

.\bin\windows\zookeeper-server-start.bat config\zookeeper.properties

and

.\bin\windows\kafka-server-start.bat config\server.properties

UPDATE UPDATE

This is the ReceiverConfig class: 这是ReceiverConfig类:

@EnableKafka
@Configuration
public class ReceiverConfig {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "foo");
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        return props;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }

}

This is the SenderConfig class: 这是SenderConfig类:

    @Configuration
public class SenderConfig {


    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }

    @Bean
    public ProducerFactory<String, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
}

And this is the method listen that is in the Consumer class 这是在Consumer类中的方法listen

@KafkaListener(topics = "${app.topic.foo}")
    public void listen(@Payload String message) {
        System.out.println("Received " + message);
    }

Producer class: 制片人类:

@Service
public class Producer {

     @Autowired
     private KafkaTemplate<String, String> kafkaTemplate;

     @Value("${app.topic.foo}")
        private String topic;

     public void send(String message){
            kafkaTemplate.send(topic, message);
        }
}

UPDATE 2 更新2

[2019-04-01 17:23:52,492] INFO Established session 0x100435950880000 with negotiated timeout 6000 for client /0:0:0:0:0:0:0:1:60079 (org.apache.zookeeper.server.ZooKeeperServer) [2019-04-01 17:23:52,539] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x1 zxid:0xef txntype:-1 reqpath:n/a Error Path:/consumers Error:KeeperErrorCode = NodeExists for /consumers (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,555] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x2 zxid:0xf0 txntype:-1 reqpath:n/a Error Path:/brokers/ids Error:KeeperErrorCode = NodeExists for /brokers/ids (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,555] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x3 zxid:0xf1 txntype:-1 reqpath:n/a Error Path:/brokers/topics Error:KeeperErrorCode = NodeExists for /brokers/topics (org.apach [2019-04-01 17:23:52,492] INFO已建立会话0x100435950880000,协商超时6000 for client / 0:0:0:0:0:0:0:1:60079(org.apache.zookeeper.server.ZooKeeperServer )[2019-04-01 17:23:52,539] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0x1 zxid:0xef txntype:-1 reqpath:n / a错误路径:/ consumers错误:KeeperErrorCode = / consume的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,555] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0x2 zxid:0xf0 txntype: -1 reqpath:不适用错误路径:/ brokers / ids错误:KeeperErrorCode = / Brokers / ids的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,555] INFO有用户处理sessionid时出现-level KeeperException:0x100435950880000类型:create cxid:0x3 zxid:0xf1 txntype:-1 reqpath:n / a错误路径:/ brokers / topics错误:KeeperErrorCode = / exker / topics的NodeExists(org.apach) e.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,555] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x4 zxid:0xf2 txntype:-1 reqpath:n/a Error Path:/config/changes Error:KeeperErrorCode = NodeExists for /config/changes (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,570] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x5 zxid:0xf3 txntype:-1 reqpath:n/a Error Path:/admin/delete_topics Error:KeeperErrorCode = NodeExists for /admin/delete_topics (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,570] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x6 zxid:0xf4 txntype:-1 reqpath:n/a Error Path:/brokers/seqid Error:KeeperErrorCode = NodeExists for /brokers/seqid (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,586] INFO Got user-level KeeperException when proce e.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,555] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0x4 zxid:0xf2 txntype:-1 reqpath:n / a错误路径:/ config / changes错误:KeeperErrorCode = NodeExists for / config / changes(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,570] INFO处理sessionid时得到用户级KeeperException:0x100435950880000 type:create cxid:0x5 zxid:0xf3 txntype:-1 reqpath:n / a错误路径:/ admin / delete_topics错误:KeeperErrorCode = / admin / delete_topics的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04 -01 17:23:52,570] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0x6 zxid:0xf4 txntype:-1 reqpath:n / a错误路径:/ brokers / seqid错误:KeeperErrorCode = NodeExists for / brokers / seqid(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,586] INFO在进行处理时获得了用户级别的KeeperException ssing sessionid:0x100435950880000 type:create cxid:0x7 zxid:0xf5 txntype:-1 reqpath:n/a Error Path:/isr_change_notification Error:KeeperErrorCode = NodeExists for /isr_change_notification (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,586] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x8 zxid:0xf6 txntype:-1 reqpath:n/a Error Path:/latest_producer_id_block Error:KeeperErrorCode = NodeExists for /latest_producer_id_block (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,586] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0x9 zxid:0xf7 txntype:-1 reqpath:n/a Error Path:/log_dir_event_notification Error:KeeperErrorCode = NodeExists for /log_dir_event_notification (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,602] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0xa zxid ssing sessionid:0x100435950880000类型:创建cxid:0x7 zxid:0xf5 txntype:-1 reqpath:不适用错误路径:/ isr_change_notification错误:KeeperErrorCode = / isr_change_notification的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04 -01 17:23:52,586] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0x8 zxid:0xf6 txntype:-1 reqpath:n / a错误路径:/ latest_producer_id_block错误:KeeperErrorCode = NodeExists for / latest_producer_id_block (org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,586] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0x9 zxid:0xf7 txntype:-1 reqpath:n / a错误路径:/ log_dir_event_notification错误:KeeperErrorCode = / log_dir_event_notification的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,602] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0xa zxid :0xf8 txntype:-1 reqpath:n/a Error Path:/config/topics Error:KeeperErrorCode = NodeExists for /config/topics (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,602] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0xb zxid:0xf9 txntype:-1 reqpath:n/a Error Path:/config/clients Error:KeeperErrorCode = NodeExists for /config/clients (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,617] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0xc zxid:0xfa txntype:-1 reqpath:n/a Error Path:/config/users Error:KeeperErrorCode = NodeExists for /config/users (org.apache.zookeeper.server.PrepRequestProcessor) [2019-04-01 17:23:52,617] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:create cxid:0xd zxid:0xfb txntype:-1 reqpath:n/a Error Path:/config/brokers Error:KeeperErrorCode = NodeExists for /config/brokers (org.apache.zook :0xf8 txntype:-1 reqpath:不适用错误路径:/ config / topics错误:KeeperErrorCode = / config / topics的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,602 ] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0xb zxid:0xf9 txntype:-1 reqpath:n / a错误路径:/ config / clients错误:KeeperErrorCode = / config / clients的NodeExists(org。 apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,617] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:create cxid:0xc zxid:0xfa txntype:-1 reqpath:n / a错误路径:/ config / users错误:KeeperErrorCode = / config / users的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)[2019-04-01 17:23:52,617] INFO处理sessionid时得到用户级KeeperException:0x100435950880000 type:create cxid:0xd zxid:0xfb txntype:-1 reqpath:n / a错误路径:/ config / brokers错误:KeeperErrorCode = / config / brokers的NodeExists(org.apache.zook eeper.server.PrepRequestProcessor) [2019-04-01 17:23:53,564] INFO Got user-level KeeperException when processing sessionid:0x100435950880000 type:multi cxid:0x3a zxid:0xff txntype:-1 reqpath:n/a aborting remaining multi ops. eeper.server.PrepRequestProcessor)[2019-04-01 17:23:53,564] INFO处理sessionid时得到用户级KeeperException:0x100435950880000类型:multi cxid:0x3a zxid:0xff txntype:-1 reqpath:n / a aborting remaining multi欢声笑语。 Error Path:/admin/preferred_replica_election Error:KeeperErrorCode = NoNode for /admin/preferred_replica_election (org.apache.zookeeper.server.PrepRequestProcessor) 错误路径:/ admin / preferred_replica_election错误:KeeperErrorCode = NoNode for / admin / preferred_replica_election(org.apache.zookeeper.server.PrepRequestProcessor)

In your application.yml , you have specified the zookeeper port instead of the kafka broker port application.yml中 ,您指定了zookeeper端口而不是kafka代理端口

spring:
  kafka:
    bootstrap-servers: localhost:8080

In the above, you should be defining the port of the kafka broker , ie the value of port= of the server.properties file. 在上面,您应该定义kafka代理的端口,即server.properties文件的port=的值。

A Spring boot app runs by default on port 8080 , so please don't use the same for Zookeeper port, unless you have changed the default port of the Spring boot app. Spring启动应用程序默认在端口8080上运行,因此请不要对Zookeeper端口使用相同的操作,除非您更改了Spring启动应用程序的默认端口。

So in the server.properties , have port=9092 and zookeeper.connect=localhost:2181 , and in the application.yml , have as below: 所以在server.properties中 ,有port=9092zookeeper.connect=localhost:2181 ,并在application.yml中 ,如下所示:

spring:
  kafka:
    bootstrap-servers: localhost:9092

Then in the zookeeper.properties , have clientPort=2181 . 然后在zookeeper.properties中 ,让clientPort=2181 Then restart the Zookeeper, the Kafka server and the Spring boot app in the same order. 然后以相同的顺序重新启动Zookeeper,Kafka服务器和Spring启动应用程序。

Update: 更新:

Newer versions of Kafka uses listeners=PLAINTEXT://localhost:9092 instead of port=9092 in the server.properties file. 较新版本的Kafka在server.properties文件中使用listeners=PLAINTEXT://localhost:9092而不是port=9092 So try replacing that. 所以尝试替换它。

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

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