Skip to content

Commit

Permalink
add native agent module (#2915)
Browse files Browse the repository at this point in the history
  • Loading branch information
flzj-kl authored Oct 23, 2024
1 parent 87a3153 commit 74290ec
Show file tree
Hide file tree
Showing 93 changed files with 4,064 additions and 1 deletion.
80 changes: 80 additions & 0 deletions cluster-management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

## Arthas Native Agent - 集群管理

![](images/cluster_management.png)

# 快速开始

## 启动native-agent
native-agent,启动在需要动态attach的服务器上
启动参数

| 参数 | 必填 | 解释 |
|----------------------|-----|-------------------------------------|
| http-port | N | http端口 ,默认2671 |
| ws-port | N | ws端口,默认2672 |
| registration-typ | Y | 注册中心类型(目前实现的有etcd和zookeeper,推荐etcd) |
| registration-address | Y | 注册中心的地址 |

example:
```shell
java -jar native-agent.jar --ip 164.196.97.123 --http-port 2671 --ws-port 2672 --registration-type etcd --registration-address 164.196.97.123:2379
```

## 启动native-agent-proxy
做为native-agent和native-agent-management-web的网络中转

| 参数 | 必填 | 解释 |
|---------------------------------|-----|------------------------------------------------------------------|
| port | N | http/ws端口 ,默认2233 |
| ip | Y | proxy的ip |
| management-registration-type | Y | native-agent-manangement-web的注册中心类型(目前实现的有etcd和zookeeper,推荐etcd) |
| management-registration-address | Y | native-agent-manangement-webd的注册中心地址 |
| agent-registration-type | Y | native-agent的注册中心类型(目前实现的有etcd和zookeeper,推荐etcd) |
| agent-registration-address | Y | native-agent的注册中心地址 |


example:
```shell
java -jar native-agent-proxy.jar --ip 164.196.97.123 --management-registration-type etcd --management-registration-address 164.196.97.123:2379 --agent-registration-type etcd --agent-registration-address 164.196.97.123:2379
```

## 启动native-agent-management-web
native-agent的管理页面

| 参数 | 必填 | 解释 |
|----------------------|-----|-------------------------------------|
| port | N | http端口 ,默认3939 |
| registration-typ | Y | 注册中心类型(目前实现的有etcd和zookeeper,推荐etcd) |
| registration-address | Y | 注册中心的地址 |


example:
```shell
java -jar native-agent-management-web.jar --registration-type etcd --registration-address 164.196.97.123:2379
```


## 监控指定JVM
进入native-agent-server管理页面,点击VIEW JAVA PROCESS INFO 按钮,可以查看到当前服务器上的Java进程
![](images/native_agent_list.png)
进入到Java进程页后,我们可以点击Monitor按钮,Monitor目标Java进程
![](images/native_agent_java_process_page.png)
之后点击MONITOR按钮就可以进入到监控界面了
![](images/native_agent_moniotr_page.png)

# 扩展注册中心
目前实现的有zookeeper和etcd,如果想要扩展注册中心,可以看看下面的实现。下面演示的是native-agent-management-web的扩展,其他也是同样的道理。

需要实现com.alibaba.arthas.nat.agent.management.web.discovery.NativeAgentProxyDiscovery接口,并在META-INF/arthas/com.alibaba.arthas.native.agent.management.web.NativeAgentProxyDiscoveryFactory 添加上你的实现
```properties
zookeeper=com.alibaba.arthas.nat.agent.management.web.discovery.impl.ZookeeperNativeAgentProxyDiscovery
etcd=com.alibaba.arthas.nat.agent.management.web.discovery.impl.EtcdNativeAgentProxyDiscovery
注册中心名称=你的实现
```
# 添加你的实现
注册中心名称=你实现类的具体路径
之后你启动native-agent-management-web就可以,通过--registration-type参数,来指定你实现的注册中心
```shell
java -jar native-agent-management-web.jar --registration-type 注册中心名称 --registration-address 注册中心的地址
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cluster-management/images/native_agent_list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions cluster-management/native-agent-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>arthas-all</artifactId>
<groupId>com.taobao.arthas</groupId>
<version>${revision}</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>native-agent-common</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-codec-http</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
</dependency>

<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-core</artifactId>
<version>0.7.0</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.arthas.nat.agent.common.constants;

/**
* @description: hello world
* @author:flzjkl
* @date: 2024-09-22 0:47
*/
public class NativeAgentConstants {

public static final int ARTHAS_SERVER_HTTP_PORT = 8563;

public static final int MAX_HTTP_CONTENT_LENGTH = 1024 * 1024 * 10;

public static final String NATIVE_AGENT_KEY = "/native-agent";

public static final String NATIVE_AGENT_PROXY_KEY = "/native-agent-proxy";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.alibaba.arthas.nat.agent.common.dto;

/**
* @description: NativeAgentInfoDTO
* @author:flzjkl
* @date: 2024-09-05 8:04
*/
public class NativeAgentInfoDTO {
private String ip;
private Integer httpPort;
private Integer wsPort;

public NativeAgentInfoDTO() {

}

public NativeAgentInfoDTO(String ip, Integer httpPort, Integer wsPort) {
this.ip = ip;
this.httpPort = httpPort;
this.wsPort = wsPort;
}

public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public Integer getHttpPort() {
return httpPort;
}

public void setHttpPort(Integer httpPort) {
this.httpPort = httpPort;
}

public Integer getWsPort() {
return wsPort;
}

public void setWsPort(Integer wsPort) {
this.wsPort = wsPort;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.alibaba.arthas.nat.agent.common.handler;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

/**
* @description: HttpOptionRequestHandler
* @author:flzjkl
* @date: 2024-09-22 7:21
*/
public class HttpOptionRequestHandler {

public FullHttpResponse handleOptionsRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
FullHttpResponse response = new DefaultFullHttpResponse(
request.getProtocolVersion(),
HttpResponseStatus.OK,
Unpooled.EMPTY_BUFFER);

// Set the CORS response header
String origin = request.headers().get(HttpHeaderNames.ORIGIN);
if (origin != null) {
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
} else {
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
}
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, DELETE, OPTIONS");
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE, 3600L);
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "Content-Type, Authorization, X-Requested-With, Accept, Origin");

// If the request contains an Access-Control-Request-Method, a response is required
String accessControlRequestMethod = request.headers().get(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD);
if (accessControlRequestMethod != null) {
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS, accessControlRequestMethod);
}

// If the request contains Access-Control-Request-Headers, a response is required
String accessControlRequestHeaders = request.headers().get(HttpHeaderNames.ACCESS_CONTROL_REQUEST_HEADERS);
if (accessControlRequestHeaders != null) {
response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, accessControlRequestHeaders);
}

return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.alibaba.arthas.nat.agent.common.utils;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* @description: OkHttpUtil
* @author:flzjkl
* @date: 2024-10-20 21:35
*/
public class OkHttpUtil {

private static final OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();

private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

public static String get(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();

try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}

public static String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder()
.url(url)
.post(body)
.header("Content-Type", "application/json")
.build();

try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.alibaba.arthas.nat.agent.common.utils;


/**
* @description: WelcomeUtil
* @author:flzjkl
* @date: 2024-09-22 18:26
*/
public class WelcomeUtil {

public static void printNativeAgentWelcomeMsg() {
String welcomeMsg = " _ _ _ \n" +
" _ __ __ _ | |_ (_) __ __ ___ __ _ __ _ ___ _ __ | |_ \n" +
" | '_ \\ / _` | | __| | | \\ \\ / / / _ \\ / _` | / _` | / _ \\ | '_ \\ | __|\n" +
" | | | | | (_| | | |_ | | \\ V / | __/ | (_| | | (_| | | __/ | | | | | |_ \n" +
" |_| |_| \\__,_| \\__| |_| \\_/ \\___| \\__,_| \\__, | \\___| |_| |_| \\__|\n" +
" |___/ ";
System.out.println(welcomeMsg);
System.out.println("=======================================================================================================================");
}

public static void printManagementWebWelcomeMsg() {
String welcomeMsg = " _ _ _ _ _ \n" +
" _ __ __ _ | |_ (_) __ __ ___ __ _ __ _ ___ _ __ | |_ _ __ ___ __ _ _ __ __ _ __ _ ___ _ __ ___ ___ _ __ | |_ __ __ ___ | |__ \n" +
" | '_ \\ / _` | | __| | | \\ \\ / / / _ \\ / _` | / _` | / _ \\ | '_ \\ | __| | '_ ` _ \\ / _` | | '_ \\ / _` | / _` | / _ \\ | '_ ` _ \\ / _ \\ | '_ \\ | __| \\ \\ /\\ / / / _ \\ | '_ \\ \n" +
" | | | | | (_| | | |_ | | \\ V / | __/ | (_| | | (_| | | __/ | | | | | |_ | | | | | | | (_| | | | | | | (_| | | (_| | | __/ | | | | | | | __/ | | | | | |_ \\ V V / | __/ | |_) |\n" +
" |_| |_| \\__,_| \\__| |_| \\_/ \\___| \\__,_| \\__, | \\___| |_| |_| \\__| |_| |_| |_| \\__,_| |_| |_| \\__,_| \\__, | \\___| |_| |_| |_| \\___| |_| |_| \\__| \\_/\\_/ \\___| |_.__/ \n" +
" |___/ |___/ ";
System.out.println(welcomeMsg);
System.out.println("=========================================================================================================================================================================================================================");
}

public static void printProxyWelcomeMsg() {
String welcomeMsg = " _ _ _ \n" +
" _ __ __ _ | |_ (_)__ __ ___ __ _ __ _ ___ _ __ | |_ _ __ _ __ ___ __ __ _ _ \n" +
"| '_ \\ / _` || __|| |\\ \\ / / / _ \\ / _` | / _` | / _ \\| '_ \\ | __| | '_ \\ | '__| / _ \\ \\ \\/ /| | | |\n" +
"| | | || (_| || |_ | | \\ V / | __/ | (_| || (_| || __/| | | || |_ | |_) || | | (_) | > < | |_| |\n" +
"|_| |_| \\__,_| \\__||_| \\_/ \\___| \\__,_| \\__, | \\___||_| |_| \\__| | .__/ |_| \\___/ /_/\\_\\ \\__, |\n" +
" |___/ |_| |___/ ";
System.out.println(welcomeMsg);
System.out.println("==========================================================================================================================");
}

}
Loading

0 comments on commit 74290ec

Please sign in to comment.