Spring Cloud 覆写远端的配置属性实例详解

2025-05-27 0 94

应用的配置源通常都是远端的config server服务器,默认情况下,本地的配置优先级低于远端配置仓库。如果想实现本地应用的系统变量和config文件覆盖远端仓库中的属性值,可以通过如下设置:

?

1

2

3

4

5

6
spring:

cloud:

config:

allowoverride: true

overridenone: true

overridesystemproperties: false

  • overridenone:当allowoverride为true时,overridenone设置为true,外部的配置优先级更低,而且不能覆盖任何存在的属性源。默认为false
  • allowoverride:标识overridesystemproperties属性是否启用。默认为true,设置为false意为禁止用户的设置
  • overridesystemproperties:用来标识外部配置是否能够覆盖系统属性,默认为true

客户端通过如上配置,可以实现本地配置优先级更高,且不能被覆盖。由于我们基于的spring cloud当前版本是 edgware.release ,上面的设置并不能起作用,而是使用了 propertysourcebootstrapproperties 中的默认值。具体情况见issue:https://github.com/spring-cloud/spring-cloud-commons/pull/250,我们在下面分析时会讲到具体的bug源。

源码分析

configservicepropertysourcelocator

覆写远端的配置属性归根结底与客户端的启动时获取配置有关,在获取到配置之后如何处理?我们看一下spring cloud config中的资源获取类 configservicepropertysourcelocator 的类图。

Spring Cloud 覆写远端的配置属性实例详解

configservicepropertysourcelocator 实质是一个属性资源定位器,其主要方法是 locate(environment environment) 。首先用当前运行应用的环境的application、profile和label替换configclientproperties中的占位符并初始化resttemplate,然后遍历labels数组直到获取到有效的配置信息,最后还会根据是否快速失败进行重试。主要流程如下:

Spring Cloud 覆写远端的配置属性实例详解

locate(environment environment) 调用 getremoteenvironment(resttemplate, properties, label, state) 方法通过http的方式获取远程服务器上的配置数据。实现也很简单,显示替换请求路径path中占位符,然后进行头部headers组装,组装好了就可以发送请求,最后返回结果。

在上面的实现中,我们看到获取到的配置信息存放在 compositepropertysource ,那是如何使用它的呢?这边补充另一个重要的类是propertysourcebootstrapconfiguration,它实现了applicationcontextinitializer接口,该接口会在应用上下文刷新之前 refresh() 被回调,从而执行初始化操作,应用启动后的调用栈如下:

?

1

2
springapplicationbuilder.run() -> springapplication.run() -> springapplication.createandrefreshcontext() -> springapplication.applyinitializers() -> propertysourcebootstrapconfiguration.initialize()

propertysourcebootstrapconfiguration

而上述 configservicepropertysourcelocator 的locate方法会在initialize中被调用,从而保证上下文在刷新之前能够拿到必要的配置信息。具体看一下initialize方法:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46
public class propertysourcebootstrapconfigurationimplements

applicationcontextinitializer<configurableapplicationcontext>, ordered {

private int order = ordered.highest_precedence + 10;

@autowired(required = false)

private list<propertysourcelocator> propertysourcelocators = new arraylist<>();

@override

public void initialize(configurableapplicationcontext applicationcontext){

compositepropertysource composite = new compositepropertysource(

bootstrap_property_source_name);

//对propertysourcelocators数组进行排序,根据默认的annotationawareordercomparator

annotationawareordercomparator.sort(this.propertysourcelocators);

boolean empty = true;

//获取运行的环境上下文

configurableenvironment environment = applicationcontext.getenvironment();

for (propertysourcelocator locator : this.propertysourcelocators) {

//遍历this.propertysourcelocators

propertysource<?> source = null;

source = locator.locate(environment);

if (source == null) {

continue;

}

logger.info("located property source: " + source);

//将source添加到propertysource的链表中

composite.addpropertysource(source);

empty = false;

}

//只有source不为空的情况,才会设置到environment中

if (!empty) {

//返回environment的可变形式,可进行的操作如addfirst、addlast

mutablepropertysources propertysources = environment.getpropertysources();

string logconfig = environment.resolveplaceholders("${logging.config:}");

logfile logfile = logfile.get(environment);

if (propertysources.contains(bootstrap_property_source_name)) {

//移除bootstrapproperties

propertysources.remove(bootstrap_property_source_name);

}

//根据config server覆写的规则,设置propertysources

insertpropertysources(propertysources, composite);

reinitializeloggingsystem(environment, logconfig, logfile);

setloglevels(environment);

//处理多个active profiles的配置信息

handleincludedprofiles(environment);

}

}

//...

}

下面我们看一下,在 initialize 方法中进行了哪些操作。

  • 根据默认的 annotationawareordercomparator 排序规则对propertysourcelocators数组进行排序
  • 获取运行的环境上下文configurableenvironment
  • 遍历propertysourcelocators时
  • 调用 locate 方法,传入获取的上下文environment
  • 将source添加到propertysource的链表中
  • 设置source是否为空的标识标量empty
  • source不为空的情况,才会设置到environment中

返回environment的可变形式,可进行的操作如addfirst、addlast

移除propertysources中的bootstrapproperties

根据config server覆写的规则,设置propertysources

处理多个active profiles的配置信息

初始化方法 initialize 处理时,先将所有propertysourcelocator类型的对象的 locate 方法遍历,然后将各种方式得到的属性值放到compositepropertysource中,最后调用 insertpropertysources(propertysources, composite) 方法设置到environment中。spring cloud context中提供了覆写远端属性propertysourcebootstrapproperties ,利用该配置类进行判断属性源的优先级。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
private void insertpropertysources(mutablepropertysources propertysources,

compositepropertysource composite) {

mutablepropertysources incoming = new mutablepropertysources();

incoming.addfirst(composite);

propertysourcebootstrapproperties remoteproperties = new propertysourcebootstrapproperties();

new relaxeddatabinder(remoteproperties, "spring.cloud.config")

.bind(new propertysourcespropertyvalues(incoming));

//如果不允许本地覆写

if (!remoteproperties.isallowoverride() || (!remoteproperties.isoverridenone()

&& remoteproperties.isoverridesystemproperties())) {

propertysources.addfirst(composite);

return;

}

//overridenone为true,外部配置优先级最低

if (remoteproperties.isoverridenone()) {

propertysources.addlast(composite);

return;

}

if (propertysources

.contains(standardenvironment.system_environment_property_source_name)) {

//根据overridesystemproperties,设置外部配置的优先级

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);

}

}

上述实现主要是根据 propertysourcebootstrapproperties 中的属性,调整多个配置源的优先级。从其实现可以看到 propertysourcebootstrapproperties 对象的是被直接初始化,使用的是默认的属性值而并未注入我们在配置文件中设置的。

修复后的实现:

?

1

2

3

4

5

6

7

8

9

10

11

12
@autowired(required = false)

private propertysourcebootstrapproperties remotepropertiesforoverriding;

@override

public int getorder(){

return this.order;

private void insertpropertysources(mutablepropertysources propertysources,

compositepropertysource composite) {

mutablepropertysources incoming = new mutablepropertysources();

incoming.addfirst(composite);

propertysourcebootstrapproperties remoteproperties = remotepropertiesforoverriding == null

? new propertysourcebootstrapproperties()

: remotepropertiesforoverriding;

总结

以上所述是小编给大家介绍的spring cloud 覆写远端的配置属性实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://blueskykong.com/2018/01/23/config-server2

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring Cloud 覆写远端的配置属性实例详解 https://www.kuaiidc.com/76391.html

相关文章

发表评论
暂无评论