无法从Kafka读取

时间:2018-11-01 21:39:57

标签: spring docker apache-kafka spring-kafka

我是kafka的初学者。我使用的是docker image Landoop ...我使用了console-producer和console-consumer,我讨厌在主题中读/写
现在,我正尝试在春季做同样的事情。我能够创建一个新主题,发送消息,但无法阅读它们。 这些是我的Java类:
这是配置类:

    @EnableKafka
@Configuration
public class KafkaConsumerConfig {

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> config = new HashMap<>();

        config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
        config.put(ConsumerConfig.GROUP_ID_CONFIG, "group_id");
        config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

        return new DefaultKafkaConsumerFactory<>(config);
    }

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

这是消费者

public class ConsumerService {
    @KafkaListener(topics = "user.subscription", group = "group_id")
    public void consume(String message) {
        System.out.println("Consumed message: " + message);
    }

}

非常感谢您

0 个答案:

没有答案