完成客户端在启动时进行一次主机基本信息上报
This commit is contained in:
parent
c23e06acbc
commit
c00d2ecee6
@ -36,6 +36,11 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.oshi</groupId>
|
||||
<artifactId>oshi-core</artifactId>
|
||||
<version>6.4.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -2,9 +2,13 @@ package com.example.config;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.example.entity.ConnectionConfig;
|
||||
import com.example.entity.data.BaseDetail;
|
||||
import com.example.utils.MonitorUtils;
|
||||
import com.example.utils.NetUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ -17,13 +21,16 @@ import java.util.Scanner;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ServerConfiguration {
|
||||
public class ServerConfiguration implements ApplicationRunner {
|
||||
|
||||
@Resource
|
||||
NetUtils net;
|
||||
|
||||
@Resource
|
||||
MonitorUtils monitor;
|
||||
|
||||
@Bean
|
||||
ConnectionConfig connectionConfig(){
|
||||
ConnectionConfig connectionConfig() {
|
||||
log.info("正在加载服务端连接配置...");
|
||||
ConnectionConfig config = this.readConfigurationFromFile();
|
||||
if(config == null)
|
||||
@ -31,6 +38,13 @@ public class ServerConfiguration {
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
log.info("正在向服务端更新基本系统信息...");
|
||||
BaseDetail detail = monitor.monitorBaseData();
|
||||
net.updateBaseDetails(detail);
|
||||
}
|
||||
|
||||
private ConnectionConfig registerToServer() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String token, address;
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.example.entity.data;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class BaseDetail {
|
||||
String osArch;
|
||||
String osName;
|
||||
String osVersion;
|
||||
int osBit;
|
||||
int cpuCore;
|
||||
double memory;
|
||||
double disk;
|
||||
String ip;
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.example.utils;
|
||||
|
||||
import com.example.entity.data.BaseDetail;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import oshi.SystemInfo;
|
||||
import oshi.hardware.HardwareAbstractionLayer;
|
||||
import oshi.hardware.NetworkIF;
|
||||
import oshi.hardware.HWDiskStore;
|
||||
import oshi.software.os.OperatingSystem;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.Properties;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class MonitorUtils {
|
||||
|
||||
private final SystemInfo info = new SystemInfo();
|
||||
private final Properties properties = System.getProperties();
|
||||
|
||||
public BaseDetail monitorBaseData() {
|
||||
OperatingSystem os = info.getOperatingSystem();
|
||||
HardwareAbstractionLayer hardware = info.getHardware();
|
||||
double memory = hardware.getMemory().getTotal() / 1024.0 / 1024 / 1024;
|
||||
double diskSize = hardware.getDiskStores().stream().mapToLong(HWDiskStore::getSize).sum() / 1024.0 / 1024 / 1024;
|
||||
try {
|
||||
String ip = null;
|
||||
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) {
|
||||
ip = network.getIPv4addr()[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new BaseDetail()
|
||||
.setOsBit(os.getBitness())
|
||||
.setOsArch(properties.getProperty("os.arch"))
|
||||
.setOsVersion(os.getVersionInfo().getVersion())
|
||||
.setOsName(os.getFamily())
|
||||
.setCpuCore(hardware.getProcessor().getLogicalProcessorCount())
|
||||
.setMemory(memory)
|
||||
.setDisk(diskSize)
|
||||
.setIp(ip);
|
||||
} catch (IOException e) {
|
||||
log.error("读取系统基本信息时出错", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package com.example.utils;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.example.entity.ConnectionConfig;
|
||||
import com.example.entity.Response;
|
||||
import com.example.entity.data.BaseDetail;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@ -34,6 +35,31 @@ public class NetUtils {
|
||||
return response.success();
|
||||
}
|
||||
|
||||
public void updateBaseDetails(BaseDetail detail){
|
||||
Response response = this.doPost("/detail", detail);
|
||||
if(response.success()) {
|
||||
log.info("系统基本信息更新已完成");
|
||||
} else {
|
||||
log.error("系统基本信息更新失败: {}", response.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private Response doPost(String url, Object data) {
|
||||
try {
|
||||
String rawData = JSONObject.from(data).toJSONString();
|
||||
HttpRequest request = HttpRequest.newBuilder().POST(HttpRequest.BodyPublishers.ofString(rawData))
|
||||
.uri(new URI(config.getAddress() + "/monitor" + url))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", config.getToken())
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return JSONObject.parseObject(response.body()).to(Response.class);
|
||||
} catch (Exception e) {
|
||||
log.error("在发起服务端请求时出现错误", e);
|
||||
return Response.errorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
private Response doGet(String url) {
|
||||
return this.doGet(url, config.getAddress(), config.getToken());
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
package com.example.controller;
|
||||
|
||||
import com.example.entity.RestBean;
|
||||
import com.example.entity.dto.Client;
|
||||
import com.example.entity.vo.request.ClientDetailVO;
|
||||
import com.example.service.ClientService;
|
||||
import com.example.utils.Const;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/monitor")
|
||||
@ -20,4 +21,11 @@ public class ClientController {
|
||||
return service.verifyAndRegister(token) ?
|
||||
RestBean.success() : RestBean.failure(401, "客户端注册失败,请检查Token是否正确");
|
||||
}
|
||||
|
||||
@PostMapping("/detail")
|
||||
public RestBean<Void> updateClientDetails(@RequestAttribute(Const.ATTR_CLIENT) Client client,
|
||||
@RequestBody @Valid ClientDetailVO vo) {
|
||||
service.updateClientDetail(vo, client);
|
||||
return RestBean.success();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.example.entity.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.example.entity.BaseData;
|
||||
import lombok.AllArgsConstructor;
|
||||
@ -11,6 +12,7 @@ import java.util.Date;
|
||||
@TableName("db_client")
|
||||
@AllArgsConstructor
|
||||
public class Client implements BaseData {
|
||||
@TableId
|
||||
Integer id;
|
||||
String name;
|
||||
String token;
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.example.entity.dto;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@TableName("db_client_detail")
|
||||
public class ClientDetail {
|
||||
@TableId
|
||||
Integer id;
|
||||
String osArch;
|
||||
String osName;
|
||||
String osVersion;
|
||||
int osBit;
|
||||
int cpuCore;
|
||||
double memory;
|
||||
double disk;
|
||||
String ip;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.example.entity.vo.request;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ClientDetailVO {
|
||||
@NotNull
|
||||
String osArch;
|
||||
@NotNull
|
||||
String osName;
|
||||
@NotNull
|
||||
String osVersion;
|
||||
@NotNull
|
||||
int osBit;
|
||||
@NotNull
|
||||
int cpuCore;
|
||||
@NotNull
|
||||
double memory;
|
||||
@NotNull
|
||||
double disk;
|
||||
@NotNull
|
||||
String ip;
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.example.filter;
|
||||
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import com.example.entity.RestBean;
|
||||
import com.example.entity.dto.Client;
|
||||
import com.example.service.ClientService;
|
||||
import com.example.utils.Const;
|
||||
@ -43,6 +44,8 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
Client client = service.findClient(authorization);
|
||||
if(client == null) {
|
||||
response.setStatus(401);
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.getWriter().write(RestBean.failure(401, "未授权").asJsonString());
|
||||
return;
|
||||
} else {
|
||||
request.setAttribute(Const.ATTR_CLIENT, client);
|
||||
|
@ -0,0 +1,9 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.entity.dto.ClientDetail;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface ClientDetailMapper extends BaseMapper<ClientDetail> {
|
||||
}
|
@ -2,10 +2,12 @@ package com.example.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.example.entity.dto.Client;
|
||||
import com.example.entity.vo.request.ClientDetailVO;
|
||||
|
||||
public interface ClientService extends IService<Client> {
|
||||
String registerToken();
|
||||
Client findClient(String token);
|
||||
Client findClient(Integer id);
|
||||
boolean verifyAndRegister(String token);
|
||||
void updateClientDetail(ClientDetailVO vo, Client client);
|
||||
}
|
||||
|
@ -2,15 +2,21 @@ package com.example.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.example.entity.dto.Client;
|
||||
import com.example.entity.dto.ClientDetail;
|
||||
import com.example.entity.vo.request.ClientDetailVO;
|
||||
import com.example.mapper.ClientDetailMapper;
|
||||
import com.example.mapper.ClientMapper;
|
||||
import com.example.service.ClientService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
|
||||
@Slf4j
|
||||
@ -22,6 +28,9 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
private final HashMap<Integer, Client> clientIdCache = new HashMap<>();
|
||||
private final HashMap<String, Client> clientTokenCache = new HashMap<>();
|
||||
|
||||
@Resource
|
||||
private ClientDetailMapper detailMapper;
|
||||
|
||||
@PostConstruct
|
||||
public void initClientCache() {
|
||||
this.list().forEach(this::addClientCache);
|
||||
@ -57,6 +66,18 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateClientDetail(ClientDetailVO vo, Client client) {
|
||||
ClientDetail detail = new ClientDetail();
|
||||
BeanUtils.copyProperties(vo, detail);
|
||||
detail.setId(client.getId());
|
||||
if(Objects.nonNull(detailMapper.selectById(client.getId()))) {
|
||||
detailMapper.updateById(detail);
|
||||
} else {
|
||||
detailMapper.insert(detail);
|
||||
}
|
||||
}
|
||||
|
||||
private void addClientCache(Client client) {
|
||||
clientIdCache.put(client.getId(), client);
|
||||
clientTokenCache.put(client.getToken(), client);
|
||||
|
Loading…
x
Reference in New Issue
Block a user