文
章
目
录
章
目
录
本文主要讲解关于如何使用Arthas 获取 Spring 应用运行时配置值相关内容,让我们来一起学习下吧!
背景
众所周之,Spring 应用的配置注入方式非常多。除了我们熟悉的方式,比如
- System Properties/System Env
- application.properties/application.yaml
- spring profiles
- spring cloud config
还有很多配置注入的方式,令人眼花缭乱
获取运行时具体配置
对于开发人员来说,在运行时怎样确定某个配置是否生效?它的具体值是什么?
用Arthas可以一行命令获取。
比如获取server.port
的具体值:
$ vmtool --action getInstances --className org.springframework.context.ConfigurableApplicationContext --express 'instances[0].getEnvironment().getProperty("server.port")'
@String[7001]
获取具体的配置来源
但是怎样具体是从哪个配置源来的?
-
对于 spring boot应用,可以打开一个新的 terminal 窗口,执行
telnet 127.0.0.1 3658
连接上Arthas。 - 直接 watch 下面的函数
-
在原来窗口用上面的
vmtool
命令来获取server.port
的值
可以看到 watch 返回结果里有具体的配置来源application.yml
:
$ watch org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource findConfigurationProperty
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 2) cost in 217 ms, listenerId: 5
method=org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource.findConfigurationProperty location=AtExit
ts=2023-11-27 16:08:07.696; [cost=0.327042ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
@ConfigurationPropertySourcesPropertySource[ConfigurationPropertySourcesPropertySource {name='configurationProperties'}],
@ConfigurationProperty[[ConfigurationProperty@18fb3ec9 name = server.port, value = 7001, origin = class path resource [application.yml] - 2:9]],
]
总结
- 用 vmtool 来获取 spring context
- 调用 Environment().getProperty 来获取具体的值
- 通过 watch spring boot 内部的 findConfigurationProperty 函数来获取具体的配置来源
以上就是关于如何使用Arthas 获取 Spring 应用运行时配置值相关的全部内容,希望对你有帮助。欢迎持续关注潘子夜个人博客(www.panziye.com),学习愉快哦!