如何使用Arthas 获取 Spring 应用运行时配置值

后端 潘老师 5个月前 (12-07) 166 ℃ (0) 扫码查看

本文主要讲解关于如何使用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]

获取具体的配置来源

但是怎样具体是从哪个配置源来的?

  1. 对于 spring boot应用,可以打开一个新的 terminal 窗口,执行 telnet 127.0.0.1 3658连接上Arthas。
  2. 直接 watch 下面的函数
  3. 在原来窗口用上面的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]],
]

总结

  1. 用 vmtool 来获取 spring context
  2. 调用 Environment().getProperty 来获取具体的值
  3. 通过 watch spring boot 内部的 findConfigurationProperty 函数来获取具体的配置来源

以上就是关于如何使用Arthas 获取 Spring 应用运行时配置值相关的全部内容,希望对你有帮助。欢迎持续关注潘子夜个人博客(www.panziye.com),学习愉快哦!


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/back/12192.html
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】