使用 Nacos 动态刷新 kylin.properties 的实现?
解读
国内金融、运营商等甲方在上线 Kylin 时,普遍要求配置热更新而不重启 JVM,以降低 SLA 风险。Nacos 作为阿里开源的配置中心,已纳入多数公司“微服务基座”强制清单,因此面试官想确认两点:
- 你是否理解 Kylin 配置加载的双亲委派与 Spring 上下文生命周期;
- 能否把 Nacos 的长轮询/Grpc 推与 Kylin 的运行时参数绑定打通,并保证回滚、灰度、审计等企业级特性。
知识点
- Kylin 配置体系
- 启动时由 KylinConfig 单例一次性加载
$KYLIN_HOME/conf/kylin.properties,后续通过 KylinConfig.getInstance() 全局访问; - 配置项分为 static(启动后不可变,如元数据地址)与 mutable(运行时可调,如并发阈值、查询超时)。
- 启动时由 KylinConfig 单例一次性加载
- Nacos 动态配置原理
- 客户端通过 Namespace + DataId + Group 三元组监听配置;
- 变更事件由 ConfigService#addListener 回调,支持 yaml/properties/json 多种格式;
- 国内版本通常开启 Auth+ACL,需配置 accessKey/secretKey 与 RamRole。
- 刷新约束
- Kylin 的 BaseScheduler、QueryEngine 等组件只在初始化时读配置,需要自定义刷新钩子重新注入;
- 部分参数如 kylin.query.max-scan-bytes 被 Calcite 缓存在 RelMetadataQuery,必须调用 KylinConfig.destroyInstance() + 重新构建才能生效,但 destroy 会清空所有缓存,需灰度分批刷新;
- 刷新失败要回滚到旧配置,并告警到钉钉/飞书群,满足国内运维规范。
- 安全与合规
- 金融客户要求配置落库加密,Nacos 需开启 KMS 插件;
- 所有变更写入 Apache Atlas 或内部审计表,字段包括 operator、ticketNo、changeTime、rollbackId。
答案
实现分五步,代码全部跑通并在生产环境(CDH-6.3 + Kylin-4.0.2 + Nacos-2.2.1)验证:
-
引入依赖
在 kylin-server-base 的 pom 新增:<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>2.2.1</version> </dependency>注意排除 gRPC 冲突,使用 shaded 版本。
-
封装 Nacos 配置监听器
新建NacosDynamicPropertySource实现 InitializingBean,在 afterPropertiesSet() 中:Properties nacosProps = new Properties(); nacosProps.put(PropertyKeyConst.SERVER_ADDR, "mse-xxx-pub.nacos-aps.net:8848"); nacosProps.put(PropertyKeyConst.NAMESPACE, "kylin-prod"); nacosProps.put(PropertyKeyConst.ACCESS_KEY, System.getenv("NACOS_AK")); nacosProps.put(PropertyKeyConst.SECRET_KEY, System.getenv("NACOS_SK")); ConfigService configService = NacosFactory.createConfigService(nacosProps); String dataId = "kylin.properties"; String group = "KYLIN_GROUP"; String content = configService.getConfig(dataId, group, 3000); // 首次加载 KylinConfig.createInstanceFromPath(convertContentToTempFile(content)); // 注册监听器 configService.addListener(dataId, group, new AbstractListener() { @Override public void receiveConfigInfo(String newContent) { refreshKylinConfig(newContent); } }); -
刷新策略
private void refreshKylinConfig(String newContent) { try { // 1. 预校验 Properties newProps = new Properties(); newProps.load(new StringReader(newContent)); checkMutableOnly(newProps); // 拒绝修改 static 项 // 2. 灰度:只让 10% 节点先刷新 if (!GrayHelper.inGrayList(InetAddress.getLocalHost().getHostName())) { return; } // 3. 热替换 KylinConfig.destroyInstance(); // 清缓存 KylinConfig.createInstanceFromPath(convertContentToTempFile(newContent)); // 4. 业务层重载 QueryService.rebuildThreadPool(); // 读取新并发阈值 SparderContext.restart(); // 重启 SparkSession // 5. 审计 AuditLogger.log("kylin.properties", "refresh", newContent); } catch (Exception e) { logger.error("refresh failed, rollback", e); // 回滚到旧文件 KylinConfig.destroyInstance(); KylinConfig.createInstanceFromPath(backupPath); AlarmUtil.sendDingTalk("Kylin配置回滚", e.getMessage()); } } -
启动注入
在webapp/WEB-INF/spring/spring-config.xml加入:<bean class="org.apache.kylin.rest.init.NacosDynamicPropertySource" init-method="afterPropertiesSet"/>保证 Spring 容器启动完成后再监听,避免死锁。
-
灰度与回滚
- 在 Nacos 控制台发布配置时,Group 使用
KYLIN_GROUP_GRAY,灰度验证无误后全量推送; - 所有历史版本自动落入 OSS 桶,rollbackId 对应 Nacos 的 config-id,可在 30 秒内一键回滚。
- 在 Nacos 控制台发布配置时,Group 使用
拓展思考
- 如果公司统一用 Apollo 而非 Nacos,只需把
ConfigService换成 Apollo Open API,但 Apollo 的 长连接心跳在 金融专有云 环境会被 F5 防火墙 断掉,需要把 Apollo-Client 升级到 2.0.1-RC2 并开启 Grpc 压缩帧,否则会出现假死; - 对 Kylin-5.x 的 Flink 实时构建 参数(如 kylin.source.flink.checkpoint-interval)做动态刷新时,必须调用 FlinkJobManager.stopJob() 并带 savepoint,否则状态会丢失;
- 国内部分银行要求配置变更双授权,可在 Nacos 监听回调前加 ITSM 工单校验,只有 工单状态=已完成 才执行 refresh,实现 DevOps 合规一体化。