Просмотр исходного кода

Merge branch 'fix/synConfig0901' into 'develop'

config文件集群同步

See merge request o2oa/o2oa!1499
o2null 5 лет назад
Родитель
Сommit
cc605c3da2

+ 2 - 0
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,6 +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);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_COLLECT);
 	}
 
 	public void setSecret(String secret) {

+ 2 - 0
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,6 +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);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_DINGDING);
 	}
 
 	public void setEnable(Boolean enable) {

+ 2 - 0
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,6 +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);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_PERSON);
 	}
 
 	public void setCodeLogin(Boolean codeLogin) {

+ 2 - 0
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,6 +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);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_PORTAL);
 	}
 
 	public LinkedHashMap<String, String> getUrlMapping() {

+ 2 - 0
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,6 +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);
+		BaseTools.executeSyncFile(Config.PATH_CONFIG_QIYEWEIXIN);
 	}
 
 

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

@@ -1,12 +1,14 @@
 package com.x.base.core.project.config;
 
 import java.io.File;
+import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.LinkedHashMap;
 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;
 
@@ -176,6 +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);
+        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 "";
+	}
 }