配置文件组装源码:
@Override public PropertySource<?> locate(Environment env) { nacosConfigProperties.setEnvironment(env); ConfigService configService = nacosConfigManager.getConfigService(); if (null == configService) { log.warn("no instance of config service found, can't load config from nacos"); return null; } long timeout = nacosConfigProperties.getTimeout(); nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); String name = nacosConfigProperties.getName(); String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = name; } if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = env.getProperty("spring.application.name"); } CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); loadSharedConfiguration(composite); loadExtConfiguration(composite); loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env); return composite; }加载应用配置文件的顺序源码:
private void loadApplicationConfiguration( CompositePropertySource compositePropertySource, String dataIdPrefix, NacosConfigProperties properties, Environment environment) { String fileExtension = properties.getFileExtension(); String nacosGroup = properties.getGroup(); // load directly once by default loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup, fileExtension, true); // load with suffix, which have a higher priority than the default loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); // Loaded with profile, which have a higher priority than the suffix for (String profile : environment.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true); } }顺序如下:
序号 说明1 加载dataIdPrefix对应的配置文件
2 加载dataIdPrefix.fileExtension对应的配置文件
3 加载 dataIdPrefix-activeProfiles.fileExtension对应的配置文件
2.1 NacosConfigAutoConfiguration 序号 说明
1 NacosConfigProperties nacos配置
2 NacosRefreshProperties 已经不建议被使用
3 NacosRefreshHistory 刷新历史
4 NacosConfigManager 配置
5 NacosContextRefresher 注册nacos的监听器到应用
2.2 NacosConfigEndpointAutoConfiguration
NacosConfigEndpoint
本地配置同步逻辑
@ReadOperation public Map<String, Object> invoke() { Map<String, Object> result = new HashMap<>(16); result.put("NacosConfigProperties", properties); List<NacosPropertySource> all = NacosPropertySourceRepository.getAll(); List<Map<String, Object>> sources = new ArrayList<>(); for (NacosPropertySource ps : all) { Map<String, Object> source = new HashMap<>(16); source.put("dataId", ps.getDataId()); source.put("lastSynced", dateFormat.get().format(ps.getTimestamp())); sources.add(source); } result.put("Sources", sources); result.put("RefreshHistory", refreshHistory.getRecords()); return result; }NacosConfigHealthIndicator
健康检查 UP,DOWN,UNKNOWN ;
3 NacosConnectionFailureAnalyzer连接不上nacos服务端抛出异常
@Override protected FailureAnalysis analyze(Throwable rootFailure, NacosConnectionFailureException cause) { return new FailureAnalysis( "Application failed to connect to Nacos server: \"" + cause.getServerAddr() + "\"", "Please check your Nacos server config", cause); }小结:服务通过集成该starter,通过http请求从nacos的服务端拉取配置数据,并做了 配置刷新历史,注册监听器到spring容器中, 本地缓存,和错误报告;
服务端封装源码位置:https://github.com/alibaba/nacos/tree/develop/config
应用启动读取配置文件整体调用链:待后续完成;
小结如果读完本篇文章你只能记住一句话:nacos作为配置中心可为单独的服务提供外部化配置文件,也支持多应用共享配置文件。
从nacos的客户端源码分析中可看到一些配置优先级的顺序。