From c23e06acbc6aadbabd88f5dcd9b708feea3e4a0b Mon Sep 17 00:00:00 2001 From: nagocoler Date: Thu, 30 Nov 2023 17:36:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AE=A2=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + itbaima-monitor-client/pom.xml | 6 +- .../example/config/ServerConfiguration.java | 73 +++++++++++++++++++ .../com/example/entity/ConnectionConfig.java | 11 +++ .../java/com/example/entity/Response.java | 31 ++++++++ .../main/java/com/example/utils/NetUtils.java | 54 ++++++++++++++ .../service/impl/ClientServiceImpl.java | 1 + 7 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 itbaima-monitor-client/src/main/java/com/example/config/ServerConfiguration.java create mode 100644 itbaima-monitor-client/src/main/java/com/example/entity/ConnectionConfig.java create mode 100644 itbaima-monitor-client/src/main/java/com/example/entity/Response.java create mode 100644 itbaima-monitor-client/src/main/java/com/example/utils/NetUtils.java diff --git a/.gitignore b/.gitignore index a87d56e..c16b7b2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ out/ .idea/ log/ +config/ !**/src/main/**/out/ !**/src/test/**/out/ diff --git a/itbaima-monitor-client/pom.xml b/itbaima-monitor-client/pom.xml index 23639d1..ba3c1ac 100644 --- a/itbaima-monitor-client/pom.xml +++ b/itbaima-monitor-client/pom.xml @@ -21,7 +21,11 @@ org.springframework.boot spring-boot-starter - + + com.alibaba.fastjson2 + fastjson2 + 2.0.34 + org.projectlombok lombok diff --git a/itbaima-monitor-client/src/main/java/com/example/config/ServerConfiguration.java b/itbaima-monitor-client/src/main/java/com/example/config/ServerConfiguration.java new file mode 100644 index 0000000..5276493 --- /dev/null +++ b/itbaima-monitor-client/src/main/java/com/example/config/ServerConfiguration.java @@ -0,0 +1,73 @@ +package com.example.config; + +import com.alibaba.fastjson2.JSONObject; +import com.example.entity.ConnectionConfig; +import com.example.utils.NetUtils; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +@Slf4j +@Configuration +public class ServerConfiguration { + + @Resource + NetUtils net; + + @Bean + ConnectionConfig connectionConfig(){ + log.info("正在加载服务端连接配置..."); + ConnectionConfig config = this.readConfigurationFromFile(); + if(config == null) + config = this.registerToServer(); + return config; + } + + private ConnectionConfig registerToServer() { + Scanner scanner = new Scanner(System.in); + String token, address; + do { + log.info("请输入需要注册的服务端访问地址,地址类似于'http://localhost:8080'这种写法:"); + address = scanner.nextLine(); + log.info("请输入由服务端生成,用于访问服务端的Token秘钥:"); + token = scanner.nextLine(); + } while (!net.registerToServer(address, token)); + ConnectionConfig connectionConfig = new ConnectionConfig(address, token); + this.saveConfigurationToFile(connectionConfig); + return connectionConfig; + } + + private void saveConfigurationToFile(ConnectionConfig config){ + File dir = new File("config"); + if(!dir.exists() && dir.mkdir()) + log.info("正在创建配置目录用于保存服务端连接信息..."); + File file = new File("config/server.json"); + try(FileWriter writer = new FileWriter(file)) { + writer.write(JSONObject.from(config).toJSONString()); + } catch (IOException e) { + log.error("保存配置文件时出现问题", e); + } + log.info("服务端连接信息已保存成功!"); + } + + private ConnectionConfig readConfigurationFromFile(){ + File configFile = new File("config/server.json"); + if(configFile.exists()) { + try (FileInputStream stream = new FileInputStream(configFile)){ + String raw = new String(stream.readAllBytes(), StandardCharsets.UTF_8); + return JSONObject.parseObject(raw).to(ConnectionConfig.class); + } catch (IOException e) { + log.error("读取配置文件时出现问题", e); + } + } + return null; + } +} diff --git a/itbaima-monitor-client/src/main/java/com/example/entity/ConnectionConfig.java b/itbaima-monitor-client/src/main/java/com/example/entity/ConnectionConfig.java new file mode 100644 index 0000000..76eda46 --- /dev/null +++ b/itbaima-monitor-client/src/main/java/com/example/entity/ConnectionConfig.java @@ -0,0 +1,11 @@ +package com.example.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ConnectionConfig { + String address; + String token; +} diff --git a/itbaima-monitor-client/src/main/java/com/example/entity/Response.java b/itbaima-monitor-client/src/main/java/com/example/entity/Response.java new file mode 100644 index 0000000..d4e656f --- /dev/null +++ b/itbaima-monitor-client/src/main/java/com/example/entity/Response.java @@ -0,0 +1,31 @@ +package com.example.entity; + +import com.alibaba.fastjson2.JSONObject; +import lombok.Data; + +@Data +public class Response { + int id; + int code; + Object data; + String message; + + public boolean success(){ + return code == 200; + } + + public JSONObject ofJson() { + return JSONObject.from(data); + } + + public String ofString() { + return data.toString(); + } + + public static Response errorResponse(Exception e) { + Response response = new Response(); + response.setCode(500); + response.setMessage(e.getMessage()); + return response; + } +} diff --git a/itbaima-monitor-client/src/main/java/com/example/utils/NetUtils.java b/itbaima-monitor-client/src/main/java/com/example/utils/NetUtils.java new file mode 100644 index 0000000..96fc28d --- /dev/null +++ b/itbaima-monitor-client/src/main/java/com/example/utils/NetUtils.java @@ -0,0 +1,54 @@ +package com.example.utils; + +import com.alibaba.fastjson2.JSONObject; +import com.example.entity.ConnectionConfig; +import com.example.entity.Response; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +@Slf4j +@Component +public class NetUtils { + + private final HttpClient client = HttpClient.newHttpClient(); + + @Lazy + @Resource + private ConnectionConfig config; + + public boolean registerToServer(String address, String token) { + log.info("正在向服务端注册,请稍后..."); + Response response = this.doGet("/register", address, token); + if(!response.success()) { + log.error("注册客户端失败: {}", response.getMessage()); + } else { + log.info("客户端注册已完成!"); + } + return response.success(); + } + + private Response doGet(String url) { + return this.doGet(url, config.getAddress(), config.getToken()); + } + + private Response doGet(String url, String address, String token) { + try { + HttpRequest request = HttpRequest.newBuilder().GET() + .uri(new URI(address + "/monitor" + url)) + .header("Authorization", token) + .build(); + HttpResponse 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); + } + } +} diff --git a/itbaima-monitor-server/src/main/java/com/example/service/impl/ClientServiceImpl.java b/itbaima-monitor-server/src/main/java/com/example/service/impl/ClientServiceImpl.java index 8eff490..dd69c94 100644 --- a/itbaima-monitor-server/src/main/java/com/example/service/impl/ClientServiceImpl.java +++ b/itbaima-monitor-server/src/main/java/com/example/service/impl/ClientServiceImpl.java @@ -72,6 +72,7 @@ public class ClientServiceImpl extends ServiceImpl impleme StringBuilder sb = new StringBuilder(24); for (int i = 0; i < 24; i++) sb.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length()))); + System.out.println(sb); return sb.toString(); } }