【JVM實戰系列】「監控調優體系」實戰開發arthas-spring-boot-starter監控你的微服務是否健康

洛神灬殤發表於2022-12-22

前提介紹

相信如果經歷了我的上一篇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倉庫下載

阿里雲的下載地址:https://arthas.aliyun.com/download/arthas-tunnel-server/latest_version?mirror=aliyun

直接執行對應的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
  2. 遠端執行的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]$ 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引數
啟動引數
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

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

{
    "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;
public class ArthasAttachExample {
	public static void main(String[] args) {
		ArthasAgent.attach();
	}
}

也可以配置屬性:

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:

$ 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,可以看到所有連線的應用列表:

http://localhost:8080/apps.html

再開啟詳情,則可以看到連線的所有 agent 列表:

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

相關文章