本文受阿里开源的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库
https://gitee.com/liyghting/SpringCloudLocalCofigDemo
新的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服务加载
分享到:
相关推荐
SpringCloud——分布式配置中心(Spring Cloud Config)之高可用的分布式配置中心
Spring Cloud Config(本地配置中心或git配置中心)搭建源码
springcloud config配置中心
1.Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为服务端和客户端。 2.服务端为分布式配置中心,是一个独立的微服务应用;客户端为分布式系统中的基础设置或微服务...
第六课: 分布式配置中心(Spring Cloud Config) 第七课: 高可用的分布式配置中心(Spring Cloud Config) 第八课: 消息总线(Spring Cloud Bus) 第九课: 服务链路追踪(Spring Cloud Sleuth) 第十课: 高可用的服务...
Spring Cloud Config + Spring Cloud Bus + kafka实现配置中心 SpringCloud微服务远程调用组件Feign的使用 springcloud-circuitbreaker.zip springcloud-config.zipspringcloud-config-oracle.zip springcloud-...
spring cloud eureka zuul ribbon hystrix feign config 示例代码
Spring Cloud配置中心获取不到最新配置信息的问题
spring cloud config&bus;整合例子包能运行,包含rabbitmq
Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新 spring cloud bus能管理和传播分布式系统间的消息,就像分布式执行器,可用于广播状态更改、时间推送等,也可以当做微服务间的通信通道 spring ...
Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务 spring-cloud-config-eureka-refresh Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh ...
主要介绍了Springboot整合Spring Cloud Kubernetes读取ConfigMap支持自动刷新配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
spring cloud中文版【Spring Cloud Config】--spring cloud中文文档
spring cloud config/bus源码解读,,包含server端,client端,bus的运行原理
SpringCloud之七 分布式配置中心SpringCloudConfig.pdf,是微服务中配置的处理使用
springcloud config分布式配置中心,需要注意gitee上创建的文件格式,特别注意不然 config client启动的时候就会报错误 说@value("${name}")的值找不到
Spring cloud示例源码,注册中心Config示例代码,参考文章https://blog.csdn.net/rishengcsdn/article/details/90288347
《深入理解Spring Cloud与微服务构建》学习笔记(二十)~配置中心Spring Cloud Config
Spring cloud 实战项目(Spring cloud zuul、spring cloud eureka、spring cloud config、 spring cloud admin 、spring cloud 微服务1、微服务2、spring cloud zipkin、网关的熔断+负载均衡、 微服务调用的熔断+...
SpringCloud Config 分布式配置中心 SpringCloud Bus 消息总线 SpringCloud Stream 消息驱动 SpringCloud Sleuth 分布式请求链路跟踪 SpringCloud Alibaba入门简介 SpringCloud Alibaba Nacos 服务注册和配置...