sc 使用了配置中心后,如何设置远程和本地配置的优先级
在 spring 中,如何获取一个 key 的值?
applicationContext.getEnvironment().getProperty("swagger.show")
那么 key 的优先级呢?spring 会加载所有的配置文件,获取 key 的 value 时,会从前往后遍历这些配置文件,找到了即返回。所以,靠前的优先级高
sc 中加载远程配置文件的逻辑:
// org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration#initialize
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
CompositePropertySource composite = new CompositePropertySource(
BOOTSTRAP_PROPERTY_SOURCE_NAME);
// NacosPropertySourceLocator 实现了这个接口,order 为 0
AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
boolean empty = true;
ConfigurableEnvironment environment = applicationContext.getEnvironment();
for (PropertySourceLocator locator : this.propertySourceLocators) {
PropertySource<?> source = null;
// nacos 拉取配置
source = locator.locate(environment);
if (source == null) {
continue;
}
logger.info("Located property source: " + source);
composite.addPropertySource(source);
empty = false;
}
if (!empty) {
MutablePropertySources propertySources = environment.getPropertySources();
String logConfig = environment.resolvePlaceholders("${logging.config:}");
LogFile logFile = LogFile.get(environment);
if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
}
// 把远程配置加入到 environment 中
insertPropertySources(propertySources, composite);
reinitializeLoggingSystem(environment, logConfig, logFile);
setLogLevels(applicationContext, environment);
handleIncludedProfiles(environment);
}
}
确定配置优先级的逻辑:
// org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration#insertPropertySources
private void insertPropertySources(MutablePropertySources propertySources,
CompositePropertySource composite) {
MutablePropertySources incoming = new MutablePropertySources();
incoming.addFirst(composite);
PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
Binder.get(environment(incoming)).bind("spring.cloud.config", Bindable.ofInstance(remoteProperties));
if (!remoteProperties.isAllowOverride() || (!remoteProperties.isOverrideNone()
&& remoteProperties.isOverrideSystemProperties())) {
// 远程配置优先级高
propertySources.addFirst(composite);
return;
}
if (remoteProperties.isOverrideNone()) {
// 远程配置优先级低
propertySources.addLast(composite);
return;
}
if (propertySources
.contains(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) {
if (!remoteProperties.isOverrideSystemProperties()) {
propertySources.addAfter(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
}
else {
propertySources.addBefore(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
composite);
}
}
else {
propertySources.addLast(composite);
}
}
如果需要使用本地配置覆盖远程配置,设置 spring.cloud.config.overrideNone = true
sc 使用了配置中心后,如何设置远程和本地配置的优先级的更多相关文章
- spring cloud 加入配置中心后的 部分 配置文件优先级
经过一个小时的实验,得出部分配置优先级如下: bootstrap.properties:位于jar包外的优先级最高 application.properties:配置中心的文件 > 命令行配置 ...
- AgileConfig 轻量级配置中心 1.5 发布 - 支持多环境配置
AgileConfig 从发布到现在,收到不同学的 issue 说需要多环境的支持.也就是一个应用在不同的环境下可以配置不同的配置项.这是一个非常有用的功能,就跟我们开发的时候会设置多个 appset ...
- mysql 添加用户 - 设置远程登录/本地登陆的权限
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户: mysql> GRANT A ...
- nacos作为配置中心动态刷新@RefreshScope添加后取值为null的一个问题
之前springboot项目常量类如下形式: @Component @RefreshScope//nacos配置中心时添加上 public class Constants { @Value(" ...
- 【微服务】之三:从零开始,轻松搞定SpringCloud微服务-配置中心
在整个微服务体系中,除了注册中心具有非常重要的意义之外,还有一个注册中心.注册中心作为管理在整个项目群的配置文件及动态参数的重要载体服务.Spring Cloud体系的子项目中,Spring Clou ...
- zookeeper配置中心实战--solrcloud zookeeper配置中心原理及源码分析
程序的发展,需要引入集中配置: 随着程序功能的日益复杂,程序的配置日益增多:各种功能的开关.参数的配置.服务器的地址…… 并且对配置的期望也越来越高,配置修改后实时生效,灰度发布,分环境.分集群管理配 ...
- Spring Cloud Consul使用——配置中心
1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- Zookeeper系列四:Zookeeper实现分布式锁、Zookeeper实现配置中心
一.Zookeeper实现分布式锁 分布式锁主要用于在分布式环境中保证数据的一致性. 包括跨进程.跨机器.跨网络导致共享资源不一致的问题. 1. 分布式锁的实现思路 说明: 这种实现会有一个缺点,即当 ...
- Spring Cloud(十四)Config 配置中心与客户端的使用与详细
前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...
随机推荐
- 动态规划(股票交易)---只能进行 k 次的股票交易
只能进行 k 次的股票交易 188. Best Time to Buy and Sell Stock IV (Hard) 题目描述: 只能进行K次股票交易,求能获得的最大利润 思路分析: 和只 ...
- Paper Reading_Distributed System
最近(以及预感接下来的一年)会读很多很多的paper......不如开个帖子记录一下读paper心得 Mark一个上海交通大学东岳网络工作室的paper notebook Mark一个大神的笔记 Ed ...
- scala学习笔记(8)
1.trait ------------------------------- 如果只有一个trait就使用extends进行扩展,如果是多个,就使用with对生于trait进行扩展 trait lo ...
- 当页面完全加载完成后执行一个JS函数
方法1.如下程序,当页面完全加载后执行openTheIndexPage()方法 <html> <head> <meta http-equiv="Conte ...
- 46. Permutations (JAVA)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- ubuntu下docker安装
首先来一个官网安装教程链接:https://docs.docker.com/install/linux/docker-ce/ubuntu/ 目前docker主要有docker-CE 与 docker- ...
- 一、JsonTree
一.JsonTree [ {"id":"4","pid":"1","name":"大家电& ...
- 10年前文章_解决parted 编译出错问题
找到 include/site/i386-linux 文件,修改定义ac_cv_sizeof_off_t 的长度,从原来的4改为8 ac_cv_sizeof_off_t=8 删除 build_dir/ ...
- Manacher || Luogu P3805【模板】manacher算法
题面:[模板]manacher算法 代码: #include<cstdio> #include<cstring> #include<iostream> #defin ...
- 使用glew和glad 新建窗口
一.添加头文件 首先,将头文件加到项目的.cpp文件中 #include <glad/glad.h> 2 #include <GLFW/glfw3.h> 注: 包含glad的头 ...