o2wwx 5 лет назад
Родитель
Сommit
e70fc99b0d

+ 2 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Collect.java

@@ -10,6 +10,7 @@ import java.util.Objects;
 
 import com.x.base.core.project.connection.ActionResponse;
 import com.x.base.core.project.http.ActionResult;
+import com.x.base.core.project.tools.BaseTools;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.BooleanUtils;
@@ -245,7 +246,7 @@ public class Collect extends ConfigObject {
 	public void save() throws Exception {
 		File file = new File(Config.base(), Config.PATH_CONFIG_COLLECT);
 		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
-		this.executeSyncFile(Config.PATH_CONFIG_COLLECT);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_COLLECT);
 	}
 
 	public void setSecret(String secret) {

+ 0 - 107
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/ConfigObject.java

@@ -1,114 +1,7 @@
 package com.x.base.core.project.config;
 
 import com.x.base.core.project.gson.GsonPropertyObject;
-import com.x.base.core.project.gson.XGsonBuilder;
-import com.x.base.core.project.logger.Logger;
-import com.x.base.core.project.logger.LoggerFactory;
-import com.x.base.core.project.tools.Crypto;
-
-import java.io.*;
-import java.net.Inet4Address;
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.net.Socket;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
 
 public class ConfigObject extends GsonPropertyObject {
-    private static Logger logger = LoggerFactory.getLogger(ConfigObject.class);
-
-    public boolean executeSyncFile(String syncFilePath) throws Exception {
-
-        boolean Syncflag = false;
-        String localip = getIpAddress();
-        Nodes nodes = Config.nodes();
-        //同步config文件
-        for (String node : nodes.keySet()) {
-            //其他服务器
-            if (!node.equalsIgnoreCase(localip)) {
-                if (nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()) {
-                     Syncflag = this.executeSyncFile(syncFilePath, node, nodes.get(node).nodeAgentPort());
-                }
-            }
-        }
-        return  Syncflag;
-    }
-
-    private boolean executeSyncFile(String syncFilePath , String nodeName ,int nodePort){
-        boolean syncFileFlag = false;
-        File syncFile;
-        InputStream fileInputStream = null;
-
-        try (Socket socket = new Socket(nodeName, nodePort)) {
-
-            syncFile = new File(Config.base(), syncFilePath);
-            fileInputStream= new FileInputStream(syncFile);
-
-            socket.setKeepAlive(true);
-            socket.setSoTimeout(5000);
-            DataOutputStream dos = null;
-            DataInputStream dis  = null;
-            try {
-                dos = new DataOutputStream(socket.getOutputStream());
-                dis = new DataInputStream(socket.getInputStream());
-
-                Map<String, Object> commandObject = new HashMap<>();
-                commandObject.put("command", "syncFile:"+ syncFilePath);
-                commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
-                dos.writeUTF(XGsonBuilder.toJson(commandObject));
-                dos.flush();
-
-                dos.writeUTF(syncFilePath);
-                dos.flush();
-
-
-                logger.info("同步文件:"+syncFilePath+" starting...");
-                byte[] bytes = new byte[1024];
-                int length =0;
-                while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
-                    dos.write(bytes, 0, length);
-                    dos.flush();
-                }
-                logger.info("同步文件:" + syncFilePath +"end.");
-
-            }finally {
-                dos.close();
-                dis.close();
-                socket.close();
-                fileInputStream.close();
-            }
-
-            syncFileFlag = true;
-        } catch (Exception ex) {
-            logger.error(ex);
-            syncFileFlag = false;
-        }
-        return syncFileFlag;
-    }
-
-    public static String getIpAddress() {
-        try {
-            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
-            InetAddress ip = null;
-            while (allNetInterfaces.hasMoreElements()) {
-                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
-                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
-                    continue;
-                } else {
-                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
-                    while (addresses.hasMoreElements()) {
-                        ip = addresses.nextElement();
-                        if (ip != null && ip instanceof Inet4Address) {
-                            return ip.getHostAddress();
-                        }
-                    }
-                }
-            }
-        } catch (Exception e) {
-            System.err.println("IP地址获取失败" + e.toString());
-        }
-        return "";
-    }
 
 }

+ 2 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Dingding.java

@@ -4,6 +4,7 @@ import java.io.File;
 import java.util.Calendar;
 import java.util.Date;
 
