feat: update native agent proxy register logic

pull/2930/head
flzj_kl 3 months ago
parent ed48afb4eb
commit d64869a839

@ -7,28 +7,26 @@
![](asserts/cluster_management_single.png)
## 单体模式启动native-agent-management-web
native-agent的管理页面
启动参数
| 参数 | 必填 | 解释 |
|----------------------|-----|-------------------------------------|
| port | N | http端口 默认3939 |
native-agent的管理页面启动需要指定prxoy地址启动参数
| 参数 | 必填 | 解释 |
|---------------|-----|-----------------------|
| port | N | http端口 默认3939 |
| proxy-address | Y | native-agent-proxy的地址 |
example
```shell
java -jar native-agent-management-web.jar
java -jar native-agent-management-web.jar --proxy-address 161.169.97.114:2233
```
## 单体模式启动native-agent-proxy
proxy会向native-agent-management-web注册自己
proxy启动参数
| 参数 | 必填 | 解释 |
|--------------------|-----|--------------------------------------|
| port | N | http/ws端口 默认2233 |
| ip | Y | proxy的ip |
| management-address | Y | native-agent-manangement-web的地址用于注册 |
```shell
java -jar native-agent-proxy.jar --ip 127.0.0.1 --management-address 127.0.0.1:3939
java -jar native-agent-proxy.jar
```
## 单体模式启动native-agent

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 29 KiB

@ -2,14 +2,8 @@ package com.alibaba.arthas.nat.agent.management.web.discovery.impl;
import com.alibaba.arthas.nat.agent.management.web.discovery.NativeAgentProxyDiscovery;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @description: NativeAgentManagementNativeAgentProxyDiscovery(Σ( °Д °;) )
@ -18,32 +12,11 @@ import java.util.concurrent.TimeUnit;
*/
public class NativeAgentManagementNativeAgentProxyDiscovery implements NativeAgentProxyDiscovery {
/**
* key: native agent ip : http port : ws port , value: expiration time
*/
private static Map<String, LocalDateTime> nativeAgentProxyMap = new ConcurrentHashMap<>();
private final static int INITIAL_DELAY_SECONDS = 5;
private final static int PERIOD_SECONDS = 5;
public static void storageNativeAgent(String address, LocalDateTime expirationTime) {
nativeAgentProxyMap.put(address, expirationTime);
}
public static void nativeAgentProxyCheckScheduled () {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
LocalDateTime now = LocalDateTime.now();
nativeAgentProxyMap.forEach((key, expirationTime) ->{
if (now.isAfter(expirationTime)) {
nativeAgentProxyMap.remove(key);
}
});
};
scheduler.scheduleAtFixedRate(task, INITIAL_DELAY_SECONDS, PERIOD_SECONDS, TimeUnit.SECONDS);
}
public static String proxyAddress;
@Override
public List<String> listNativeAgentProxy(String address) {
return new ArrayList<>(nativeAgentProxyMap.keySet());
return Arrays.asList(proxyAddress);
}
}

@ -30,7 +30,7 @@ import java.util.Arrays;
*/
@Name("arthas-native-agent-management-web")
@Summary("Bootstrap Arthas Native Management Web")
@Description("EXAMPLES:\n" + "java -jar native-agent-management-web.jar\n"
@Description("EXAMPLES:\n" + "java -jar native-agent-management-web.jar --proxy-address 161.169.97.114:2233\n"
+ "java -jar native-agent-management-web.jar --registration-type etcd --registration-address 161.169.97.114:2379\n"
+ "java -jar native-agent-management-web.jar --port 3939 --registration-type etcd --registration-address 161.169.97.114:2379\n"
+ "https://arthas.aliyun.com/doc\n")
@ -38,6 +38,7 @@ public class NativeAgentManagementWebBootstrap {
private static final Logger logger = LoggerFactory.getLogger(NativeAgentManagementWebBootstrap.class);
private static final int DEFAULT_NATIVE_AGENT_MANAGEMENT_WEB_PORT = 3939;
private Integer port;
private String proxyAddress;
public static String registrationType;
public static String registrationAddress;
@ -47,6 +48,12 @@ public class NativeAgentManagementWebBootstrap {
this.port = port;
}
@Option(longName = "proxy-address")
@Description("native agent proxy address")
public void setProxyAddress(String proxyAddress) {
this.proxyAddress = proxyAddress;
}
@Option(longName = "registration-type")
@Description("registration type")
public void setRegistrationType(String registrationType) {
@ -82,10 +89,12 @@ public class NativeAgentManagementWebBootstrap {
throw new RuntimeException("Failed to verify the bootstrap parameters. " +
"Please read the documentation and check the parameters you entered");
}
if (nativeAgentManagementWebBootstrap.getRegistrationType() == null && nativeAgentManagementWebBootstrap.getRegistrationAddress() == null) {
if (nativeAgentManagementWebBootstrap.getRegistrationType() == null
&& nativeAgentManagementWebBootstrap.getRegistrationAddress() == null
&& nativeAgentManagementWebBootstrap.getProxyAddress() != null) {
nativeAgentManagementWebBootstrap.setRegistrationType("native-agent-management");
nativeAgentManagementWebBootstrap.setRegistrationAddress("127.0.0,1:" + nativeAgentManagementWebBootstrap.getPortOrDefault());
NativeAgentManagementNativeAgentProxyDiscovery.nativeAgentProxyCheckScheduled();
NativeAgentManagementNativeAgentProxyDiscovery.proxyAddress = nativeAgentManagementWebBootstrap.getProxyAddress();
}
// Start the http server
logger.info("start the http server... httPort:{}", nativeAgentManagementWebBootstrap.getPortOrDefault());
@ -121,12 +130,13 @@ public class NativeAgentManagementWebBootstrap {
private static boolean checkBootstrapParams(NativeAgentManagementWebBootstrap managementBootstrap) {
String address = managementBootstrap.getRegistrationAddress();
String type = managementBootstrap.getRegistrationType();
String proxyAddress = managementBootstrap.getProxyAddress();
// single
if (address == null && type == null) {
if (address == null && type == null && proxyAddress != null) {
return true;
}
// cluster
if (address != null && type != null) {
if (address != null && type != null && proxyAddress == null) {
return true;
}
return false;
@ -148,6 +158,10 @@ public class NativeAgentManagementWebBootstrap {
return registrationAddress;
}
public String getProxyAddress() {
return proxyAddress;
}
public Integer getPort() {
return port;
}

@ -34,27 +34,10 @@ public class HttpNativeAgentProxyHandler {
return responseFindAvailableProxyAddress(ctx, request);
}
if ("register".equals(operation)) {
String addressInfo = (String) bodyMap.get("nativeAgentProxyAddress");
String expirationTimeStr = (String) bodyMap.get("expirationTime");
return doRegisterNativeAgentProxy(request, addressInfo, expirationTimeStr);
}
return null;
}
private FullHttpResponse doRegisterNativeAgentProxy(FullHttpRequest request, String addressInfo, String expirationTimeStr) {
LocalDateTime expirationTime = LocalDateTime.parse(expirationTimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
NativeAgentManagementNativeAgentProxyDiscovery.storageNativeAgent(addressInfo, expirationTime);
DefaultFullHttpResponse response = new DefaultFullHttpResponse(
request.getProtocolVersion(),
HttpResponseStatus.OK,
Unpooled.copiedBuffer("success", StandardCharsets.UTF_8)
);
fillCorsHead(response);
return response;
}
public FullHttpResponse responseFindAvailableProxyAddress(ChannelHandlerContext ctx, FullHttpRequest request) {
String availableProxyAddress = findAvailableProxyAddress();

@ -1,16 +1,6 @@
package com.alibaba.arthas.nat.agent.proxy.registry.impl;
import com.alibaba.arthas.nat.agent.common.utils.OkHttpUtil;
import com.alibaba.arthas.nat.agent.proxy.registry.NativeAgentProxyRegistry;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* @description: NativeAgentManagementNativeAgentProxyRegistry(...)
@ -19,41 +9,9 @@ import java.util.concurrent.TimeUnit;
*/
public class NativeAgentManagementNativeAgentProxyRegistry implements NativeAgentProxyRegistry {
private static final Logger logger = LoggerFactory.getLogger(NativeAgentManagementNativeAgentProxyRegistry.class);
private final int INITIAL_DELAY_SECONDS = 5;
private final int PERIOD_SECONDS = 5;
private final int TIME_OUT_SECONDS = 15;
@Override
public void register(String address, String k, String v) {
registerProxy(address, k, v);
logger.info("register to native agent management success, native agent proxy address:{}", k);
sendHeadBeat(address, k, v);
}
private void sendHeadBeat(String address, String k, String v) {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
registerProxy(address, k, v);
};
scheduler.scheduleAtFixedRate(task, INITIAL_DELAY_SECONDS, PERIOD_SECONDS, TimeUnit.SECONDS);
}
private void registerProxy (String address, String k, String v) {
try {
String url = "http://" + address + "/api/native-agent-proxy";
LocalDateTime expirationTime = LocalDateTime.now().plusSeconds(TIME_OUT_SECONDS);
String jsonBody = "{" +
"\"operation\": \"register\"," +
"\"nativeAgentProxyAddress\":\""+ k +"\", " +
"\"expirationTime\": \"" + expirationTime+ "\"}";
Response response = OkHttpUtil.postAndResponse(url, jsonBody);
if (response.code() != 200) {
throw new RuntimeException("Register failed! response code: " + response.code());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// do nothing
}
}

@ -32,7 +32,7 @@ import java.util.Arrays;
*/
@Name("arthas-native-agent-proxy")
@Summary("Bootstrap Arthas Native Agent Proxy")
@Description("EXAMPLES:\n" + "java -jar native-agent-proxy.jar --ip 127.0.0.1 --management-address 127.0.0.1:3939\n"
@Description("EXAMPLES:\n" + "java -jar native-agent-proxy.jar\n"
+ "java -jar native-agent-proxy.jar --ip 151.159.27.114 --management-registration-type etcd --management-registration-address 161.169.97.114:2379 --agent-registration-type etcd --agent-registration-address 161.169.97.114:2379\n"
+ "java -jar native-agent-proxy.jar --ip 151.159.27.114 --port 2233 --management-registration-type etcd --management-registration-address 161.169.97.114:2379 --agent-registration-type etcd --agent-registration-address 161.169.97.114:2379\n"
+ "https://arthas.aliyun.com/doc\n")
@ -48,7 +48,7 @@ public class NativeAgentProxyBootstrap {
public static String agentRegistrationType;
public static String managementRegistrationAddress;
public static String agentRegistrationAddress;
private static String managementAddress;
@Option(longName = "port")
@Description("native agent proxy http/ws port, default 2233")
@ -56,7 +56,7 @@ public class NativeAgentProxyBootstrap {
this.port = port;
}
@Option(longName = "ip", required = true)
@Option(longName = "ip")
@Description("ip")
public void setIp(String ip) {
this.ip = ip;
@ -86,12 +86,6 @@ public class NativeAgentProxyBootstrap {
this.agentRegistrationAddress = agentRegistrationAddress;
}
@Option(longName = "management-address")
@Description("native agent management address")
public void setManagementAddress(String managementAddress) {
this.managementAddress = managementAddress;
}
public static void main(String[] args) {
// Print welcome message
WelcomeUtil.printProxyWelcomeMsg();
@ -117,11 +111,13 @@ public class NativeAgentProxyBootstrap {
throw new RuntimeException("Failed to verify the registration parameters. " +
"Please read the documentation and check the parameters you entered");
}
if (nativeAgentProxyBootstrap.getManagementAddress() != null) {
if (managementRegistrationType == null
&& managementRegistrationAddress == null
&& agentRegistrationType == null
&& agentRegistrationAddress ==null) {
nativeAgentProxyBootstrap.setAgentRegistrationType("native-agent-proxy");
nativeAgentProxyBootstrap.setManagementRegistrationType("native-agent-management");
nativeAgentProxyBootstrap.setAgentRegistrationAddress("127.0.0.1:" + nativeAgentProxyBootstrap.getPortOrDefault());
nativeAgentProxyBootstrap.setManagementRegistrationAddress(nativeAgentProxyBootstrap.getManagementAddress());
NativeAgentProxyNativeAgentDiscovery.nativeAgentCheckScheduled();
}
try {
@ -173,20 +169,24 @@ public class NativeAgentProxyBootstrap {
}
private static boolean checkRegisterParams(NativeAgentProxyBootstrap nativeAgentProxyBootstrap) {
String ip = nativeAgentProxyBootstrap.getIp();
String managementRegistrationType = nativeAgentProxyBootstrap.getManagementRegistrationType();
String managementRegistrationAddress = nativeAgentProxyBootstrap.getManagementRegistrationAddress();
String agentRegistrationType = nativeAgentProxyBootstrap.getAgentRegistrationType();
String agentRegistrationAddress = nativeAgentProxyBootstrap.getAgentRegistrationAddress();
String managementAddress = nativeAgentProxyBootstrap.getManagementAddress();
if (managementAddress != null
&& (managementRegistrationType == null && managementRegistrationAddress == null
&& agentRegistrationType == null && agentRegistrationAddress ==null)) {
// single
if (managementRegistrationType == null
&& managementRegistrationAddress == null
&& agentRegistrationType == null
&& agentRegistrationAddress ==null) {
return true;
}
if (managementAddress == null
&& (managementRegistrationType != null && managementRegistrationAddress != null
&& agentRegistrationType != null && agentRegistrationAddress != null)) {
// cluster
if (ip != null
&& managementRegistrationType != null
&& managementRegistrationAddress != null
&& agentRegistrationType != null
&& agentRegistrationAddress != null) {
return true;
}
@ -217,9 +217,6 @@ public class NativeAgentProxyBootstrap {
return agentRegistrationAddress;
}
public String getManagementAddress() {
return managementAddress;
}
public String getIp() {
return ip;

Loading…
Cancel
Save