前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >初到武汉,向你敬礼-salute|顺便解决个SkyWalking问题

初到武汉,向你敬礼-salute|顺便解决个SkyWalking问题

作者头像
狼王编程
发布2021-06-01 16:02:41
8660
发布2021-06-01 16:02:41
举报
文章被收录于专栏:狼王编程之路狼王编程之路

「武汉,我向你敬礼-salute」

「前言」

上周六我正津津有味的看着「拆弹专家2」,刘德华扮演的“潘乘风”从片头的正义,凌然,到失去一条腿之后的厌世,愤恨,到后来的失忆,直到最后的自我救赎,真的很刺激啊,可是这个时候电话响了「出差武汉」,好吧,看完剩下的影片,吃个饭,回家开始订酒店,订机票,收拾行李,第二天出发武汉

解决问题

这次来「武汉」,工作就是进行架构对接,对各种组件的相关问题进行沟通和协商,然后对方就抛出了一个「问题」,这个「问题」呢主要来源于「SkyWalking」这个「分布式链路追踪」「SkyWalking」这个组件我也看到最近有好多文章都写了,后面我也会单独写一篇来介绍一下,这次主要来看看这个问题,它到底怎么解决

遇到的问题

1.使用「SkyWalking Agent」时,某些其他代理(例如「Arthas」)无法正常工作

2.Java代理重新转换类在「SkyWalking」代理上失败

原因

当Java应用程序启动时,「SkyWalking」代理使用ByteBuddy转换类。ByteBuddy每次都会生成具有不同随机名称的辅助类。

当另一个Java代理重新转换相同的类时,它将触发「SkyWalking」代理再次增强该类。更改了由ByteBuddy重新生成的字节码,修改了字段和导入的类名,有关类字节码的JVM验证失败,导致重新转换失败。

解决方案

1.首先「SkyWalking」的版本需要升级到8.1版本之上

2.启用类缓存功能 添加JVM参数:-Dskywalking.agent.is_cache_enhanced_class=true -Dskywalking.agent.class_cache_mode=MEMORY

或取消注释选项agent.conf:

代码语言:javascript
复制
# If true, SkyWalking agent will cache all instrumented classes files to memory or disk files (decided by class cache mode),
# allow other javaagent to enhance those classes that enhanced by SkyWalking agent.
agent.is_cache_enhanced_class = ${SW_AGENT_CACHE_CLASS:false}

# The instrumented classes cache mode: MEMORY or FILE
# MEMORY: cache class bytes to memory, if instrumented classes is too many or too large, it may take up more memory
# FILE: cache class bytes to user temp folder starts with 'class-cache', automatically clean up cached class files when the application exits
agent.class_cache_mode = ${SW_AGENT_CLASS_CACHE_MODE:MEMORY}

如果启用了类缓存功能,请将检测到的类字节码保存到内存或临时文件中。当其他Java代理重新转换同一类时,「SkyWalking」代理首先尝试从缓存加载。

如果找到了缓存的类,则将直接使用它,而无需重新生成新的随机名称辅助类,这不会影响后续java代理的处理。

3.Class缓存保存模式 建议将缓存类放入内存中,与此同时,如果它占用更多的内存资源。另一种选择是使用本地文件系统。通过以下选项设置类缓存模式

代码语言:javascript
复制
-Dskywalking.agent.class_cache_mode=MEMORY:将缓存类保存到Java内存中。
-Dskywalking.agent.class_cache_mode=FILE:将缓存类保存到SkyWalking代理路径“ / class-cache”。

或修改以下选项agent.conf:

代码语言:javascript
复制
agent.class_cache_mode = ${SW_AGENT_CLASS_CACHE_MODE:MEMORY}
agent.class_cache_mode = ${SW_AGENT_CLASS_CACHE_MODE:FILE}
测试验证

官方提供了一个测试demo: retransform-conflict-demo.jar

说明:该应用程序通过ByteBuddy动态附加Java代理,获取Instrumentation对象,然后调用instrumentation.retransformClasses()以获得目标类的字节码。

在不开启类缓存的情况下启动demo

代码语言:javascript
复制
java -javaagent:/apache-skywalking-apm-bin/agent/skywalking-agent.jar -jar retransform-conflict-demo.jar
代码语言:javascript
复制
before retransform:
com.example.demo.TestController$auxiliary$ckZJlKPI
com.example.demo.TestController
com.example.demo.TestController$auxiliary$DNtevU4I

retransform:
java.lang.ClassFormatError
        at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
        at java.instrument/sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:167)
        at com.example.demo.DemoApplication.reTransform(DemoApplication.java:69)
        at com.example.demo.DemoApplication.main(DemoApplication.java:30)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

after retransform:
com.example.demo.TestController$auxiliary$twNcBbRq
com.example.demo.TestController$auxiliary$ckZJlKPI
com.example.demo.TestController
com.example.demo.TestController$auxiliary$DNtevU4I

check retransform classes:
retransform classes not equal.

开启类缓存的情况下启动demo

代码语言:javascript
复制
java -Dskywalking.agent.is_cache_enhanced_class=true -javaagent:/apache-skywalking-apm-bin/agent/skywalking-agent.jar -jar retransform-conflict-demo.jar
代码语言:javascript
复制
before retransform:
com.example.demo.TestController$auxiliary$lNjbfE8J
com.example.demo.TestController
com.example.demo.TestController$auxiliary$vmhaXoD8

retransform:

after retransform:
com.example.demo.TestController$auxiliary$lNjbfE8J
com.example.demo.TestController
com.example.demo.TestController$auxiliary$vmhaXoD8

check retransform classes:
retransform classes successful.
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-12-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 狼王编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 「前言」
    • 解决问题
      • 遇到的问题
      • 原因
      • 解决方案
      • 测试验证
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档