我的spring / java消费者无法访问生产者产生的消息。但是,当我从控制台/终端运行使用者时,它可以接收spring / java生产者产生的消息。
消费者配置:
@Component
@ConfigurationProperties(prefix="kafka.consumer")
public class KafkaConsumerProperties {
private String bootstrap;
private String group;
private String topic;
public String getBootstrap() {
return bootstrap;
}
public void setBootstrap(String bootstrap) {
this.bootstrap = bootstrap;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
}
监听器配置:
@Configuration
@EnableKafka
public class KafkaListenerConfig {
@Autowired
private KafkaConsumerProperties kafkaConsumerProperties;
@Bean
public Map<String, Object> getConsumerProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConsumerProperties.getBootstrap());
properties.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaConsumerProperties.getGroup());
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "100");
properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "15000");
return properties;
}
@Bean
public Deserializer stringKeyDeserializer() {
return new StringDeserializer();
}
@Bean
public Deserializer transactionJsonValueDeserializer() {
return new JsonDeserializer(Transaction.class);
}
@Bean
public ConsumerFactory<String, Transaction> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(getConsumerProperties(), stringKeyDeserializer(), transactionJsonValueDeserializer());
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, Transaction> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, Transaction> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConcurrency(1);
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
Kafka监听器:
@Service
public class TransactionConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(Transaction.class);
@KafkaListener(topics={"transactions"}, containerFactory = "kafkaListenerContainerFactory")
public void onReceive(Transaction transaction) {
LOGGER.info("transaction = {}",transaction);
}
}
消费者应用程序:
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
测试案例1:通过 我开始了我的spring / java生产者,并从控制台运行了使用者。当我生成消息表单生产者时,我的控制台使用者可以访问该消息。
测试案例2:失败 我启动了spring / java消费者,并从控制台运行了生产者。当我生成消息表单控制台生成器时,我的spring / java使用者无法访问消息。
测试案例3:失败 我开始了我的spring / java消费者,并运行了spring / java生产者。当我从spring / java生产者生成消息表单时,spring / java使用者无法访问消息。
问题
使用者代码中有什么问题吗?
我是否缺少对kafka-listener的任何配置?
是否需要显式运行侦听器? (我不这么认为,因为我可以在终端日志中看到连接到主题的信息,但仍然不确定)
答案 0 :(得分:0)
好吧,您在AUTO_OFFSET_RESET_CONFIG
中缺少Consumer Configs
properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
自动偏移重置
当Kafka中没有初始偏移量或服务器上不再存在当前偏移量时(例如因为该数据已被删除),该怎么办:
最早的:将偏移量自动重置为最早的偏移量
最新:将偏移量自动重置为最新的偏移量
无:如果未找到消费者组的先前偏移量,则向消费者抛出异常
其他任何地方:向消费者抛出异常
注意::auto.offset.reset
至earliest
仅在该消费者组的kafka没有偏移量时才有效(因此,您需要将此属性与新的消费者组一起添加并重新启动应用程序)