场景:配置中心为 consul spring 集成 kafka 时 项目中的所有 配置包括 kafka 统一都从 配置中心 consul 中读取
第一步:实现 InitializingBean接口
public class TopicConf implements InitializingBean {
Consul consul = ConsulClient.singleConsul();
@Override
public void afterPropertiesSet() throws Exception {
System.setProperty("topics", consul.keyValueClient().getValuesAsString(ConsulConf.LEGO_DOMAIN_TOPIC_UPLOAD).get(0).trim());
}
}
然后将上述的 类 注入到 Spring 容器中

第二步: 配置监听 listener

这样就可以实现在 @KafkaListener 中动态实现 topic 的配置
本文介绍如何使用Spring框架集成Kafka消息队列和Consul配置中心,通过实现InitializingBean接口动态从Consul读取Kafka配置,实现动态topic配置。
1394

被折叠的 条评论
为什么被折叠?



