springcloud项目配置拓展从本地config目录加载
2019-08-27 18:14
1231 查看
本文受阿里开源的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); }
到了这里就明白怎么做了。
具体请看我分享的git库https://github.com/lyg123/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系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- **Spring cloud config 配置本地属性供其它客户端使用
- SpringCloud之分布式配置中心-本地配置文件(Spring Cloud Config) |第十章 -yellowcong
- Spring Cloud 本地项目从远程服务中心抓取配置文件
- springCloud config本地配置
- Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
- 在 Golang 项目中使用 Spring Cloud Config Server 管理配置
- 在 Golang 项目中使用 Spring Cloud Config Server 管理配置
- spring cloud config server 使用本地配置文件报错
- spring cloud config 如何优先读取本地配置
- Spring Cloud Config 使用本地配置文件
- spring cloud config 统一配置中心 读取Git/SVN/本地文件配置及动态刷新
- No1 OpenFirein 创建Spring Cloud Config (本地配置)
- VS加载项目时报错:尚未配置为Web项目.指定的本地IIS URL http://localhsst/..要打开项目,需要配置虚拟目录 。是否立即创建虚拟目录
- (Spring Cloud微服务实战-书中之坑)spring cloud config优先读取本地配置,其次读取git仓库
- Spring Cloud学习:06高可用分布式配置中心(Config Cluster)
- Spring Cloud教程 | 第四篇:远程配置中心账号密码设置以及高可用 | Spring Cloud Config
- 第六篇: 分布式配置中心(Spring Cloud Config)
- 第六篇: 分布式配置中心(Spring Cloud Config)
- 第七篇: 高可用的分布式配置中心(Spring Cloud Config)