本文受阿里開源的Nacos啟發,應用啟動后從Nacos服務加載配置到應用中,想着本地開發的時候加載配置能否從本地存儲中加載,這樣也能加快開發效率
首先
我們來看下SpringCloud項目應用Nacos服務的bootstrap.yaml
配置如下
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
discovery:
server-addr: 127.0.0.1:8848
application:
name: demo
profiles:
active: db,redis,rabbit,es,zk
然后在Nacos控制台加配置
經過如上之后,這樣應用就能從Nacos取配置。
問題點
筆者認為這里開發的時候如果能從文件系統中加載配置替代Nacos,能加快開發效率,也能心情舒暢的Coding業務代碼了。
解決思路
分析
經過分析啟動配置spring.factories
和配置類NacosConfigProperties
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.alibaba.nacos.NacosConfigBootstrapConfiguration
找到NacosConfigBootstrapConfiguration
代碼如下
@Configuration
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
public class NacosConfigBootstrapConfiguration {
@Bean
@ConditionalOnMissingBean
public NacosConfigProperties nacosConfigProperties() {
return new NacosConfigProperties();
}
@Bean
public NacosPropertySourceLocator nacosPropertySourceLocator(
NacosConfigProperties nacosConfigProperties) {
return new NacosPropertySourceLocator(nacosConfigProperties);
}
}
里面關鍵是NacosPropertySourceLocator
實現的接口PropertySourceLocator
/**
* Strategy for locating (possibly remote) property sources for the Environment.
* Implementations should not fail unless they intend to prevent the application from
* starting.
*
* @author Dave Syer
*
*/
public interface PropertySourceLocator {
/**
* @param environment The current Environment.
* @return A PropertySource, or null if there is none.
* @throws IllegalStateException if there is a fail-fast condition.
*/
PropertySource<?> locate(Environment environment);
}
到了這里就明白怎么做了。
實現
定義自己的應用啟動配置類MyLocalConfigBootstrapConfiguration
@Configuration
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", havingValue = "false")
public class MyLocalConfigBootstrapConfiguration {
@Bean
public MyLocalPropertySourceLocator fyLocalPropertySourceLocator() {
return new MyLocalPropertySourceLocator();
}
}
定義META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.liyghting.core.MyLocalConfigBootstrapConfiguration
定義自己的配置加載器MyLocalPropertySourceLocator
@Order(0)
public class MyLocalPropertySourceLocator implements PropertySourceLocator {
private static final Logger LOGGER = LoggerFactory
.getLogger(MyLocalPropertySourceLocator.class);
private static final String MYLOCAL_PROPERTY_SOURCE_NAME = "MYLOCAL";
@Override
public PropertySource<?> locate(Environment environment) {
CompositePropertySource composite = new CompositePropertySource(
MYLOCAL_PROPERTY_SOURCE_NAME);
String dataIdPrefix = environment.getProperty("spring.application.name");
String fileExtension = ".yaml";
PropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
URL baseClassesUrl = this.getClass().getClassLoader().getResource("");
File baseClassesFile = new File(baseClassesUrl.getFile());
String basePath = baseClassesFile.getParentFile().getParentFile().getAbsolutePath();
Resource resource = new FileSystemResource(basePath + "/config/" + dataIdPrefix + fileExtension);
try {
composite.addFirstPropertySource(propertySourceLoader.load(dataIdPrefix + fileExtension, resource).get(0));
} catch (IOException e) {
LOGGER.warn("can not load property source {}, exception: {}", dataIdPrefix + fileExtension, e);
}
for (String activeProfile : environment.getActiveProfiles()) {
try {
resource = resource.createRelative(dataIdPrefix + "-" + activeProfile + fileExtension);
composite.addFirstPropertySource(propertySourceLoader.load(dataIdPrefix + "-" + activeProfile + fileExtension, resource).get(0));
} catch (IOException e) {
LOGGER.warn("can not load property source {}, exception: {}", dataIdPrefix + "-" + activeProfile + fileExtension, e);
}
}
return composite;
}
}
版本信息spring-boot 2.1.6.RELEASE
spring-cloud Greenwich.SR2
spring-cloud-alibaba 0.9.0.RELEASE
具體請看我分享的git庫
新的bootstarp.yaml
配置如下
spring:
cloud:
nacos:
config:
enabled: false
server-addr: 127.0.0.1:8848
file-extension: yaml
discovery:
server-addr: 127.0.0.1:8848
application:
name: demo
profiles:
active: db,redis,rabbit,es,zk
這樣應用啟動配置能從本地文件系統加載或Nacos服務加載