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

vbscript

复制代码

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
  2. 远程运行的Arthas Spring Boot Starter的agent探针服务连接Arthas tunnel server


启动 arthas 时连接到 tunnel server

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

rust

复制代码

as.sh --tunnel-server 'ws://127.0.0.1:7777/ws'

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

bash

复制代码

,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  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:

markdown

复制代码

[arthas@86183]$ session
 Name           Value
-----------------------------------------------------
 JAVA_PID       86183
 SESSION_ID     f7273eb5-e7b0-4a00-bc5b-3fe55d741882
 AGENT_ID       URJZ5L48RPBR2ALI5K4V
 TUNNEL_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参数
启动参数

rust

复制代码

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 依赖:

arthas.version:3.6.7

xml

复制代码

<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配置

yaml

复制代码

arthas:
  agent-name: nihaotest
  agent-id: URJZ5L48RPBR2ALI5K4V  #需手工指定agent-id
  tunnel-server: ws://127.0.0.1:7777/ws

查看 Endpoint 信息

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

http://localhost:8080/actuator/arthas

json

复制代码

{
    "arthasConfigMap": {
        "agent-id": "hsehdfsfghhwertyfad",
        "tunnel-server": "ws://47.75.156.201:7777/ws",
    }
}

最后,启动SpringBoot服务即可

非 spring boot 应用使用方式

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

xml

复制代码

<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本身的服务进行探针探测。

java

复制代码

import com.taobao.arthas.agent.attach.ArthasAgent;
public class ArthasAttachExample {
  public static void main(String[] args) {
    ArthasAgent.attach();
  }
}

也可以配置属性:

java

复制代码

HashMap<String, String> configMap = new HashMap<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:

kotlin

复制代码

$ as.sh --tunnel-server 'ws://127.0.0.1:7777/ws' --app-name test
telnet connecting to arthas server... current timestamp is 1627539688
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_class demo.MathGame
pid        65825
time       2022-07-29 14:21:29
id         test_PE3LZO9NA9ENJYTPGL9L

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

bash

复制代码


http://localhost:8080/apps.html


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

bash

复制代码

http://localhost:8080/agents.html?app=test


相关文章
|
1月前
|
Prometheus 监控 Java
日志收集和Spring 微服务监控的最佳实践
在微服务架构中,日志记录与监控对系统稳定性、问题排查和性能优化至关重要。本文介绍了在 Spring 微服务中实现高效日志记录与监控的最佳实践,涵盖日志级别选择、结构化日志、集中记录、服务ID跟踪、上下文信息添加、日志轮转,以及使用 Spring Boot Actuator、Micrometer、Prometheus、Grafana、ELK 堆栈等工具进行监控与可视化。通过这些方法,可提升系统的可观测性与运维效率。
157 1
日志收集和Spring 微服务监控的最佳实践
|
2月前
|
存储 Prometheus 监控
从入门到实战:一文掌握微服务监控系统 Prometheus + Grafana
随着微服务架构的发展,系统监控变得愈发重要。本文介绍如何利用 Prometheus 和 Grafana 构建高效的监控系统,涵盖数据采集、存储、可视化与告警机制,帮助开发者提升系统可观测性,及时发现故障并优化性能。内容涵盖 Prometheus 的核心组件、数据模型及部署方案,并结合 Grafana 实现可视化监控,适合初学者和进阶开发者参考实践。
388 6
|
1月前
|
监控 Kubernetes Java
使用 New Relic APM 和 Kubernetes Metrics 监控 EKS 上的 Java 微服务
在阿里云AKS上运行Java微服务常遇性能瓶颈与OOMKilled等问题。本文教你通过New Relic实现集群与JVM双层监控,集成Helm部署、JVM代理注入、GC调优及告警仪表盘,打通从节点资源到应用内存的全链路观测,提升排障效率,保障服务稳定。
132 1
|
6月前
|
Arthas 监控 Java
Arthas memory(查看 JVM 内存信息)
Arthas memory(查看 JVM 内存信息)
487 6
|
3月前
|
存储 监控 Shell
SkyWalking微服务监控部署与优化全攻略
综上所述,虽然SkyWalking的初始部署流程相对复杂,但通过一步步的准备和配置,可以充分发挥其作为可观测平台的强大功能,实现对微服务架构的高效监控和治理。尽管未亲临,心已向往。将一件事做到极致,便是天分的展现。
|
4月前
|
Prometheus 监控 Cloud Native
|
5月前
|
Arthas 监控 Java
Arthas jvm(查看当前JVM的信息)
Arthas jvm(查看当前JVM的信息)
198 17
|
5月前
|
Arthas 监控 Java
Arthas vmoption(查看和修改 JVM里诊断相关的option)
Arthas vmoption(查看和修改 JVM里诊断相关的option)
84 16
|
4月前
|
Prometheus 监控 Cloud Native
Spring Boot 可视化监控
本文介绍了如何通过Spring Actuator、Micrometer、Prometheus和Grafana为Spring Boot应用程序添加监控功能。首先创建了一个Spring Boot应用,并配置了Spring Actuator以暴露健康状态和指标接口。接着,利用Micrometer收集应用性能数据,并通过Prometheus抓取这些数据进行存储。最后,使用Grafana将Prometheus中的数据可视化,展示在精美的仪表板上。整个过程简单易行,为Spring Boot应用提供了基本的监控能力,同时也为后续扩展更详细的监控指标奠定了基础。
743 2
|
5月前
|
Arthas 监控 Java
Arthas thread(查看当前JVM的线程堆栈信息)
Arthas thread(查看当前JVM的线程堆栈信息)
871 10

热门文章

最新文章

下一篇
oss教程