春季启动Kafka不起作用-消费者未收到消息

时间:2019-04-01 14:16:43

标签: java spring-boot apache-kafka

我正在尝试运行一个简单的Spring Boot Kafka应用程序,但无法正常运行。我遵循了各种教程,现在我正在实现this one,但是当我启动应用程序时,会发生以下情况:

enter image description here

我可以在控制台中写东西,但是使用者没有收到任何消息。
这是我的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:

spring:
  kafka:
    bootstrap-servers: localhost:8080

app:
  topic:
    foo: foo.t

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

消费类,生产者类及其配置的类与本教程中编写的相同。
在我的server.properties文件中:

zookeeper.connect=localhost:8080

和zookeeper.properties中的

clientPort=8080

application.yml中指定的相同端口。在启动应用程序之前,我运行

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

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

更新

这是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;
    }

}

这是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());
    }
}

这是Consumer类中的方法listen

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

生产者类别:

@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);
        }
}

更新2

  

[2019-04-01 17:23:52,492] INFO为客户端/ 0:0:0:0:0:0:0:1:60079(org.apache.zookeeper。)建立会话0x100435950880000,协商超时6000 server.ZooKeeperServer)   [2019-04-01 17:23:52,539]信息在处理sessionid:0x100435950880000 type:create cxid:0x1 zxid:0xef txntype:-1 reqpath:n / a错误路径:/消费者错误:KeeperErrorCode =适用于/ consumers的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,555]信息处理sessionid:0x100435950880000 type:create cxid:0x2 zxid:0xf0 txntype:-1 reqpath:n / a错误路径:/ brokers / ids错误时,出现用户级KeeperException KeeperErrorCode =用于/ brokers / ids的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,555]信息处理sessionid:0x100435950880000 type:create cxid:0x3 zxid:0xf1 txntype:-1 reqpath:n / a错误路径:/ brokers / topics错误时,出现了用户级KeeperException KeeperErrorCode =用于/ brokers / topics的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,555]信息在处理sessionid:0x100435950880000 type:create cxid:0x4 zxid:0xf2 txntype:-1 reqpath:n / a错误路径:/ config / changes错误时,出现用户级KeeperException KeeperErrorCode =用于/ config / changes的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,570]信息处理sessionid: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]信息在处理sessionid:0x100435950880000 type:create cxid:0x6 zxid:0xf4 txntype:-1 reqpath:n / a错误路径:/ brokers / seqid错误时,出现了用户级KeeperException: KeeperErrorCode = / brokers / seqid的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,586]信息在处理sessionid:0x100435950880000 type:create cxid:0x7 zxid:0xf5 txntype:-1 reqpath:n / a错误路径:/ isr_change_notification错误:KeeperErrorCode =用于/ isr_change_notification的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,586]信息在处理sessionid:0x100435950880000 type:create cxid:0x8 zxid:0xf6 txntype:-1 reqpath:n / a错误路径:/ latest_producer_id_block错误:KeeperErrorCode =用于/ latest_producer_id_block的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,586]信息处理会话ID: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]信息在处理sessionid:0x100435950880000 type:create cxid:0xa zxid:0xf8 txntype:-1 reqpath:n / a错误路径:/ config / topics错误时,出现用户级KeeperException KeeperErrorCode =用于/ config / topics的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,602]信息在处理sessionid:0x100435950880000 type:create cxid:0xb zxid:0xf9 txntype:-1 reqpath:n / a错误路径:/ config / clients错误时,出现了用户级KeeperException: KeeperErrorCode =用于/ config / clients的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,617]信息在处理sessionid:0x100435950880000 type:create cxid:0xc zxid:0xfa txntype:-1 reqpath:n / a错误路径:/ config / users错误时,出现了用户级KeeperException: KeeperErrorCode =适用于/ config / users的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:52,617]信息在处理sessionid:0x100435950880000 type:create cxid:0xd zxid:0xfb txntype:-1 reqpath:n / a错误路径:/ config / brokers错误时,出现用户级KeeperException KeeperErrorCode =用于/ config / brokers的NodeExists(org.apache.zookeeper.server.PrepRequestProcessor)   [2019-04-01 17:23:53,564]信息在处理sessionid:0x100435950880000 type:multi cxid:0x3a zxid:0xff txntype:-1 reqpath:n /时,中止了用户级的KeeperException操作。错误路径:/ admin / preferred_replica_election错误:KeeperErrorCode =用于/ admin / preferred_replica_election的NoNode(org.apache.zookeeper.server.PrepRequestProcessor)

1 个答案:

答案 0 :(得分:3)

在您的 application.yml 中,您指定了 zookeeper端口,而不是 kafka代理端口

spring:
  kafka:
    bootstrap-servers: localhost:8080

在上面,您应该定义 kafka代理的端口,即 server.properties 文件的port=的值。

默认情况下,Spring启动应用程序在端口 8080 上运行,因此,除非已更改了Spring Boot应用程序的默认端口,否则请勿将其用于Zookeeper端口。

因此,在 server.properties 中,具有port=9092zookeeper.connect=localhost:2181,在 application.yml 中,具有以下内容:

spring:
  kafka:
    bootstrap-servers: localhost:9092

然后在 zookeeper.properties 中添加clientPort=2181。然后以相同的顺序重新启动Zookeeper,Kafka服务器和Spring引导应用程序。

更新

较新版本的Kafka在server.properties文件中使用listeners=PLAINTEXT://localhost:9092而不是port=9092。因此,尝试更换它。