金石原创 |【JVM实战系列】「监控调优体系」实战开发arthas-spring-boot-starter监控你的微服务是否健康!

本文主要是介绍金石原创 |【JVM实战系列】「监控调优体系」实战开发arthas-spring-boot-starter监控你的微服务是否健康!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前提介绍

相信如果经历了我的上一篇Arthas的文章[【JVM实战系列】「监控调优体系」针对于Alibaba-Arthas的安装入门及基础使用开发实战指南]之后,相信你对Arthas的功能和使用应该有了一定的理解了。那么我们就要进行下一步的探索功能。

Arthas对于SpringBoot2的支持和监控体系

在SpringBoot2应用中加入arthas-spring-boot-starter后,Spring会启动arthas服务,并且进行attach自身进程,并配合tunnel server实现远程管理。这样的方案非常适合在微服务容器环境中进行远程诊断,在容器网络环境中仅需要对外暴露tunnel server的端口。

Arthas的监控体系所需要的组件支持

  • Arthas Tunnel Server/Client(Java agent探针的管理和监控,方便我们管理服务和探针)

  • Web Console

什么是Arthas Tunnel

在容器化部署的环境内部,Java进程可以是在不同的机器启动的,想要使用Arthas去诊断会比较麻烦,因为用户通常没有机器的权限,即使登陆机器也分不清是哪个Java进程。在这种情况下,可以使用 Arthas Tunnel Server/Client。

Arthas Tunnel的作用和目的

整个Arthas的功能体系中,可以通过Arthas Tunnel Server/Client来远程管理/连接多个Agent(也就代表着可以监控多个JVM进程)。主要目的用于监控和获取目标的JVM的进程数据信息。

下载部署Arthas tunnel server
Github源码仓库下载

下载地址Arthas tunnel server,目前最新版本为arthas-all-3.6.7版本,如下图所示。

针对于Arthas的安装包进行下载资料进行介绍:

  • arthas-3.6.7.deb:主要用于debian操作系统去运行的安装包

  • arthas-bin.zip:二进制可运行执行包

  • arthas-doc.zip:针对于arthas的文档

  • arthas-tunnel-server-3.6.7-fatjar.jar:Arthas tunnel server服务的Jar可以执行包

  • Source code(zip):源码zip压缩包

  • Source code(tar.gz):源码tar包

Maven仓库下载

阿里云的下载地址:arthas.aliyun.com/download/ar…

直接运行对应的Arthas tunnel server

Arthas tunnel server是一个Spring boot fat jar 应用,直接java -jar启动:

java -jar  arthas-tunnel-server.jar
复制代码

默认情况下,arthas tunnel server的web端口是8080,Arthas agent 连接的端口是7777

打开WebConsole,分别输入Arthas agent的ip(127.0.0.1)和port(7777),和SpringBoot应用里配置的agent-id(URJZ5L48RPBR2ALI5K4V),点Connect即可。

Web Console

如果希望可以通过浏览器连接Arthas服务,此时这里的Arthas服务指的不是Arthas tunnel server,Arthas是总体的服务控制端,发送指令的部分,而Arthas tunnel server属于对接和管理agent的专门服务(依赖于Arthas Spring Boot Starter的服务)。

出了CLI模式之外,Arthas目前支持 Web Console,用户在attach成功之后,可以直接访问:http://127.0.0.1:8563/。可以填入 IP,远程连接其它机器上的arthas。启动之后,可以访问 http://127.0.0.1:8080/ ,再通过agentId连接到已注册的arthas agent 上,如下图所示。

通过Spring Boot的Endpoint,可以查看到具体的连接信息: http://127.0.0.1:8080/actuator/arthas ,

登陆用户名是arthas,密码在 arthas tunnel server 的日志里可以找到,比如:

注意:默认情况下,arthas 只 listen 127.0.0.1,所以如果想从远程连接,则可以使用 --target-ip参数指定 listen 的 IP,更多参考-h的帮助说明。 注意会有安全风险,考虑下面的 tunnel server 的方案。

如何将服务连接Arthas tunnel server

主要有两种模式连接Arthas tunnel server:

  1. 远程运行的Arthas server连接Arthas tunnel server

  1. 远程运行的Arthas Spring Boot Starter的agent探针服务连接Arthas tunnel server

启动 arthas 时连接到 tunnel server

在启动 arthas,可以传递--tunnel-server参数,比如:

as.sh --tunnel-server 'ws://127.0.0.1:7777/ws'复制代码

如果有特殊需求,可以通过--agent-id参数里指定 agentId。默认情况下,会生成随机 ID。attach 成功之后,会打印出 agentId。

  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---./  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--''--'   `--'   `--'  `--'`--' `--'`-----'wiki      https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version   3.1.2
pid       86183
time      2022-11-30 15:40:53
id        URJZ5L48RPBR2ALI5K4V复制代码

即使是启动时没有连接到 tunnel server,也可以在后续自动重连成功之后,通过 session 命令来获取 agentId:

[arthas@86183]$ sessionName           Value
-----------------------------------------------------JAVA_PID       86183SESSION_ID     f7273eb5-e7b0-4a00-bc5b-3fe55d741882AGENT_ID       URJZ5L48RPBR2ALI5K4VTUNNEL_SERVER  ws://127.0.0.1:7777/ws
复制代码

在浏览器里访问 http://localhost:8080/arthas,输入agentId,就可以连接到本机/其他机器上上的 arthas 了。

tunnel server的注意要点
  • agentId要保持唯一,否则会在 tunnel server 上冲突,不能正常工作。

  • 如果arthas agent配置了appName,则生成的agentId会带上appName的前缀。

添加对应的app-name参数
启动参数
as.sh --tunnel-server 'ws://127.0.0.1:7777/ws' --app-name demoapp ,则生成的 agentId 可能是demoapp_URJZ5L48RPBR2ALI5K4V。复制代码
Tunnel server 会以_做分隔符,提取出appName,方便按应用进行管理
配置参数

解压的 arthas 目录下的 arthas.properties,或者在 spring boot 应用的application.properties里配置appName。

Arthas Spring Boot Starter的agent服务连接Jar
只支持 springboot2
  • maven的仓库地址:search.maven.org/search?q=a:…

  • 配置 maven 依赖:

arthas.version:3.6.7

<dependency><groupId>com.taobao.arthas</groupId><artifactId>arthas-spring-boot-starter</artifactId><version>${arthas.version}</version></dependency>复制代码

应用启动后,spring 会启动 arthas,并且 attach 自身进程。如果你不知道如何创建或者引入哪些依赖,可以采用一键创建包含 Arthas Spring Boot Starter 的工程:点击跳转到云原生脚手架

可以看到最下面已经自动勾选了arthas的监控机制体系。

application.yml配置
arthas:agent-name:nihaotestagent-id:URJZ5L48RPBR2ALI5K4V#需手工指定agent-idtunnel-server:ws://127.0.0.1:7777/ws复制代码
查看 Endpoint 信息

需要配置 spring boot 暴露 endpoint:假定endpoint 端口是 8080,则通过下面 url 可以查看:

http://localhost:8080/actuator/arthas
{"arthasConfigMap":{"agent-id":"hsehdfsfghhwertyfad","tunnel-server":"ws://47.75.156.201:7777/ws",}}复制代码
最后,启动SpringBoot服务即可
非 spring boot 应用使用方式

非 Spring Boot 应用,可以通过下面的方式来使用:

<dependency><groupId>com.taobao.arthas</groupId><artifactId>arthas-agent-attach</artifactId><version>${arthas.version}</version></dependency><dependency><groupId>com.taobao.arthas</groupId><artifactId>arthas-packaging</artifactId><version>${arthas.version}</version></dependency>复制代码
attach本身的服务进行探针探测。
import com.taobao.arthas.agent.attach.ArthasAgent;
publicclassArthasAttachExample {publicstaticvoidmain(String[] args) {ArthasAgent.attach();}
}
复制代码

也可以配置属性:

HashMap<String, String> configMap = newHashMap<String, String>();
configMap.put("arthas.appName", "demo");
configMap.put("arthas.tunnelServer", "ws://127.0.0.1:7777/ws");
ArthasAgent.attach(configMap);
复制代码

Tunnel Server 的管理页面

需要在 spring boot 的application.properties里配置 arthas.enable-detail-pages=true

注意,开放管理页面有风险!管理页面没有安全拦截功能,务必自行增加安全措施。

在本地启动 tunnel-server,然后使用as.sh attach,并且指定应用名--app-name test:

$ as.sh --tunnel-server 'ws://127.0.0.1:7777/ws' --app-name test
telnet connecting to arthas server... current timestamp is1627539688
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is'^]'.,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---./  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--''--'   `--'   `--'  `--'`--' `--'`-----'wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.5.3
main_classdemo.MathGame
pid        65825
time       2022-07-2914:21:29
id         test_PE3LZO9NA9ENJYTPGL9L复制代码

然后访问 tunnel-server,可以看到所有连接的应用列表:

http://localhost:8080/apps.html
复制代码

再打开详情,则可以看到连接的所有 agent 列表:

http://localhost:8080/agents.html?app=test复制代码

作者:洛神灬殇

链接:https://juejin.cn/post/7213744681393176633

这篇关于金石原创 |【JVM实战系列】「监控调优体系」实战开发arthas-spring-boot-starter监控你的微服务是否健康!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/791467

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis