arthas中怎么防止自身依赖的jar包与应用的jar包产生冲突

1 相关的tarce,watch命令执行怎么和应用的jar不冲突

首先可以看到arthas创建了自己的agentclassLoader

private static ClassLoader getClassLoader(Instrumentation inst, File spyJarFile, File agentJarFile) throws Throwable {
    
    // 将Spy添加到BootstrapClassLoader
    inst.appendToBootstrapClassLoaderSearch(new JarFile(spyJarFile));

    // 构造自定义的类加载器,尽量减少Arthas对现有工程的侵蚀
    return loadOrDefineClassLoader(agentJarFile);
}
private static ClassLoader loadOrDefineClassLoader(File agentJar) throws Throwable {
   
    if (arthasClassLoader == null) {
   
        arthasClassLoader = new ArthasClassloader(new URL[]{
   agentJar.toURI().toURL()});
    }
    return arthasClassLoader;
}

其中shellserver的启动通过反射调用的 ,使用的classLoader正是agentClassLoader

Class<?> classOfConfigure = agentLoader.loadClass(ARTHAS_CONFIGURE);
Object configure = classOfConfigure.getMethod(TO_CONFIGURE, String.class).invoke(null, args);
int javaPid = (Integer) classOfConfigure.getMethod(GET_JAVA_PID).invoke(configure);
Class<?> bootstrapClass = agentLoader.loadClass(ARTHAS_BOOTSTRAP);
Object bootstrap = bootstrapClass.getMethod(GET_INSTANCE, int.class, Instrumentation.class).invoke(null, javaPid, inst);
boolean isBind = (Boolean) bootstrapClass.getMethod(IS_BIND).invoke(bootstrap);
if (!isBind) {
   
    try {
   
        ps.println("Arthas start to bind...");
        bootstrapClass.getMethod(BIND, classOfConfigure).invoke(bootstrap, configure);
        ps.println("Arthas server bind success.");
        return;
    } catch (Exception e) {
   
        ps.println("Arthas server port binding failed! Please check $HOME/logs/arthas/arthas.log for more details.");
        throw e;
    }
}

反射调用的方法如下 可以看到server 的启动过程,所以后续相关的commandHandler的执行均是在agentClassLoader这个classLoader中执行的,不会跟应用的jar包冲突

public void bind(Configure configure) throws Throwable {
   
    long start = System.currentTimeMillis();

    if (!isBindRef.compareAndSet(false, true)) {
   
        throw new IllegalStateException("already bind");
    }

    String agentId = null;
    try {
   
        if (configure.getTunnelServer() != null && configure.getHttpPort() > 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值