This commit is contained in:
liulu
2024-10-28 09:55:21 +08:00
parent 4c0524415c
commit e59811ced4
200 changed files with 18322 additions and 0 deletions
@@ -0,0 +1,167 @@
package com.sunyard.ssp.server;
import com.alibaba.fastjson.JSONObject;
import com.sunyard.ssp.proto.control.Control;
import lombok.Cleanup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.UUID;
/**
* 这个类暂时先这么记着
* [TODO] 读写分离
* [TODO] 对接配置中心,热重载。
*/
@Slf4j
public class Config {
public static Config global = new Config();
public System system = new System();
public static class System {
public String name;
public System() {
this.name = findName();
}
private boolean getBooleFromEnv(String name){
log.debug("尝试从环境变量载入 {}", name);
Map map = java.lang.System.getenv();
if ( map.containsKey(name) ){
String sn = (String) map.get(name);
if ( "true".equals( sn.trim().toLowerCase() ) ){
return true;
}
}
return false;
}
private String readNameFromFile(){
// 检查路径
File folder = new File("/data");
if ( !folder.exists() ){
folder.mkdirs();
}
File snFile = new File(folder, "security.name");
if ( snFile.exists() ){
try(FileReader fr = new FileReader(snFile) ; BufferedReader bfr = new BufferedReader(fr) ) {
String sn = bfr.readLine();
sn = sn.trim().toLowerCase();
if ( sn.length() >= 6 ){
saveNameToFile(sn);
return sn;
} else {
log.debug("security.name 的值不合规");
return null;
}
} catch (IOException e) {
e.printStackTrace();
log.error("写入 security.name 配置文件的时候失败了,{}", snFile.getAbsolutePath() );
return null;
}
} else {
log.error("security.name 配置文件不存在,{}", snFile.getAbsolutePath() );
return null;
}
}
private String genSn(){
return UUID.randomUUID().toString().replaceAll("-","");
}
private void saveNameToFile( String sn ){
// 检查路径
File folder = new File("/data");
if ( !folder.exists() ){
folder.mkdirs();
}
File snFile = new File(folder, "security.name");
if ( snFile.exists() ){
snFile.delete();
}
try( FileOutputStream fio = new FileOutputStream(snFile); ) {
snFile.createNewFile();
fio.write( sn.getBytes(Charset.forName("UTF-8")) );
fio.flush();
} catch (IOException e) {
e.printStackTrace();
log.error("写入 security.name 配置文件的时候失败了,{}", snFile.getAbsolutePath() );
}
log.debug("写入 security.name 配置文件完成,SERVER_NAME = {},{}", sn, snFile.getAbsolutePath());
}
private String findName(){
log.debug("尝试从环境变量载入 SERVER_NAME");
Map map = java.lang.System.getenv();
if ( map.containsKey("SERVER_NAME") ){
String sn = (String) map.get("SERVER_NAME");
sn = sn.trim().toUpperCase();
if ( sn.length() >= 6 ){
//saveNameToFile(sn);
return sn;
} else {
log.debug("环境变量 SERVER_NAME 的值不合规");
}
}
log.debug("尝试从 security.name 配置载入 SERVER_NAME");
String sn = readNameFromFile();
if ( null != sn){
return sn;
}
sn = genSn();
// saveNameToFile( sn );
return sn;
}
}
public Messenger messenger = new Messenger();
public static class Messenger {
//cluster solo
public JSONObject redisCluster = (JSONObject) loadFileConfig("redis.config.json");
public int maxWaitTime = 10; // 秒
public String privateCliChannel(){
return Control.Req.Origin.CLI.name() + "_" + Config.global.system.name;
}
public String privateServerChannel(){
return Control.Req.Origin.SERVER.name() + "_" + Config.global.system.name;
}
}
@SneakyThrows
public static JSONObject loadFileConfig(String name ){
File folder = new File("./config");
File configFile = new File(folder, name);
if ( ! configFile.exists() ){
log.error("没有找到配置文件 {}", name);
}
@Cleanup FileInputStream fio = new FileInputStream(configFile);
byte[] buff = new byte[ fio.available() ];
fio.read( buff );
return (JSONObject) JSONObject.parse( new String(buff) );
}
}