@Bean
public ConsumerFactory<String,String> consumerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
configProps.put(ConsumerConfig.GROUP_ID_CONFIG, UUID.randomUUID().toString());
configProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
configProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
configProps.put("auto.offset.reset", "earliest");
return new DefaultKafkaConsumerFactory<>(configProps);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String>kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String>
factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
@Bean
public Receiver receiver(){
return new Receiver();
}
**Receiver class :**
public class Receiver {
@KafkaListener(topics = "topicname")
public void listen(String message) {System.out.println("Kakfa Listener received message " + message);
}
}
问题是“字符串消息”参数只给我消息,而不给我与Kafka消息相对应的密钥。我想同时检索密钥和消息。如果还有一种获取键值的方法,请回答。