完成启动时基础数据上报
This commit is contained in:
parent
22522a730b
commit
55f48b2156
@ -6,6 +6,8 @@ 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;
|
||||
|
||||
@ -18,7 +20,7 @@ import java.util.Scanner;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ServerConfiguration {
|
||||
public class ServerConfiguration implements ApplicationRunner {
|
||||
|
||||
@Resource
|
||||
NetUtils net;
|
||||
@ -32,10 +34,15 @@ public class ServerConfiguration {
|
||||
ConnectionConfig config = this.readConfigurationFromFile();
|
||||
if(config == null)
|
||||
config = this.registerToServer();
|
||||
System.out.println(monitor.monitorBaseDetail());
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
log.info("正在向服务端更新基本系统信息...");
|
||||
net.updateBaseDetails(monitor.monitorBaseDetail());
|
||||
}
|
||||
|
||||
private ConnectionConfig registerToServer() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String token, address;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.example.utils;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.example.entity.BaseDetail;
|
||||
import com.example.entity.ConnectionConfig;
|
||||
import com.example.entity.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
@ -8,6 +9,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
@ -50,4 +52,29 @@ public class NetUtils {
|
||||
return Response.errorResponse(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateBaseDetails(BaseDetail detail) {
|
||||
Response response = this.doPost("/detail", detail);
|
||||
if(response.success()) {
|
||||
log.info("系统基本信息已更新完成");
|
||||
} else {
|
||||
log.error("系统基本信息更新失败: {}", response.message());
|
||||
}
|
||||
}
|
||||
|
||||
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("Authorization", config.getToken())
|
||||
.header("Content-Type", "application/json")
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
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;
|
||||
String cpuName;
|
||||
int cpuCore;
|
||||
double memory;
|
||||
double disk;
|
||||
String ip;
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
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
|
||||
String cpuName;
|
||||
@NotNull
|
||||
int cpuCore;
|
||||
@NotNull
|
||||
double memory;
|
||||
@NotNull
|
||||
double disk;
|
||||
@NotNull
|
||||
String ip;
|
||||
}
|
@ -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 findClientById(int id);
|
||||
Client findClientByToken(String token);
|
||||
boolean verifyAndRegister(String token);
|
||||
void updateClientDetail(ClientDetailVO vo, Client client);
|
||||
}
|
||||
|
@ -2,14 +2,20 @@ 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 org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -21,6 +27,9 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> impleme
|
||||
private final Map<Integer, Client> clientIdCache = new ConcurrentHashMap<>();
|
||||
private final Map<String, Client> clientTokenCache = new ConcurrentHashMap<>();
|
||||
|
||||
@Resource
|
||||
ClientDetailMapper detailMapper;
|
||||
|
||||
@PostConstruct
|
||||
public void initClientCache() {
|
||||
this.list().forEach(this::addClientCache);
|
||||
@ -55,6 +64,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