做者 | sparrow
来源 | 阿里巴巴云原生公众号前端
本文来自 Arthas 2021 年 3 月征文投稿,4 月有奖征文参与方式可见文末。vue
项目最初使用 Arthas 主要有两个目的:java
由于公司还未能创建起较为统一的生产微服务配置以及状态管理的能力,各自系统的研发运维较为独立。如今项目使用了 Spring Cloud 以及 Eureka 的框架结构,和 SBA 的基础支撑能力较为匹配,同时,SBA 已经能够提供服务感知,日志级别配置管理,以及基于 actuator 的 JVM、Spring 容器的众多管理插件,能够知足基础使用的需求。git
在调研期间,Arthas 总体版本为 3.4.5,提供了基于 Webconsole 的 Tunner Server 模式,经过前面连接文章已经实践,与SBA已经能够实现集成。由于项目自己没有历史包袱,在实际集成的过程当中采用了 SBA 2.0 版本以提供更多的管理功能和图形界面能力。其余优势:github
web console 界面嵌入 SBA 总体密码登陆和网页权限管理,实现登录 SBA 后才能够使用相关 arthas web console 的功能。web
几个关键点,使用 JVM 内置 Arthas Spring Boot 插件,参考工商银行的模式创建完善的客户端下载以及修改脚本实现远程控制。内置方案工做开发量小,只须要集成相关的开源组件便可实现相关的远程使用的模式并兼顾安全。工银的方案大而全适合总体架构规划后配置专有研发团队之城。内置方案同时包含经过 JMX 的启停操做(基于 3.4.5 的 Spring Boot 插件没法得到相关句柄,暂时没法实现),默认不启动。经过远程 JMX 开通后,JVM 新增相关线程 8 个,新增虚拟机内存 30MB 左右,和本文参考的 SBA1.0 方案相同,须要考虑在线开启前 JVM 内存是否能够支持。spring
SBA 2.0 最大的方便就是提供了配置化连接外部网页的能力,同时若是网页实如今当前 JVM 进程,能够实现 Spring-Security 的本地权限管理,在生产环境下只有在登陆 SBA 后才能使用相关集成的 arthas 功能。bootstrap
参考原文 -SpringBoot Admin 集成 Arthas 实践中实现的几个步骤。tomcat
总体工程修改自 SBA 开源项目的 example 工程,具体使用 custom-ui 的工程连接为:[spring-boot-admin-sample-custom-ui]_,_红色框的部分是 arthas web console 的所有静态文件,经过 Maven Resource 的指定配置打入指定目录,实现 SBA 启动时的自定义加载。maven resource 配置--下:安全
<resource> <directory>static</directory> <targetPath>${project.build.directory}/classes/META-INF/spring-boot-admin-server-ui/extensions/arthas </targetPath> <filtering>false</filtering> </resource>
最终构建的 jar 中 META-INFO 中包含相关的文件便可在 SBA 自带的 tomcat 启动后加载到相关的静态资源,最后的 url 和自定义实现的 arthas console 配置的外部 URL 对应便可。
SBA 2.0 开始已经使用 vue 全家桶了,扩展集成均比较方便。其中,官方文档给出了外嵌链接的配置方式:[Linking / Embedding External Pages]。
参考 sba example 工程的 application.yml 配置便可:
# tag::customization-external-views[] spring: boot: admin: ui: external-views: - label: "Arthas Console" url: http://21.129.49.153:8080/ order: 1900 # end::customization-external-views[]
参考引用原实现的 SBA 集成部分,该部分主要修改实现以下功能:
参考引用原实现的 SBA 集成中插件修改以及客户端配置 application.yml。
对原版 Spring boot 插件修改主要在于原有插件是经过 Spring的@ConditionalOnMissingBean 实现自动加载。
修改主要是经过修改这部分实现经过配置文件默认不启动,而后使用时经过远程启动相关 agent 线程。
SBA client 在 maven 引入中会默认引入 jolokia-core.jar,若是没有由于 SBA client 依赖能够自行引入该包,能够实现经过 actuator 开放基于 http 的 jmx 操做能力和 SBA 控制台的相关功能无缝配合。
application.yml 中开放 management 相关配置,根据自身环境状况,也能够开在客户端侧开启 Spring security 认证,SBA 也能够很好的支持经过服务发现实现密码保护 actuator 端点的访问。
#放开management management: endpoints: web: exposure: # 这里用* 表明暴露全部端点只是为了观察效果,实际中按照需进行端点暴露 include: "*" exclude: env endpoint: health: # 详细信息显示给全部用户。 show-details: ALWAYS health: status: http-mapping: # 自定义健康检查返回状态码对应的 http 状态码 FATAL: 503
JMX 实现参考原文中 EnvironmentChangeListener 的实现思路,基于 Spring 的 JMX 注解实现便可。
@Component @ManagedResource(objectName = "com.ArthasAgentManageMbean:name=ArthasMbean", description = "Arthas远程管理Mbean") public class ArthasMbeanImpl { @Autowired private Map<String, String> arthasConfigMap; @Autowired private ArthasProperties arthasProperties; @Autowired private ApplicationContext applicationContext; /** * 初始化 * * @return */ private ArthasAgent arthasAgentInit() { arthasConfigMap = StringUtils.removeDashKey(arthasConfigMap); // 给配置全加上前缀 Map<String, String> mapWithPrefix = new HashMap<String, String>(arthasConfigMap.size()); for (Map.Entry<String, String> entry : arthasConfigMap.entrySet()) { mapWithPrefix.put("arthas." + entry.getKey(), entry.getValue()); } final ArthasAgent arthasAgent = new ArthasAgent(mapWithPrefix, arthasProperties.getHome(), arthasProperties.isSlientInit(), null); arthasAgent.init(); return arthasAgent; } @ManagedOperation(description = "获取配置Arthas Tunnel Server地址") public String getArthasTunnelServerUrl() { return arthasProperties.getTunnelServer(); } @ManagedOperation(description = "设置Arthas Tunnel Server地址,从新attach后生效") @ManagedOperationParameter(name = "tunnelServer", description = "example:ws://127.0.0.1:7777/ws") public Boolean setArthasTunnelServerUrl(String tunnelServer) { if (tunnelServer == null || tunnelServer.trim().equals("") || tunnelServer.indexOf("ws://") < 0) { return false; } arthasProperties.setTunnelServer(tunnelServer); return true; } @ManagedOperation(description = "获取AgentID") public String getAgentId() { return arthasProperties.getAgentId(); } @ManagedOperation(description = "获取应用名称") public String getAppName() { return arthasProperties.getAppName(); } @ManagedOperation(description = "获取ArthasConfigMap") public HashMap<String, String> getArthasConfigMap() { return (HashMap) arthasConfigMap; } @ManagedOperation(description = "返回是否已经加载Arthas agent") public Boolean isArthasAttched() { DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); String bean = "arthasAgent"; if (defaultListableBeanFactory.containsBean(bean)) { return true; } return false; } @ManagedOperation(description = "启动Arthas agent") public Boolean startArthasAgent() { DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); String bean = "arthasAgent"; if (defaultListableBeanFactory.containsBean(bean)) { ((ArthasAgent) defaultListableBeanFactory.getBean(bean)).init(); return true; } defaultListableBeanFactory.registerSingleton(bean, arthasAgentInit()); return true; } @ManagedOperation(description = "关闭Arthas agent,暂未实现") public Boolean stopArthasAgent() { // TODO 没法获取自定义tmp文件夹加载的classLoader,所以没法获取到com.taobao.arthas.core.server.ArthasBootstrap类并调用destroy方法 DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory(); String bean = "arthasAgent"; if (defaultListableBeanFactory.containsBean(bean)) { defaultListableBeanFactory.destroySingleton(bean); return true; } else { return false; } } }
管理工程投产后,屡次在生产环境用于问题排查和代码热修复。性能问题主要用于性能流控组件以及灰度发布相关配置参数的在线验证和 debug。
代码热加载相关初期经过 jad+mc 的方式进行操做,后续发现 jad 在部分代码上因环境配置以及 jvm 问题产生反编译代码不一致的状况,后续经过 maven 打包部署应用程序 source 压缩包的方式解决,直接使用和应用 jar 同版本构建的 source 进行修改更加可靠。总体方案在管理较为严格的生产环境提供了有效的性能分析以及热修复的能力。
现有官方提供的 com.taobao.arthas.agent.attach.ArthasAgent 中启动 arthas agent 的客户端使用的 arthasClassLoader 和 bootstrapClass 均为方法内的临时变量,外部没法获取相关句柄实现经过 bootstrapClass 关闭 arthas agent 的功能;临时解决方案为经过 JMX 启动后,在 web console 链接使用后,使用 stop 命令实现目标进程中 arthas agent 的关闭。
现有字节码加载工具能够很好的实现内部类,私有类的在线热部署替换,同时经测试能够兼容 SkyWalk8.x 版本的 javaagent 插件,可是在测试环境由于配置有 jacoco 覆盖度采集插件与 Arthas 字节码产生了不兼容的状况,在部分环境使用时须要先关闭对应的 agent 后才能正常使用 arthas 的相关功能。