+import com.x.base.core.project.tools.BaseTools;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -223,7 +224,7 @@ public class Dingding extends ConfigObject {
 	public void save() throws Exception {
 		File file = new File(Config.base(), Config.PATH_CONFIG_DINGDING);
 		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
-		this.executeSyncFile(Config.PATH_CONFIG_DINGDING);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_DINGDING);
 	}
 
 	public void setEnable(Boolean enable) {

+ 2 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Person.java

@@ -4,6 +4,7 @@ import java.io.File;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import com.x.base.core.project.tools.BaseTools;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -247,7 +248,7 @@ public class Person extends ConfigObject {
 	public void save() throws Exception {
 		File file = new File(Config.base(), Config.PATH_CONFIG_PERSON);
 		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
-		this.executeSyncFile(Config.PATH_CONFIG_PERSON);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_PERSON);
 	}
 
 	public void setCodeLogin(Boolean codeLogin) {

+ 2 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Portal.java

@@ -5,6 +5,7 @@ import java.util.LinkedHashMap;
 
 import com.x.base.core.project.annotation.FieldDescribe;
 import com.x.base.core.project.gson.XGsonBuilder;
+import com.x.base.core.project.tools.BaseTools;
 import com.x.base.core.project.tools.DefaultCharset;
 
 import org.apache.commons.io.FileUtils;
@@ -141,7 +142,7 @@ public class Portal extends ConfigObject {
 	public void save() throws Exception {
 		File file = new File(Config.base(), Config.PATH_CONFIG_PORTAL);
 		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
-		this.executeSyncFile(Config.PATH_CONFIG_PORTAL);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_PORTAL);
 	}
 
 	public LinkedHashMap<String, String> getUrlMapping() {

+ 2 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Qiyeweixin.java

@@ -4,6 +4,7 @@ import java.io.File;
 import java.util.Calendar;
 import java.util.Date;
 
+import com.x.base.core.project.tools.BaseTools;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -447,7 +448,7 @@ public class Qiyeweixin extends ConfigObject {
 	public void save() throws Exception {
 		File file = new File(Config.base(), Config.PATH_CONFIG_QIYEWEIXIN);
 		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
-		this.executeSyncFile(Config.PATH_CONFIG_QIYEWEIXIN);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_QIYEWEIXIN);
 	}
 
 

+ 2 - 1
o2server/x_base_core_project/src/main/java/com/x/base/core/project/config/Token.java

@@ -8,6 +8,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 
+import com.x.base.core.project.tools.BaseTools;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 
@@ -177,7 +178,7 @@ public class Token extends ConfigObject {
 	public void save() throws Exception {
 		File file = new File(Config.base(), Config.PATH_CONFIG_TOKEN);
 		FileUtils.write(file, XGsonBuilder.toJson(this), DefaultCharset.charset);
-        this.executeSyncFile(Config.PATH_CONFIG_TOKEN);
+        BaseTools.executeSyncFile(Config.PATH_CONFIG_TOKEN);
 	}
 
 	public boolean isInitialManager(String name) {

+ 106 - 7
o2server/x_base_core_project/src/main/java/com/x/base/core/project/tools/BaseTools.java

@@ -1,13 +1,19 @@
 package com.x.base.core.project.tools;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.io.*;
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.Socket;
+import java.util.*;
 import java.util.Map.Entry;
-import java.util.Objects;
 
+import com.x.base.core.project.config.Config;
+import com.x.base.core.project.config.ConfigObject;
+import com.x.base.core.project.config.Nodes;
+import com.x.base.core.project.gson.XGsonBuilder;
+import com.x.base.core.project.logger.Logger;
+import com.x.base.core.project.logger.LoggerFactory;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang3.StringUtils;
 
@@ -15,7 +21,7 @@ import com.google.gson.Gson;
 import com.google.gson.JsonElement;
 
 public class BaseTools {
-
+	private static Logger logger = LoggerFactory.getLogger(BaseTools.class);
 	public static String getBasePath() throws Exception {
 		return getBaseDirectory().getAbsolutePath();
 	}
@@ -135,4 +141,97 @@ public class BaseTools {
 		}
 		return FileUtils.readFileToString(file, DefaultCharset.charset);
 	}
+
+	public  static boolean executeSyncFile(String syncFilePath) throws Exception {
+
+		boolean Syncflag = false;
+		String localip = getIpAddress();
+		Nodes nodes = Config.nodes();
+		//同步config文件
+		for (String node : nodes.keySet()) {
+			//其他服务器
+			if (!node.equalsIgnoreCase(localip)) {
+				if (nodes.get(node).getApplication().getEnable() || nodes.get(node).getCenter().getEnable()) {
+					Syncflag = executeSyncFile(syncFilePath, node, nodes.get(node).nodeAgentPort());
+				}
+			}
+		}
+		return  Syncflag;
+	}
+
+	private  static boolean executeSyncFile(String syncFilePath , String nodeName ,int nodePort){
+		boolean syncFileFlag = false;
+		File syncFile;
+		InputStream fileInputStream = null;
+
+		try (Socket socket = new Socket(nodeName, nodePort)) {
+
+			syncFile = new File(Config.base(), syncFilePath);
+			fileInputStream= new FileInputStream(syncFile);
+
+			socket.setKeepAlive(true);
+			socket.setSoTimeout(5000);
+			DataOutputStream dos = null;
+			DataInputStream dis  = null;
+			try {
+				dos = new DataOutputStream(socket.getOutputStream());
+				dis = new DataInputStream(socket.getInputStream());
+
+				Map<String, Object> commandObject = new HashMap<>();
+				commandObject.put("command", "syncFile:"+ syncFilePath);
+				commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
+				dos.writeUTF(XGsonBuilder.toJson(commandObject));
+				dos.flush();
+
+				dos.writeUTF(syncFilePath);
+				dos.flush();
+
+
+				logger.info("同步文件:"+syncFilePath+" starting...");
+				byte[] bytes = new byte[1024];
+				int length =0;
+				while((length = fileInputStream.read(bytes, 0, bytes.length)) != -1) {
+					dos.write(bytes, 0, length);
+					dos.flush();
+				}
+				logger.info("同步文件:" + syncFilePath +"end.");
+
+			}finally {
+				dos.close();
+				dis.close();
+				socket.close();
+				fileInputStream.close();
+			}
+
+			syncFileFlag = true;
+		} catch (Exception ex) {
+			logger.error(ex);
+			syncFileFlag = false;
+		}
+		return syncFileFlag;
+	}
+
+	public static String getIpAddress() {
+		try {
+			Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
+			InetAddress ip = null;
+			while (allNetInterfaces.hasMoreElements()) {
+				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
+				if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
+					continue;
+				} else {
+					Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
+					while (addresses.hasMoreElements()) {
+						ip = addresses.nextElement();
+						if (ip != null && ip instanceof Inet4Address) {
+							return ip.getHostAddress();
+						}
+					}
+				}
+			}
+		} catch (Exception e) {
+			System.err.println("IP地址获取失败" + e.toString());
+		}
+		return "";
+	}
 }