[agent] [init] - 优化启动信息处理
This commit is contained in:
@@ -8,14 +8,13 @@ import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@@ -23,26 +22,58 @@ import java.util.Properties;
|
||||
@Slf4j
|
||||
public class CollectSystemInfo implements ApplicationContextAware {
|
||||
|
||||
public AgentServerInfo agentServerInfo;
|
||||
@Resource
|
||||
Environment environment;
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@Resource
|
||||
OctopusAgentInitService octopusAgentInitService;
|
||||
|
||||
public AgentServerInfo agentServerInfo;
|
||||
|
||||
|
||||
|
||||
private ApplicationContext context;
|
||||
|
||||
@PostConstruct
|
||||
private void getInjectServerInfo(){
|
||||
private void getInjectServerInfo() {
|
||||
|
||||
log.info("[ Octopus Agent ]-- Starting getInjectServerInfo");
|
||||
|
||||
agentServerInfo = (AgentServerInfo) context.getBean("agentServerInfo");
|
||||
|
||||
// 进行归一化处理,去除掉属性中所有多余的 "
|
||||
log.debug("开始归一化处理 AgentInfoVO");
|
||||
Class<AgentServerInfo> infoClass = AgentServerInfo.class;
|
||||
Arrays
|
||||
.stream(infoClass.getDeclaredFields())
|
||||
.forEach(
|
||||
field -> {
|
||||
try {
|
||||
log.debug(
|
||||
"开始处理初始化AgentInfoVO中的字段 => {}",
|
||||
field.getName()
|
||||
);
|
||||
if (!field
|
||||
.getType()
|
||||
.equals(String.class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// must do this
|
||||
field.setAccessible(true);
|
||||
Object o = field.get(agentServerInfo);
|
||||
if (null == o) {
|
||||
return;
|
||||
}
|
||||
|
||||
String value = (String) o;
|
||||
field.set(
|
||||
agentServerInfo,
|
||||
value.replace("\"",
|
||||
"")
|
||||
);
|
||||
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (ObjectUtils.isEmpty(agentServerInfo)) {
|
||||
throw new MyRuntimeException(" Collect server info error !");
|
||||
}
|
||||
@@ -62,16 +93,16 @@ public class CollectSystemInfo implements ApplicationContextAware {
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void initialReadingEnvironment(){
|
||||
public void initialReadingEnvironment() {
|
||||
|
||||
// https://zhuanlan.zhihu.com/p/449416472
|
||||
// https://cloud.tencent.com/developer/article/1919814
|
||||
// https://blog.51cto.com/binghe001/5244823
|
||||
|
||||
|
||||
try{
|
||||
try {
|
||||
|
||||
Properties props =System.getProperties();
|
||||
Properties props = System.getProperties();
|
||||
|
||||
System.out.println("props = " + props);
|
||||
System.out.println();
|
||||
@@ -99,8 +130,8 @@ public class CollectSystemInfo implements ApplicationContextAware {
|
||||
String osArch = System.getProperty("os.arch");
|
||||
|
||||
System.out.println("当前用户:" + userName);
|
||||
System.out.println("用户的主目录:"+props.getProperty("user.home"));
|
||||
System.out.println("用户的当前工作目录:"+props.getProperty("user.dir"));
|
||||
System.out.println("用户的主目录:" + props.getProperty("user.home"));
|
||||
System.out.println("用户的当前工作目录:" + props.getProperty("user.dir"));
|
||||
System.out.println("主机名称:" + localName);
|
||||
System.out.println("主机系统:" + osName);
|
||||
System.out.println("系统版本:" + osVersion);
|
||||
|
||||
Reference in New Issue
Block a user