NacosConfigManager
public class NacosConfigManager {private static ConfigService service = null;private NacosConfigProperties nacosConfigProperties;public NacosConfigManager(NacosConfigProperties nacosConfigProperties) {this.nacosConfigProperties = nacosConfigProperties;// 创建配置服务createConfigService(nacosConfigProperties);}static ConfigService createConfigService(NacosConfigProperties nacosConfigProperties) {if (Objects.isNull(service)) {synchronized (NacosConfigManager.class) {try {if (Objects.isNull(service)) {// 通过工厂创建服务// assembleConfigServiceProperties方法// 就是收集关于Nacos所有配置信息,如:地址,端口,用户名,密码等// 详细查看NacosConfigProperties#assembleConfigServicePropertiesservice = NacosFactory.createConfigService(nacosConfigProperties.assembleConfigServiceProperties());}}// ...}}return service;}}// NacosFactorypublic class NacosFactory {public static ConfigService createConfigService(Properties properties) throws NacosException {return ConfigFactory.createConfigService(properties);}}public class ConfigFactory {public static ConfigService createConfigService(Properties properties) throws NacosException {try {Class<?> driverImplClass = Class.forName("com.alibaba.nacos.client.config.NacosConfigService");Constructor constructor = driverImplClass.getConstructor(Properties.class);ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties);// 通过反射构造了NacosConfigservice,同时设置属性信息// 这些属性信息就是一些Naocs服务的地址,端口,用户名,密码等信息return vendorImpl;} catch (Throwable e) {throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);}}}
到此就得到了一个ConfigService服务类NacosConfigService 。
获取应用程序配置接着95.2获取到了ConfigService以后继续执行
public class NacosPropertySourceLocator implements PropertySourceLocator {private NacosPropertySourceBuilder nacosPropertySourceBuilder;public PropertySource<?> locate(Environment env) {nacosConfigProperties.setEnvironment(env);// 关键通过NacosConfigManager获取ConfigService服务ConfigService configService = nacosConfigManager.getConfigService();// ...nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,timeout);CompositePropertySource composite = new CompositePropertySource(NACOS_PROPERTY_SOURCE_NAME);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 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// 这里会根据不同配置的profiles再次加载不同环境的配置for (String profile : environment.getActiveProfiles()) {String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,fileExtension, true);}}private void loadNacosDataIfPresent(final CompositePropertySource composite,final String dataId, final String group, String fileExtension,boolean isRefreshable) {if (null == dataId || dataId.trim().length() < 1) {return;}if (null == group || group.trim().length() < 1) {return;}// 加载Nacos属性源NacosPropertySource propertySource = this.loadNacosPropertySource(dataId, group,fileExtension, isRefreshable);this.addFirstPropertySource(composite, propertySource, false);}private NacosPropertySource loadNacosPropertySource(final String dataId,final String group, String fileExtension, boolean isRefreshable) {// ...return nacosPropertySourceBuilder.build(dataId, group, fileExtension,isRefreshable);}private void addFirstPropertySource(final CompositePropertySource composite,NacosPropertySource nacosPropertySource, boolean ignoreEmpty) {if (null == nacosPropertySource || null == composite) {return;}if (ignoreEmpty && nacosPropertySource.getSource().isEmpty()) {return;}composite.addFirstPropertySource(nacosPropertySource);}}// 这里面开始加载数据public class NacosPropertySourceBuilder {NacosPropertySource build(String dataId, String group, String fileExtension,boolean isRefreshable) {Map<String, Object> p = loadNacosData(dataId, group, fileExtension);NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,p, new Date(), isRefreshable);NacosPropertySourceRepository.collectNacosPropertySource(nacosPropertySource);return nacosPropertySource;}// 加载数据private Map<String, Object> loadNacosData(String dataId, String group,String fileExtension) {String data = https://www.isolves.com/it/cxkf/kj/2022-06-13/null;try {// 通过ConfigService加载配置内容(从远程服务获取)data = configService.getConfig(dataId, group, timeout);if (StringUtils.isEmpty(data)) {return EMPTY_MAP;}Map
推荐阅读
- icloud照片在哪看?
- 成语老马识途的故事?成语老马识途的故事
- iCloud储存空间已满?推荐4种方法 icloud储存空间已满怎么解决
- Spring的注入模型
- SpringBoot 实现 Office 各种格式在线预览
- Spring Boot与JAX-RS框架Jersey的完美搭配
- iCloud怎么用 icloud是什么意思
- springboot集成elasticsearch
- 几行代码搞定 SpringBoot 接口恶意刷新和暴力请求
- SpringBoot配置多数据源