springcloud项目配置拓展从本地config目录加载
本文受阿里开源的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服务加载
springcloud项目配置拓展从本地config目录加载的更多相关文章
- ruby -- 进阶学习(十一)配置解决production环境下无法加载css或js
最近配置production环境,找了好几份文档,从傻逼到苦逼~~终于配置成功~~@_@!!! 首先,先加载以下几个插件: # Use Uglifier as compressor for JavaS ...
- django 项目运行时static静态文件不能加载问题处理
一.首先检查网页中的加载路径是否正确,如果和文件所在路径不一致,就把html改下路径 二.加载路径和文件实际路径一致,看下配置文件: STATIC_URL = '/static/'STATIC_ROO ...
- 配置Spring的用于解决懒加载问题的过滤器
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" ...
- Bean 注解(Annotation)配置(1)- 通过注解加载Bean
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- Prism 学习:从本地目录加载 Module
在 Prism 中,将外部模块加载到主程序有以下几种方式:Code.XAML.配置文件.指定模块目录:其中,如果要使用 Code 方式来加载 Module,则需要将该 Module 引用到当前项目中: ...
- 【.NET】VS 本地调试 无法加载Json文件
1.如果要是发布到iis,还加载不出来,那就要配置一下MIME类型: 2.本地调试时,无法加载json文件: 解决方案: 在web.config中添加如下配置:mimeMap <system.w ...
- 创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或
C:\Users\xxx\Documents\IISExpress\config\applicationhost.config 将这里面带的项目路径替换成你当前路径 {"创建 caching ...
- scrapy项目5:爬取ajax形式加载的数据,并用ImagePipeline保存图片
1.目标分析: 我们想要获取的数据为如下图: 1).每本书的名称 2).每本书的价格 3).每本书的简介 2.网页分析: 网站url:http://e.dangdang.com/list-WY1-dd ...
- SSM项目使用junit单元测试时Mybaties通配符加载Mapper不能正常加载
个人博客 地址:http://www.wenhaofan.com/article/20181108104133 问题描述 项目使用maven build 以及tomcat run能够正常运行,但是使用 ...
随机推荐
- Excel催化剂开源第20波-条件格式版聚光灯功能,行列标示方便阅读
Excel聚光灯功能,辅助数据查看,选择区域下的高亮显示所在行列位置,此功能已被广大Excel开发者研究得十分透彻,各种版本的聚光灯流转在网络里,同样地也是一大堆的VBA代码,难找.Net的现成代码, ...
- vijos p1484 ISBN号码
#include<iostream>#include<string>#include<cctype>using namespace std;int main() { ...
- zstack源码编译安装(1.7.x版本)
图片没粘贴过来,请看本人gitbook吧https://www.gitbook.com/book/jingtyu/how-to-learn-zstack-code 运行环境 zstack的安装方式有很 ...
- AMD CPU环境下使用android studio,eclipse的Genymotion插件
1.下载安装VirtualBox Genymotion的运行需要此环境(链接) 2.下载安装android模拟器Genymotion 由于官网的下载速度过慢,建议直接百度下载Genymotion(链接 ...
- Clock Crossing Adapter传输效率分析 (Latency增加,传输效率降低)
原创By DeeZeng [ Intel FPGA笔记 ] 在用Nios II测试 DDR3时候发现一个现象 (测试为:写全片,读全片+比对) 用单独的PLL产生时钟(200MHz)驱动 Nios I ...
- web设计_7_页面缺失图片或CSS的情况下仍然易读
1. 在任何可能使用背景图片的地方应设置同样的颜色的背景色. 防止图片不能加载的情况下,页面内容同样保持较好可读性. 例如文字为白色,背景图为深色,如果不设置背景色,当背景图未成功加载, 而浏览器多数 ...
- vue教程二 vue组件(1)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script ...
- 【JDK】JDK源码分析-Collection
Java 集合框架(Java Collections Framework, JCF)包含很多平时开发中的常用类,例如 List.Set.ArrayList.HashMap.HashSet 等,因此打算 ...
- 最火的分布式 HTAP 数据库 TiDB - 入门实践教程
偶然在某篇博客看到了 TiDB,一个融合 OLTP 和 OLAP 的分布式开源数据库, GitHub 上 Star 很多,然后 watch 了,发现 commit 和 pull request 一直都 ...
- ASP.NET Core MVC 之局部视图(Partial Views)
1.什么是局部视图 局部视图是在其他视图中呈现的视图.通过执行局部视图生成的HTML输出呈现在调用视图中.与视图一样,局部视图使用 .cshtml 文件扩展名.当希望在不同视图之间共享网页的可重用部分 ...