Arthas查看SpringBoot配置及ognl-ClassNotFoundException处理

👽个人博客:https://everspring.github.io/
👽公众号:爱历史的IT男

能看到这篇文章的同学大概率已经知道Arthas是什么了,这里就不多余赘述,本文介绍一下通过Arthas查看SpringBoot工程的配置及曾遇到过的问题。
本文相关知识:scognlspring配置保存的地方

注意:ognl是3.5.0以后出现的

  1. arthas有可以查看JVM环境变量的sysenv,也有可以查看和修改JVM的系统属性的sysprop,但SpringBoot配置文件的内容不是在环境变量和系统属性中的,而是在ConfigurableApplicationContextenvironmentConfigurableApplicationContext继承自ApplicationContext,在我们的工程中经常要用到ApplicationContext,可以从ApplicationContext入手拿到environment。
  2. 通过ognl表达式获取,ognl在java中运用广泛,比如Spring Cache的注解、Spring @Value注解都有用到。假如工程下有一个com.xxx.spring.util.SpringUtils(见文末)可以获取到ApplicationContext,想要获取到application.yml中在server.port的值,可以采用如下方法
ognl '#context=@com.xxx.util.SpringUtils@applicationContext,#context.getEnvironment().getProperty("server.port")'

#context --代表的变量
@com.xxx.util.SpringUtils --指定的类
@applicationContext --类的属性
#context.getEnvironment().getProperty(“server.port”) --通过context获取到environment属性,然后再通过environment获取到property的值

  1. 有些情况下通过ognl获取applicationContext的时候会出现ClassNotFoundException,详情如下:

Failed to execute ognl, exception message: ognl.OgnlException: Could not get static field applicationContext from class com.xxx.util.SpringUtils [java.lang.ClassNotFoundException: Unable to resolve class: com.xxx.util.SpringUtils], please check $HOME/logs/arthas/arthas.log for more details.

出现此类问题是由于classloader不是默认加载器导致,见官网issue:https://github.com/alibaba/arthas/issues/71
解决方法:

  • 1.通过sc -d 查询出当前类的classloader
sc -d com.xxx.util.SpringUtils

image-20220327234537394

获取到classLoaderHash值

  • 2.使用指定的classloader。ognl -c classloader的hash值
ognl '#context=@com.xxx.common.utils.spring.SpringUtils@applicationContext,#context.getEnvironment().getProperty("dev.name")' -c 20ad941

SpringUtils的代码

@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
    {
        SpringUtils.beanFactory = beanFactory;
    }

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

    /**
     * 获取对象
     *
     * @param name
     * @return Object 一个以所给名字注册的bean的实例
     * @throws org.springframework.beans.BeansException
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) throws BeansException
    {
        return (T) beanFactory.getBean(name);
    }
}
### 如何在Spring Boot项目中使用Arthas进行调试或性能分析 #### Maven依赖引入 为了使Arthas能够集成到Spring Boot应用程序中,需通过Maven添加`arthas-spring-boot-starter`依赖项。这一步骤确保了Arthas可以自动启动并附着于JVM进程上。 ```xml <dependency> <groupId>com.taobao.arthas</groupId> <artifactId>arthas-spring-boot-starter</artifactId> <version>3.6.3</version> </dependency> ``` 此操作简化了Arthas的安装过程,并允许开发者更方便地利用其功能来监控和诊断运行中的Java程序[^3]。 #### 配置文件设置 对于希望指定应用名称的情况,在解压后的Arthas目录下的`arthas.properties`文件或是Spring Boot项目的`application.properties`文件中定义`appName`属性即可完成配置。这一设定有助于区分不同环境下的多个实例,便于管理和追踪特定的应用行为[^1]。 #### 启动与连接Agent服务 一旦上述准备工作就绪,可以通过命令行工具或其他方式激活Arthas Agent服务。该组件负责收集关于目标系统的实时数据流,并提供接口供用户查询这些信息。通常情况下,当正确集成了`arthas-spring-boot-starter`之后,无需额外手动干预就能实现这一点;不过具体细节可能会因版本差异而有所不同。 #### 使用场景举例 假设遇到HTTP状态码错误(如404未找到资源),借助Arthas的强大能力可以帮助快速定位问题所在。例如,执行`watch`指令查看请求参数变化情况,或者运用`ttrace`跟踪方法调用链路,从而找出潜在瓶颈位置以及异常抛出处等重要线索[^2]。 #### 性能优化建议 除了基本的功能外,Arthas还提供了丰富的特性用于评估系统表现。比如,采用`thread`命令监测线程活动状况,识别是否存在死锁现象;又或者是利用`profiler`记录CPU消耗热点图谱,辅助调整算法逻辑达到更好的效率水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值