完成客户端注册操作
This commit is contained in:
parent
4695864db8
commit
c23e06acbc
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,6 +2,7 @@
|
||||
out/
|
||||
.idea/
|
||||
log/
|
||||
config/
|
||||
!**/src/main/**/out/
|
||||
!**/src/test/**/out/
|
||||
|
||||
|
@ -21,7 +21,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.34</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.example.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ConnectionConfig {
|
||||
String address;
|
||||
String token;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
@ -72,6 +72,7 @@ public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> 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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user