Sfoglia il codice sorgente

处理日志相关

wukai 2 anni fa
parent
commit
c8fed786c2

+ 56 - 16
sync-common/src/main/java/com/jjt/common/utils/LinuxCommand.java

@@ -4,9 +4,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.*;
+import java.util.List;
 
 /**
  * Linux命令执行工具
+ *
  * @author wukai
  */
 public class LinuxCommand {
@@ -17,23 +19,61 @@ public class LinuxCommand {
      *
      * @param cmd
      */
-    public static void exec(String cmd) {
-        try {
-            Process process = Runtime.getRuntime().exec(cmd);
-            try (PrintWriter printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(process.getOutputStream())), true);
-                 BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
-
-                printWriter.println(cmd);
-                printWriter.println("exit");
-
-                String line;
-                while ((line = read.readLine()) != null) {
-                    log.info(line);
-                }
+    public static void exec(String cmd) throws Exception {
+        Process process = Runtime.getRuntime().exec(cmd);
+        print(process);
+        log.info("{}命令,执行状态{}", cmd, process);
+
+        if (process.waitFor() != 0) {
+            throw new Exception("执行命令出错啦!" + cmd);
+        }
+    }
+
+    private static void print(Process process) {
+        // 获取子进程的输入流
+        InputStream inputStream = process.getInputStream();
+        // 获取子进程的错误流
+        InputStream errorStream = process.getErrorStream();
+        // 获取子进程的输出流
+        OutputStream outputStream = process.getOutputStream();
+        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)), true);
+             BufferedReader readInput = new BufferedReader(new InputStreamReader(inputStream));
+             BufferedReader readError = new BufferedReader(new InputStreamReader(errorStream))) {
+            pw.println("exit");
+
+            String line;
+            log.info("子进程输入流:");
+            while ((line = readInput.readLine()) != null) {
+                log.info(line);
+            }
+            log.info("子进程错误流:");
+            while ((line = readError.readLine()) != null) {
+                log.info(line);
             }
-            log.info(cmd + " 执行状态:" + process.waitFor());
-        } catch (Exception e) {
-            log.error("执行命令出错!", e);
+        } catch (IOException e) {
+            log.error("出错啦:{}", e.getMessage());
         }
     }
+
+    /**
+     * 执行mysql导出命令
+     *
+     * @param commands 命令
+     * @param path     导出文件路径
+     * @throws Exception 异常说明
+     */
+    public static void mysqlExport(List<String> commands, String path) throws Exception {
+        ProcessBuilder processBuilder = new ProcessBuilder();
+        processBuilder.command(commands);
+        processBuilder.redirectOutput(new File(path));
+        Process process = processBuilder.start();
+
+        print(process);
+
+        log.info("mysql备份执行状态:{}", process.waitFor());
+        if (process.waitFor() != 0) {
+            throw new Exception("执行mysql备份命令出错啦!");
+        }
+
+    }
 }

+ 11 - 6
sync-in/src/main/java/com/jjt/in/service/impl/InEsServiceImpl.java

@@ -107,12 +107,17 @@ public class InEsServiceImpl extends InBaseService implements IInEsService {
 
                             String mappingName = "mapping_" + ido.getIndexName();
                             String dataName = "data_" + ido.getIndexName();
-                            //导入mapping命令
-                            String mappingCmd = "/usr/bin/elasticdump --input " + dir + mappingName + " --output " + uri + " --type=mapping";
-                            LinuxCommand.exec(mappingCmd);
-                            //导入data命令
-                            String dataCmd = "/usr/bin/elasticdump --input " + dir + dataName + " --output " + uri + " --type=data";
-                            LinuxCommand.exec(dataCmd);
+                            try {
+                                //导入mapping命令
+                                String mappingCmd = "/usr/bin/elasticdump --input " + dir + mappingName + " --output " + uri + " --type=mapping";
+                                LinuxCommand.exec(mappingCmd);
+                                //导入data命令
+                                String dataCmd = "/usr/bin/elasticdump --input " + dir + dataName + " --output " + uri + " --type=data";
+                                LinuxCommand.exec(dataCmd);
+                            } catch (Exception e) {
+                                log.error("执行命令出错:");
+                                throw new RuntimeException(e);
+                            }
                         }
                     }
                 }

+ 16 - 4
sync-in/src/main/java/com/jjt/in/service/impl/InProcessServiceImpl.java

@@ -145,7 +145,11 @@ public class InProcessServiceImpl extends InBaseService implements IInProcessSer
         Files.copy(zipFile.toPath(), Paths.get(shellDir + zipFile.getName()));
         // 给文件赋执行权限
         String dataCmd = "chmod +x " + shellDir + zipFile.getName();
