环境:Springboot2.3.12.RELEASE + Spring Cloud Alibaba2.2.5.RELEASE + Spring Cloud Hoxton.SR12
应用的核心技术是:自定义PropertySourceLocator,然后配置spring.factories
在如下包中配置:
spring-cloud-context-xxx.jar中
org.springframework.cloud.bootstrap.BootstrapConfiguration=org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration
Nacos加载配置的时候默认会通过一下三个DataId加载数据:
核心类:
NacosPropertySourceLocator
- dataId=nacos-config, tenant(namespace)=7205e694-ac51-4ac1-bbe9-87c28689b88a, group=HisGroup
- loadNacosDataIfPresent(compositePropertySource, dataIdPrefix, nacosGroup,
fileExtension, true); - dataId=nacos-config.properties, tenant=7205e694-ac51-4ac1-bbe9-87c28689b88a, group=HisGroup
- loadNacosDataIfPresent(compositePropertySource,
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); - 通过定义的profile
- private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
private static final String SEP1 = "-";
private static final String DOT = ".";
for (String profile : environment.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
fileExtension, true);
}
@Configuration(proxyBeanMethods = false)@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)public class NacosConfigBootstrapConfiguration {// Nacos Config相关的配置属性@Bean@ConditionalOnMissingBeanpublic NacosConfigProperties nacosConfigProperties() {return new NacosConfigProperties();}// 管理Nacos Config相关的服务@Bean@ConditionalOnMissingBeanpublic NacosConfigManager nacosConfigManager(NacosConfigProperties nacosConfigProperties) {return new NacosConfigManager(nacosConfigProperties);}// 核心类自定义启动加载配置文件(Bootstraps)@Beanpublic NacosPropertySourceLocator nacosPropertySourceLocator(NacosConfigManager nacosConfigManager) {return new NacosPropertySourceLocator(nacosConfigManager);}}
org.springframework.cloud.bootstrap.BootstrapConfiguration=com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration
自定义属性源public class NacosPropertySourceLocator implements PropertySourceLocator {private NacosPropertySourceBuilder nacosPropertySourceBuilder;private NacosConfigProperties nacosConfigProperties;private NacosConfigManager nacosConfigManager;public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager) {this.nacosConfigManager = nacosConfigManager;this.nacosConfigProperties = nacosConfigManager.getNacosConfigProperties();}public PropertySource<?> locate(Environment env) {nacosConfigProperties.setEnvironment(env);// 关键通过NacosConfigManager获取ConfigService服务ConfigService configService = nacosConfigManager.getConfigService();// ...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);// NACOS_PROPERTY_SOURCE_NAME = NACOS// 加载共享配置loadSharedConfiguration(composite);// 加载扩展配置loadExtConfiguration(composite);// 加载应用程序配置(这里就以应用程序配置,深入查看)loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);return composite;}}
获取配置服务ConfigService配置服务是获取配置信息的核心方法:public interface ConfigService {// 获取配置String getConfig(String dataId, String group, long timeoutMs) throws NacosException;// 获取配置并设置监听String getConfigAndSignListener(String dataId, String group, long timeoutMs, Listener listener)throws NacosException;// 给配置添加监听void addListener(String dataId, String group, Listener listener) throws NacosException;// 发布配置boolean publishConfig(String dataId, String group, String content) throws NacosException;// 删除配置boolean removeConfig(String dataId, String group) throws NacosException;// 删除监听void removeListener(String dataId, String group, Listener listener);// 获取服务状态String getServerStatus();// 关闭资源服务void shutDown() throws NacosException;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- icloud照片在哪看?
- 成语老马识途的故事?成语老马识途的故事
- iCloud储存空间已满?推荐4种方法 icloud储存空间已满怎么解决
- Spring的注入模型
- SpringBoot 实现 Office 各种格式在线预览
- Spring Boot与JAX-RS框架Jersey的完美搭配
- iCloud怎么用 icloud是什么意思
- springboot集成elasticsearch
- 几行代码搞定 SpringBoot 接口恶意刷新和暴力请求
- SpringBoot配置多数据源