简体   繁体   中英

Spring-kafka example from documentation doesn't work

I am reading the following doc: and try to run code:

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

But listen method is not invoked.

Why?

PS I checked console and sure that topic exists:

 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

but topic is empty:

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

I just copied your code and it worked perfectly fine for me with boot 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

I suggest you turn on DEBUG logging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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