Apollo配置中心与本地配置优先级

article/2025/8/26 5:13:29

背景

在项目重构时,删除若干个application-{env}.yml文件,仅保留一个application.yml文件,该文件中保留的配置项都是几乎不会变更的配置,至于需要跟随不同环境而变更的配置项都放置在Apollo配置中心。

然后本地application.yml文件里面有一个静态配置,调用外部接口的URL:https://graph.facebook.com/v9.0/aaaaa,有一天没有得到事前通知(至少研发同事都没人注意到),这个v9.0版本的API被废弃(deprecated),进而引发严重的生产问题。
方案:

  1. 修改本地配置,然后重新部署应用;
  2. 添加Apollo配置。

故而引出问题:本地配置和Apollo配置的优先级关系是怎样的?方案2的预设前提是:Apollo的优先级高于本地配置应用,并且可以自动下发推送到客户端,即无需重新部署应用。

调研

Apollo使用的Spring @Value注解为字段注入值,那么Apollo与yml同时存在相同配置时,以谁为准?Apollo官网有此解释,在 3.1 和Spring集成的原理,结论是优先读取Apollo的配置,Apollo中没有的读取其他的。官网解释如下:

Apollo除了支持API方式获取配置,也支持和Spring/Spring Boot集成,集成原理简述如下。

Spring从3.1版本开始增加ConfigurableEnvironment和PropertySource:
ConfigurableEnvironment
Spring的ApplicationContext会包含一个Environment(实现ConfigurableEnvironment接口)
ConfigurableEnvironment自身包含了很多个PropertySource
PropertySource
属性源
可以理解为很多个Key - Value的属性配置
在运行时的结构形如:

Application ContextEnvironmentPropertySourcesPropertySource1PropertySource2

PropertySource之间是有优先级顺序的,如果有一个Key在多个property source中都存在,在前面的property source优先。

基于此原理后,Apollo和Spring/SB集成的方案就是:在应用启动阶段,Apollo从远端获取配置,然后组装成PropertySource并插入到第一个即可,即 Remote Property Source

源码