-        LinuxCommand.exec(dataCmd);
+        try {
+            LinuxCommand.exec(dataCmd);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private void handleMysqlType(File zipFile) throws IOException {
@@ -160,8 +164,12 @@ public class InProcessServiceImpl extends InBaseService implements IInProcessSer
         String pass = jsonObject.getString("pass");
         String db = jsonObject.getString("db");
         //组装导入命令
-        String cmd = String.format("mysql -u%s -p%s %s < %s", user, pass, db, zipFile.getPath());
-        LinuxCommand.exec(cmd);
+        String cmd = String.format("/usr/bin/mysql -u%s -p%s %s < %s", user, pass, db, zipFile.getPath());
+        try {
+            LinuxCommand.exec(cmd);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
     private void handlePhpType(String phpDir, File zipFile) throws IOException {
@@ -194,6 +202,10 @@ public class InProcessServiceImpl extends InBaseService implements IInProcessSer
     private void handleCrontabType(File zipFile) {
         String cronCmd = "crontab " + zipFile.getPath();
         log.info("添加crontab", cronCmd);
-        LinuxCommand.exec(cronCmd);
+        try {
+            LinuxCommand.exec(cronCmd);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 }

+ 10 - 6
sync-out/src/main/java/com/jjt/out/service/impl/OutEsServiceImpl.java

@@ -136,12 +136,16 @@ public class OutEsServiceImpl extends OutBaseService implements IOutEsService {
             } catch (IOException ignored) {
             }
 
-            //导出mapping命令
-            String mappingCmd = "/usr/bin/elasticdump --input " + uri + " --output " + tmpDir + mappingName + " --type=mapping";
-            LinuxCommand.exec(mappingCmd);
-            //导出data命令
-            String dataCmd = "/usr/bin/elasticdump --input " + uri + " --output " + tmpDir + dataName + " --type=data";
-            LinuxCommand.exec(dataCmd);
+            try {
+                //导出mapping命令
+                String mappingCmd = "/usr/bin/elasticdump --input " + uri + " --output " + tmpDir + mappingName + " --type=mapping";
+                LinuxCommand.exec(mappingCmd);
+                //导出data命令
+                String dataCmd = "/usr/bin/elasticdump --input " + uri + " --output " + tmpDir + dataName + " --type=data";
+                LinuxCommand.exec(dataCmd);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
         }
 
         //获取外网同步正式目录

+ 25 - 6
sync-out/src/main/java/com/jjt/out/service/impl/OutMysqlServiceImpl.java

@@ -26,6 +26,7 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -55,8 +56,8 @@ public class OutMysqlServiceImpl extends OutBaseService implements IOutMysqlServ
 
         JSONObject jsonObject = JSONObject.parseObject(params);
 
-//        String hostname = jsonObject.getString("host");
-//        int port = jsonObject.getIntValue("port");
+        String host = jsonObject.getString("host");
+        int port = jsonObject.getIntValue("port");
 
         String user = jsonObject.getString("user");
         String pass = jsonObject.getString("pass");
@@ -68,10 +69,27 @@ public class OutMysqlServiceImpl extends OutBaseService implements IOutMysqlServ
         String syncDir = syncDIr();
         String exportName = String.format("%s_%s.sql", db, time);
         String exportPath = tmpDir + exportName;
-        String cmd = String.format("mysqldump -u%s -p%s %s > %s", user, pass, db, exportPath);
-        LinuxCommand.exec(cmd);
+
+        List<String> commands = new ArrayList<>();
+        commands.add("/usr/bin/mysqldump");
+        commands.add("-u" + user);
+        commands.add("-h");
+        commands.add(host);
+        commands.add("-P");
+        commands.add(port + "");
+        commands.add("-p" + pass);
+        commands.add("-B");
+        commands.add(db);
+        commands.add("--set-gtid-purged=off");
+        commands.add("--default-character-set=utf8");
+        commands.add("--skip-tz-utc");
+        commands.add("--lock-tables=false");
+        commands.add("--skip-add-locks");
+
 
         try {
+
+            LinuxCommand.mysqlExport(commands, exportPath);
             //移动备份文件
             Path target = Paths.get(syncDir + exportName);
             Files.move(Paths.get(exportPath), target, StandardCopyOption.REPLACE_EXISTING);
@@ -87,8 +105,9 @@ public class OutMysqlServiceImpl extends OutBaseService implements IOutMysqlServ
             ObjectMapper mapper = new ObjectMapper();
             mapper.writeValue(descFile, desc);
             //生成描述json文件--end
-        } catch (IOException e) {
-            log.error("mysql备份文件从临时目录移动至同步目录失败!", exportPath, syncDir + exportName);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("mysql备份文件从临时目录{}移动至同步目录{}失败!", exportPath, syncDir + exportName);
         }
     }
 }

+ 22 - 0
sync-out/src/main/java/com/jjt/out/task/MysqlOutTask.java

@@ -0,0 +1,22 @@
+package com.jjt.out.task;
+
+import com.jjt.out.service.IOutEsService;
+import com.jjt.out.service.IOutMysqlService;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+
+/**
+ * Es定时任务
+ *
+ * @author ruoyi
+ */
+@Component("mysqlOutTask")
+public class MysqlOutTask {
+    @Resource
+    private IOutMysqlService mysqlService;
+
+    public void export() {
+        mysqlService.export();
+    }
+}