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能够正常运行,但是使用 ...
随机推荐
- 小白开学Asp.Net Core 《一》
在开篇中介绍了项目的搭建以及项目中所用到的第三方工具 本篇介绍SqlSugar和Dapper在本项目的实现 一.SqlSugar SqlSuagr的介绍就直接浏览官方网站,官网地址将在底部给出. 在本 ...
- React躬行记(10)——高阶组件
高阶组件(High Order Component,简称HOC)不是一个真的组件,而是一个没有副作用的纯函数,以组件作为参数,返回一个功能增强的新组件,在很多第三方库(例如Redux.Relay等)中 ...
- PhpCms V9中的{date('Y-m-d',$r[inputtime])}问题解决方法
不少朋友会碰到这个问题:在PhpCms V9中的首页或者文章内容页调用发布时间{date('Y-m-d',$r[inputtime])}调用显示1970-01-01,然后尝试用截断的方法也没有成功,应 ...
- Windows 使用 helm3 和 kubectl
简介: 主要原因是,我不会 vim ,在 linux 上修改 charts 的很蹩脚,所以就想着能不能再 windows 上执行 helm 命令,将 charts install linux 上搭建的 ...
- Shell基本语法---shell介绍
简介 1. shell是在linux系统上高效运行的脚本语言 2. 主要用来开发一些实用的.自动化的小工具,而不是用来开发具有复杂业务逻辑的中大型软件 3. shell的基本命令也是linux操作系统 ...
- setInterval循环设置并传入不同的参数
var taskId; var __sto = setInterval; window.setInterval = function(callback,timeout,param){ var args ...
- Netty源码解析—客户端启动
Netty源码解析-客户端启动 Bootstrap示例 public final class EchoClient { static final boolean SSL = System.getPro ...
- Docker 的另外两个话题: DockerHub 和 容器网络
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. Hello,大家好,我是子悠,作为本周的小编昨天已经跟大家介绍过自己了,今天就让我们继续学习江南一点雨 ...
- X-Admin&ABP框架开发-设置管理
在网站开发中,设置是不可缺少的一环,如用户设置.系统设置.甚至是租户设置等.ABP对于设置的管理已经做了很好的处理,我们可以借助巨人的力量来完成我们的冒险. ABP官网地址:https://aspne ...
- 在canvas中使用其他HTML元素
做一个功能如下图,随机生成100个大小.颜色随机的小球.点击开始运动的时候,小球开始运动,然后点击停止运动的时候,小球停止运动. 点击旁边的白色或者黑色,则背景颜色变为相应的颜色. HTML部分: & ...