前言
Arthas 是 Alibaba 开源的 Java 诊断工具,具有实时查看系统的运行状况;查看函数调用参数、返回值和异常;在线热更新代码;秒解决类冲突问题;定位类加载路径;生成热点;通过网页诊断线上应用。如今在各大厂都有广泛应用,也延伸出很多产品。
这里将介绍如何将 Arthas 集成进 Spring Boot 监控平台中。
SpringBoot Admin
为了方便,SpringBoot Admin 简称为 SBA(版本:1.5.x)。
1.5 版本的 SBA 如果要开发插件比较麻烦,需要下载 SBA 的源码包,再按照 Spring-boot-admin-server-ui-hystrix的形式 Copy 一份,由于 JS 使用的是 Angular,本人尝试了很久,虽然掌握了如何开发插件,奈何不会 Angular,遂放弃

版本:2.x 2.x 版本的 SBA 插件开发,官网有介绍如何开发,JS 使用 Vue,方便很多,由于我们项目还在使用 1.5,所以并没有使用该版本,请读者自行尝试。
不能使用 SBA 的插件进行集成,那还有什么办法呢?
SBA 集成
鄙人的办法是将 Arthas 的相关文件直接 Copy 到 Admin 服务中,这些文件都来自 Arthas-all 项目 Tunnel-server。

admin 目录结构
1. Arthas 目录
该包下存放的是所有 Arthas 的 Java 文件。
- Endpoint 包下的文件可以都注释掉,没多大用。
- ArthasController 这个文件是我自己新建的,用来获取所有注册到 Arthas 的客户端,这在后面是有用的。
- 其他文件直接 Copy 过来就行。
@RequestMapping("/api/arthas") @RestController public class ArthasController { @Autowired private TunnelServer tunnelServer; @RequestMapping(value = "/clients", method = RequestMethod.GET) public Set<String> getClients() { Map<String, AgentInfo> agentInfoMap = tunnelServer.getAgentInfoMap(); return agentInfoMap.keySet(); } }
spring-boot-admin-server-ui
该文件建在 Resources.META-INF 下,Admin 会在启动的时候加载该目录下的文件。

2. Resources 目录
- index.html 覆盖 SBA 原来的首页,在其中添加一个 Arthas 导航

<!DOCTYPE html> <html class="no-js"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Spring Boot Admin</title> <meta name="description" content=""> <meta name="viewport" content="width=device-width"> <link rel="shortcut icon" type="image/x-icon" href="img/favicon.png"/> <link rel="stylesheet" type="text/css" href="core.css"/> <link rel="stylesheet" type="text/css" href="all-modules.css"/> </head> <body> <header class="navbar header--navbar desktop-only"> <div class="navbar-inner"> <div class="container-fluid"> <div class="spring-logo--container"> <a class="spring-logo" href="#"><span></span></a> </div> <div class="spring-logo--container"> <a class="spring-boot-logo" href="#"><span></span></a> </div> <ul class="nav pull-right"> <!--增加Arthas导航--> <li class="navbar-link ng-scope"> <a class="ng-binding" href="arthas/arthas.html">Arthas</a> </li> <li ng-repeat="view in mainViews" class="navbar-link" ng-class="{active: $state.includes(view.state)}"> <a ui-sref="{{view.state}}" ng-bind-html="view.title"></a> </li> </ul> </div> </div> </header> <div ui-view></div> <footer class="footer"> <ul class="inline"> <li><a href="https://codecentric.github.io/spring-boot-admin/@project.version@" target="_blank">Reference Guide</a></li> <li>-</li> <li><a href="https://github.com/codecentric/spring-boot-admin" target="_blank">Sources</a></li> <li>-</li> <li>Code licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a></li> </ul> </footer> <script src="dependencies.js" type="text/javascript"></script> <script type="text/javascript"> sbaModules = []; </script> <script src="core.js" type="text/javascript"></script> <script src="all-modules.js" type="text/javascript"></script> <