文档中的Spring-kafka示例不起作用

时间:2017-10-13 14:21:25

标签: java spring apache-kafka spring-kafka

我正在阅读following doc: 并尝试运行代码:

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args).close();
    }

    @Autowired
    private KafkaTemplate<String, String> template;

    private final CountDownLatch latch = new CountDownLatch(3);

    @Override
    public void run(String... args) throws Exception {
        this.template.send("spring_kafka_topic", "foo1");
        this.template.send("spring_kafka_topic", "foo2");
        this.template.send("spring_kafka_topic", "foo3");
        latch.await(60, TimeUnit.SECONDS);
        logger.info("All received");
    }

    @KafkaListener(topics = "spring_kafka_topic")
    public void listen(ConsumerRecord<?, ?> cr) throws Exception {
        logger.info(cr.toString());
        latch.countDown();
    }
}

但未调用listen方法。

为什么?

P.S。我检查了控制台并确定该主题存在:

 D:\work\kafka\kafka_2.11-0.11.0.1>bin\windows\kafka-topics.bat --list --zookeeper localhost:2181
__consumer_offsets
myTopic
my_topic
new_topic
part_2_example_1
spring_kafka_topic
test

但主题为空:

D:\work\kafka\kafka_2.11-0.11.0.1>bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic spring_kafka_topic --from-beginning

D:\work\kafka\kafka_2.11-0.11.0.1>

application.properties

spring.kafka.consumer.group-id=foo
spring.kafka.consumer.auto-offset-reset=earliest

1 个答案:

答案 0 :(得分:0)

我刚刚复制了你的代码,它对我来说非常适合启动1.5.7 ......

2017-10-13 12:13:13.379  INFO 3537 --- [ntainer#0-0-C-1] com.example.So46732065Application        : ConsumerRecord(topic = spring_kafka_topic, partition = 0, offset = 0, CreateTime = 1507909008962, checksum = 4047989513, serialized key size = -1, serialized value size = 4, key = null, value = foo1)
2017-10-13 12:13:13.381  INFO 3537 --- [ntainer#0-0-C-1] com.example.So46732065Application        : ConsumerRecord(topic = spring_kafka_topic, partition = 0, offset = 1, CreateTime = 1507909008989, checksum = 4214835548, serialized key size = -1, serialized value size = 4, key = null, value = foo2)
2017-10-13 12:13:13.381  INFO 3537 --- [ntainer#0-0-C-1] com.example.So46732065Application        : ConsumerRecord(topic = spring_kafka_topic, partition = 0, offset = 2, CreateTime = 1507909008989, checksum = 2352904650, serialized key size = -1, serialized value size = 4, key = null, value = foo3)

$ kafka-console-consumer --bootstrap-server localhost:9092 --topic spring_kafka_topic --from-beginning
foo1
foo2
foo3

$ kafka-consumer-groups --bootstrap-server localhost:9092 --describe --group foo
GROUP                          TOPIC                          PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             OWNER
foo                            spring_kafka_topic             0          3               3               0               consumer-1_/127.0.0.1

我建议您启用DEBUG日志记录。