# 动态控制kafka容器生效代码
package com.jieshun.jscpdo.bigdata.config;
import com.alibaba.cloud.nacos.refresh.NacosContextRefresher;
import com.alibaba.nacos.client.config.impl.CacheData;
import com.jieshun.jscpdo.bigdata.config.kafka.KafkaDynamicProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Hooks;
@Slf4j
@Component
public class NacosConfigListener implements ApplicationListener<RefreshScopeRefreshedEvent> {
@Autowired
private Environment environment;
@Autowired
private KafkaListenerEndpointRegistry registry;
/*@Autowired
private KafkaDynamicProperties kafkaDynamicProperties;*/
@Override
public void onApplicationEvent(RefreshScopeRefreshedEvent refreshScopeRefreshedEvent) {
// 使用 Binder 工具将 Environment 中的最新配置绑定到一个全新的对象上
KafkaDynamicProperties latestProps = Binder.get(environment)
.bind("parking-data.kafka.dynamic", Bindable.of(KafkaDynamicProperties.class))
.orElse(new KafkaDynamicProperties());
log.info("动态属性已刷新(直接从Environment读取): {}", latestProps);
// log.info("注入的kafkaDynamicProps : {}", kafkaDynamicProperties);
// for (KafkaDynamicProperties.TopicConfig tc : latestProps.getTopicConfigs()) {
// MessageListenerContainer container = registry.getListenerContainer(tc.getContainerId());
// if (container == null) continue;
//
// boolean shouldConsume = isConsumeEnabled(tc); // 将配置映射为布尔值
//
// // 方案 A:使用 pause/resume(推荐临时开关)
// if (shouldConsume) {
// if (container.isContainerPaused()) {
// container.resume();
// log.info("容器 [{}] 恢复消费", tc.getContainerId());
// } else if (!container.isRunning()) {
// // 如果容器从未启动,则启动(但 pause/resume 不改变运行状态)
// container.start();
// }
// } else {
// if (!container.isContainerPaused() && container.isRunning()) {
// container.pause();
// log.info("容器 [{}] 暂停消费", tc.getContainerId());
// }
// }
//
// // 方案 B:如果需要彻底停止(如释放资源),使用优雅 stop
// // if (!shouldConsume && container.isRunning()) {
// // container.stop(() -> log.info("容器 [{}] 已优雅停止", tc.getContainerId()));
// // } else if (shouldConsume && !container.isRunning()) {
// // container.start();
// // }
// }
Hooks h = null;
CacheData cd = null;
NacosContextRefresher ncr = null;
}
// 将配置值转换为布尔,支持多种格式
private boolean isConsumeEnabled(KafkaDynamicProperties.TopicConfig tc) {
Object val = tc.getConsumeEnabled();
if (val instanceof Boolean) {
return (Boolean) val;
} else if (val instanceof String) {
String s = (String) val;
return "open".equalsIgnoreCase(s) || "true".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s);
}
return false; // 默认关闭
}
}
代码收录 —— (一)
最新推荐文章于 2026-06-30 02:39:34 发布
835

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