package com.ctrip.framework.apollo.spring.config;import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.spring.property.AutoUpdateConfigChangeListener;
import com.ctrip.framework.apollo.spring.util.SpringInjector;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;/*** Apollo Property Sources processor for Spring Annotation Based Application. <br /> <br />** The reason why PropertySourcesProcessor implements {@link BeanFactoryPostProcessor} instead of* {@link org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor} is that lower versions of* Spring (e.g. 3.1.1) doesn't support registering BeanDefinitionRegistryPostProcessor in ImportBeanDefinitionRegistrar* - {@link com.ctrip.framework.apollo.spring.annotation.ApolloConfigRegistrar}** @author Jason Song(song_s@ctrip.com)*/
public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware, PriorityOrdered {private static final Multimap<Integer, String> NAMESPACE_NAMES = LinkedHashMultimap.create();private static final Set<BeanFactory> AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES = Sets.newConcurrentHashSet();private final ConfigPropertySourceFactory configPropertySourceFactory = SpringInjector.getInstance(ConfigPropertySourceFactory.class);private ConfigUtil configUtil;private ConfigurableEnvironment environment;public static boolean addNamespaces(Collection<String> namespaces, int order) {return NAMESPACE_NAMES.putAll(order, namespaces);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {this.configUtil = ApolloInjector.getInstance(ConfigUtil.class);initializePropertySources();initializeAutoUpdatePropertiesFeature(beanFactory);}private void initializePropertySources() {if (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME)) {//already initializedreturn;}CompositePropertySource composite;if (configUtil.isPropertyNamesCacheEnabled()) {composite = new CachedCompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);} else {composite = new CompositePropertySource(PropertySourcesConstants.APOLLO_PROPERTY_SOURCE_NAME);}//sort by order ascImmutableSortedSet<Integer> orders = ImmutableSortedSet.copyOf(NAMESPACE_NAMES.keySet());Iterator<Integer> iterator = orders.iterator();while (iterator.hasNext()) {int order = iterator.next();for (String namespace : NAMESPACE_NAMES.get(order)) {Config config = ConfigService.getConfig(namespace);composite.addPropertySource(configPropertySourceFactory.getConfigPropertySource(namespace, config));}}// clean upNAMESPACE_NAMES.clear();// add after the bootstrap property source or to the firstif (environment.getPropertySources().contains(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME)) {// ensure ApolloBootstrapPropertySources is still the firstensureBootstrapPropertyPrecedence(environment);environment.getPropertySources().addAfter(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME, composite);} else {environment.getPropertySources().addFirst(composite);}}private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) {MutablePropertySources propertySources = environment.getPropertySources();PropertySource<?> bootstrapPropertySource = propertySources.get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);// not exists or already in the first placeif (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) {return;}propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);propertySources.addFirst(bootstrapPropertySource);}private void initializeAutoUpdatePropertiesFeature(ConfigurableListableBeanFactory beanFactory) {if (!configUtil.isAutoUpdateInjectedSpringPropertiesEnabled() || !AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.add(beanFactory)) {return;}AutoUpdateConfigChangeListener autoUpdateConfigChangeListener = new AutoUpdateConfigChangeListener(environment, beanFactory);List<ConfigPropertySource> configPropertySources = configPropertySourceFactory.getAllConfigPropertySources();for (ConfigPropertySource configPropertySource : configPropertySources) {configPropertySource.addChangeListener(autoUpdateConfigChangeListener);}}@Overridepublic void setEnvironment(Environment environment) {//it is safe enough to cast as all known environment is derived from ConfigurableEnvironmentthis.environment = (ConfigurableEnvironment) environment;}@Overridepublic int getOrder() {//make it as early as possiblereturn Ordered.HIGHEST_PRECEDENCE;}// for test onlystatic void reset() {NAMESPACE_NAMES.clear();AUTO_UPDATE_INITIALIZED_BEAN_FACTORIES.clear();}
}

验证

本地application.yml添加一个配置test: aaaaa,Controller打印配置值(不够严谨:没有加时间戳):

@RestController
public class HealthController {@Value("${test}")private String test;@RequestMapping(value = "/hs")public String hs() {System.out.println("test" + test);return "ok";}
}

启动应用程序,postman请求接口http://localhost:8080/hs,控制台输出:testaaaaa

增加Apollo配置项:
在这里插入图片描述
发布配置项,配置生效时间有延迟,等一分钟,再次请求接口,控制台打印输出依然是:testaaaaa

至于未打印时间的不够严谨的问题,给出postman截图:
在这里插入图片描述
所以,应用需要重启

结论:应用发布后,Apollo新增的配置,无论这个配置是否在本地配置文件存在与否,都不能覆盖。想要Apollo的配置生效,必须要重启(重新部署)应用,因为配置是启动时加载的

注:本文使用的Apollo为公司内部二次开发版,基于Apollo 1.4版本,开源最新版本为1.9。

热更新

public String currentUser() {log.info("levelOne" + levelOne);
}
2022-03-09 15:50:29.413 [INFO][http-nio-8080-exec-4]:c.x.c.common.services.impl.UserServiceImpl [currentUser:274] levelOne10

在这里插入图片描述
另外,再给出配置发布历史,发布前是10,发布后是15。
在这里插入图片描述

2022-03-09 15:52:58.564 [INFO][http-nio-8080-exec-1]:c.x.c.common.services.impl.UserServiceImpl [currentUser:274] levelOne10

结论:公司内部维护的Apollo版本,真是一个笑话!!!!!!!!

如何实现热更新

答案:使用@ApolloConfig

实例:

@ApolloConfig
private Config config;int one = config.getIntProperty("category.level.one", 11);
log.info("one: " + one);

变更前的日志:
2022-03-10 14:01:38.373 [INFO][http-nio-8080-exec-4]:c.x.c.common.services.impl.UserServiceImpl [currentUser:280] one: 10
配置变更:
在这里插入图片描述
变更前的日志:
2022-03-10 14:03:11.463 [INFO][http-nio-8080-exec-8]:c.x.c.common.services.impl.UserServiceImpl [currentUser:280] one: 12

其他

另外还有一个感觉很恶心的使用问题,配置项必须不能为空:
在这里插入图片描述

参考

Apollo配置中心设计
apollo配置中心与yml中存在相同配置
https://github.com/apolloconfig/apollo/issues/1800


http://chatgpt.dhexx.cn/article/oBf3RUV4.shtml

相关文章

Apollo配置中心搭建

目录 1. 下载安装包和源码包2. 创建数据库和表3. 启动Apollo服务端4. 访问Apollo客户端 1. 下载安装包和源码包 下载地址 找到要安装的版本&#xff0c;我这里选择的是1.3.0版本 下载好安装包后上传至linux的 /usr/local/src文件下并执行下面命令解压到对应文件夹 mkdir apo…

Apollo配置中心动态生效机制

看了其他大佬的文章记录一下自己追源码的过程。 Apollo配置中心动态生效机制&#xff0c;是基于Http长轮询请求和Spring扩展机制实现的&#xff0c;在Spring容器启动过程中&#xff0c;Apollo通过自定义的BeanPostProcessor和BeanFactoryPostProcessor將参数中包含${…}占位符和…

Apollo配置中心使用篇

Apollo配置中心使用篇 常见配置中心对比Apollo核心概念Apollo核心特性Apollo架构设计各模块介绍服务端设计客户端设计Apollo与Spring集成的底层原理 Apollo安装安装apollo-portalconfig service和admin service部署多网卡问题解决修改Portal环境配置调整ApolloPortal配置 Apoll…

携程 Apollo 配置中心 | 学习笔记(一) Apollo配置中心简单介绍

本章将介绍如何在Apollo配置中心中删除已经发布的项目。 专栏目录&#xff1a; 携程 Apollo 配置中心 | 学习笔记 序章 欢迎关注个人公众号&#xff1a; Coder编程 欢迎关注个人网站&#xff1a;https://coder-programming.cn/ 一、前言 之前一直学习SpringCloud, 对于配置…

Apollo 配置中心 多环境配置 Apollo Profiles 配置

Apollo 配置中心 多环境配置 Apollo Profiles 配置 一、全局的配置 1、各环境不变的参数配置在Spring Boot的 application.properties中&#xff1a; app.id你的appid apollo.bootstrap.enabledtrue apollo.bootstrap.namespaces名字空间1&#xff0c;名字空间2 (可以省略) 2、…

Apollo配置中心的基本使用

1、首先创建SpringBoot项目&#xff0c;保证可以正常启动访问 2、加入依赖包 <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.1.2</version> </…

开源配置中心之Apollo

Introduction Apollo&#xff08;阿波罗&#xff09;是携程框架部门研发的配置管理平台&#xff0c;能够集中化管理应用不同环境、不同集群的配置&#xff0c;配置修改后能够实时推送到应用端&#xff0c;并且具备规范的权限、流程治理等特性。 服务端基于Spring Boot和Sprin…

【Apollo配置中心】Apollo环境配置

一、简介 Apollo&#xff08;阿波罗&#xff09;是携程框架部门研发的分布式配置中心&#xff0c;能够集中化管理应用不同环境、不同集群的配置&#xff0c;配置修改后能够实时推送到应用端&#xff0c;并且具备规范的权限、流程治理等特性&#xff0c;适用于微服务配置管理场景…

【配置中心----Apollo】Apollo的介绍及使用方式

环境SpringBoot 2 一、Apollo简介 项目组最近的项目都是使用springcloud微服务开发&#xff0c;整个微服务框架中分布式的系统服务、集群等等都非常的多。 每一个服务都有着自己的配置&#xff08;包括参数配置、服务器地址配置、功能开关等都能&#xff09;&#xff0c;当配…

Apollo-阿波罗配置中心详细使用教程

Apollo基本概念 一、简介 Apollo - A reliable configuration management system Apollo的Github地址 Apollo&#xff08;阿波罗&#xff09;是携程框架部门研发的分布式配置中心&#xff0c;能够集中化管理应用的不同环境、不同集群的配置&#xff0c;配置修改后能够实时推送…

Apollo配置中心介绍

一、背景 最近公司订单中心重构&#xff0c;利用spring boot集成apollo配置中心&#xff0c;因此学习一下apollo配置中心 因为如今程序功能越来越复杂&#xff0c;程序的配置日益增多&#xff1a;各种功能的开关、参数配置、服务器地址、数据库链接等 对于配置的期望值越来越…

你们信不信,everyting找不全文件

everything这个软件思路,很好.查名字.找文件. 可是这个软件有两大缺点: 一,内存占用太大,500M,这还只是我1千万文件下面的情况,我还屏蔽了很大部分.要是3,4千万,everything根本启动不了. 二,搜索文件不全,不相信,看证据: 这是我保存在百度下载里面的文件,我没有加入排除列表. …

搜索工具 Everything 的简单设置

文章目录 1、常规2、界面3、结果4、视图&#xff08;重要修改&#xff09;5、字体与颜色&#xff08;重要修改&#xff09;1&#xff09;高亮部分 &#xff0c;对其 前景色 设置 自定义为 红色2&#xff09; 鼠标悬停 &#xff0c;对其 背景色 设置 自定义为 浅蓝色3&#xff0…

通过Everything 快速搭建局域网内文件服务器

文章目录 通过Everything 快速搭建局域网内文件服务器1、软件下载2、通过工具里面的Http 服务器构建局域网文件服务器3、通过局域网IP 或者自己的电脑访问 通过Everything 快速搭建局域网内文件服务器 1、软件下载 传送门 安装软件请自行完成&#xff0c;一路next 2、通过工…

Everything排除某个目录、隐藏文件、系统文件

工具——选项——索引——排除列表

Everything扫描非C盘

Tools>>Options>>Rescan Now

C# 调用Everything查找文件

Everything everything的下载 https://www.voidtools.com/zh-cn/ 在下载页面往下拉&#xff0c;我们还需要 Everything的命令行接口工具 ES.exe ES.exe的使用 在官网中也有介绍这个工具如何使用以及一些案例&#xff0c;https://www.voidtools.com/zh-cn/support/everythi…

everything-everything使用技巧,过滤文件语法

文章目录 前言技巧everything 搜索条件的与或非everything过滤文件语法 前言 everything是个神器无需多言&#xff0c;能在几秒内从几百G的windows系统文件中找到符合你需求的文件和文件夹&#xff0c;赞叹不已 技巧 everything 搜索条件的与或非 everything可以多条件搜索…

【搜索神器——Everything】的下载安装使用教程

1. 软件介绍 Everything是voidtools开发的一款文件搜索工具&#xff0c;是一个运行于Windows系统&#xff0c;基于文件、文件夹名称的快速搜索引擎&#xff0c;它在搜索之前会把所用的文件和文件夹都列出来&#xff0c;与Windows自带的搜索系统不一样&#xff0c;所以我们称之为…

【高效办公】Everything高效应用案例——软件基本信息篇

软件基本信息篇 『20』Everything软件简介"Everything"是Windows上文件名搜索引擎,其基于名称快速定位文件和文件夹。软件小巧轻便,高效易用,具有以下特点: 轻量安装文件;干净简洁的用户界面;快速文件索引;快速搜索;快速启动;最小资源使用;轻量数据库;实时…