Arthas 获取 Spring Context 动态修改 Spring Boot 配置文件中的值

Arthas Spring Boot 诊断工具 About 3,071 words

案例代码

Spring Boot配置文件:application.yml

config:
  url: http://www.baidu.com
  isTest: true
  maxConn: 100

配置类:TestConfig

@Data
@Configuration
@ConfigurationProperties(prefix = "config")
public class TestConfig {

    private String url;

    private Boolean isTest;

    private Integer maxConn;

}

@Value方式获取配置

package com.example.arthas.controller;

@Slf4j
@RestController
public class Test111Controller {

    @Value("${config.maxConn}")
    private Integer maxConn;

    @GetMapping("/test1")
    public Result test1(@RequestParam String key) {
        log.info("test1111111111111#{}", key);
        List<String> list = (List<String>) CollectionUtils.arrayToList(new String[]{"aaa", "bbb", "ccc"});
        return Result.builder().msg("请求成功#" + maxConn).data(list).build();
    }

}

获取 Spring Context

SpringMVC 方式

监听Web请求,有接口请求进入服务会被记录进Time Tunnel

tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod -n 1

获取Spring Context对象。

tt -i 1000 -w 'target.getApplicationContext()'

获取@Value方式赋值的值(private修饰的也可获取)。

tt -i 1000 -w 'target.getApplicationContext().getBean("test111Controller").maxConn'

修改@Value赋值的maxConn200(原始设置的为100)。

tt -i 1000 -w 'target.getApplicationContext().getBean("test111Controller").maxConn=200'

获取配置类方式配置的字段。

tt -i 1000 -w 'target.getApplicationContext().getBean("testConfig").isTest'

修改isTestfalse

tt -i 1000 -w 'target.getApplicationContext().getBean("testConfig").isTest=false'

ognl 方式一

对于Spring Boot项目还可以在入口类中SpringApplication.run接收ApplicationContext,注意用静态变量接收。

@SpringBootApplication
public class ArthasApplication {

    private static ConfigurableApplicationContext cac;

    public static void main(String[] args) {
        cac = SpringApplication.run(ArthasApplication.class, args);
    }

}

获取Spring Context对象。

ognl '@com.example.arthas.ArthasApplication@cac'

获取Spring Context管理的Bean类。

ognl '@com.example.arthas.ArthasApplication@cac.getBean("testConfig")'

修改配置类中的属性。

ognl '@com.example.arthas.ArthasApplication@cac.getBean("testConfig").url="http://www.google.com"'

ognl 方式二

需自定义一个获取Spring Context对象。

package com.example.arthas.util;

@Component
public class SpringContextUtil implements ApplicationContextAware {

    // Arthas ognl获取applicationContext
    // ognl '@com.example.arthas.util.SpringContextUtil@applicationContext'
    public static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringContextUtil.applicationContext == null) {
            SpringContextUtil.applicationContext = applicationContext;
        }
    }

}

获取Spring Context对象。

ognl '@com.example.arthas.util.SpringContextUtil@applicationContext'

获取Spring Context管理的Bean类。

ognl '@com.example.arthas.util.SpringContextUtil@applicationContext.getBean("testConfig")'

修改配置类中的属性。

ognl '@com.example.arthas.util.SpringUtils@applicationContext.getBean("testConfig").url="http://www.google.com"'

备注

即使Arthas停止后(stop),修改过的字段还是生效。

参考

https://github.com/alibaba/arthas/issues/482

Views: 7,345 · Posted: 2021-05-02

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh