金石原创 |【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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。