完成信息读取操作
This commit is contained in:
parent
5a6db11a3a
commit
22522a730b
@ -37,6 +37,11 @@
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.37</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.oshi</groupId>
|
||||
<artifactId>oshi-core</artifactId>
|
||||
<version>6.4.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -2,6 +2,7 @@ package com.example.config;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.example.entity.ConnectionConfig;
|
||||
import com.example.utils.MonitorUtils;
|
||||
import com.example.utils.NetUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -22,12 +23,16 @@ public class ServerConfiguration {
|
||||
@Resource
|
||||
NetUtils net;
|
||||
|
||||
@Resource
|
||||
MonitorUtils monitor;
|
||||
|
||||
@Bean
|
||||
ConnectionConfig connectionConfig() {
|
||||
log.info("正在加载服务端连接配置...");
|
||||
ConnectionConfig config = this.readConfigurationFromFile();
|
||||
if(config == null)
|
||||
config = this.registerToServer();
|
||||
System.out.println(monitor.monitorBaseDetail());
|
||||
return config;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,18 @@
|
||||
package com.example.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class BaseDetail {
|
||||
String osArch;
|
||||
String osName;
|
||||
String osVersion;
|
||||
int osBit;
|
||||
String cpuName;
|
||||
int cpuCore;
|
||||
double memory;
|
||||
double disk;
|
||||
String ip;
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.example.utils;
|
||||
|
||||
import com.example.entity.BaseDetail;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import oshi.SystemInfo;
|
||||
import oshi.hardware.HardwareAbstractionLayer;
|
||||
import oshi.hardware.NetworkIF;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MonitorUtils {
|
||||
|
||||
private final SystemInfo info = new SystemInfo();
|
||||
private final Properties properties = System.getProperties();
|
||||
|
||||
public BaseDetail monitorBaseDetail() {
|
||||
OperatingSystem os = info.getOperatingSystem();
|
||||
HardwareAbstractionLayer hardware = info.getHardware();
|
||||
double memory = hardware.getMemory().getTotal() / 1024.0 / 1024 /1024;
|
||||
double diskSize = Arrays.stream(File.listRoots()).mapToLong(File::getTotalSpace).sum() / 1024.0 / 1024 / 1024;
|
||||
String ip = Objects.requireNonNull(this.findNetworkInterface(hardware)).getIPv4addr()[0];
|
||||
return new BaseDetail()
|
||||
.setOsArch(properties.getProperty("os.arch"))
|
||||
.setOsName(os.getFamily())
|
||||
.setOsVersion(os.getVersionInfo().getVersion())
|
||||
.setOsBit(os.getBitness())
|
||||
.setCpuName(hardware.getProcessor().getProcessorIdentifier().getName())
|
||||
.setCpuCore(hardware.getProcessor().getLogicalProcessorCount())
|
||||
.setMemory(memory)
|
||||
.setDisk(diskSize)
|
||||
.setIp(ip);
|
||||
}
|
||||
|
||||
private NetworkIF findNetworkInterface(HardwareAbstractionLayer hardware) {
|
||||
try {
|
||||
for (NetworkIF network : hardware.getNetworkIFs()) {
|
||||
String[] ipv4Addr = network.getIPv4addr();
|
||||
NetworkInterface ni = network.queryNetworkInterface();
|
||||
if(!ni.isLoopback() && !ni.isPointToPoint() && ni.isUp() && !ni.isVirtual()
|
||||
&& (ni.getName().startsWith("eth") || ni.getName().startsWith("en"))
|
||||
&& ipv4Addr.length > 0) {
|
||||
return network;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("读取网络接口信息时出错", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user