Browse Source

dmaster 构建出的 jar 包现在能复制到指定目录

reghao 4 years ago
parent
commit
ab1021d79e

+ 6 - 0
dagent/bin/console.log

@@ -0,0 +1,6 @@
+14:23:40.016 [main] DEBUG oshi.util.FileUtil - Reading file /proc/stat
+14:23:40.020 [main] DEBUG oshi.util.FileUtil - Reading file /etc/os-release
+14:23:40.020 [main] DEBUG oshi.software.os.linux.LinuxOperatingSystem - os-release: NAME="Manjaro Linux"
+14:23:40.021 [main] DEBUG oshi.util.FileUtil - Reading file /proc/version
+14:23:40.033 [main] DEBUG oshi.util.FileUtil - Reading file /proc/cpuinfo
+14:23:40.501 [MQTT Call: dagent5d1a727991f34d3a9c1220a1899e6ebd] INFO cn.reghao.autodop.dagent.mqttsub.DagentConnActionListener - MQTT 连接建立成功,开始订阅 topic

+ 5 - 0
dagent/bin/mqtt.json

@@ -0,0 +1,5 @@
+{
+  "broker": "tcp://localhost:1883",
+  "username": "dev",
+  "password": "Dev@123456"
+}

+ 5 - 0
dagent/bin/mqtt1.json

@@ -0,0 +1,5 @@
+{
+  "broker": "tcp://192.168.0.62:1883",
+  "username": "test",
+  "password": "Test@123456"
+}

+ 3 - 2
dagent/bin/start.sh

@@ -1,4 +1,5 @@
 #!/bin/bash
 
-app_path='/opt/apps/dagent/autodop-dagent.jar'
-nohup java -jar ${app_path} > console.log 2>&1 &
+app_dir=`pwd`
+app_name='dagent.jar'
+nohup java -jar ${app_dir}"/"${app_name} ${app_dir}/mqtt.json > console.log 2>&1 &

+ 53 - 0
dagent/src/main/java/cn/reghao/autodop/dagent/ConfigFile.java

@@ -0,0 +1,53 @@
+package cn.reghao.autodop.dagent;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.File;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @author reghao
+ * @date 2021-03-03 18:47:01
+ */
+@Slf4j
+public class ConfigFile {
+    public static String configFilePath(String arg, Class<?> clazz) {
+        String configFilePath = null;
+        File configFile = new File(arg);
+        if (!configFile.exists()) {
+            if (arg.startsWith("./")) {
+                String filename = arg.replace(".", "")
+                        .replace("/", "");
+                configFilePath = runningHome(clazz) + "/" + filename;
+            } else if (!arg.contains("/")) {
+                configFilePath = runningHome(clazz) + "/" + arg;
+            } else {
+                log.error("相对路径的配置文件必须以 ./configFile 或 configFile 形式指定...");
+            }
+        } else {
+            // 绝对路径
+            configFilePath = arg;
+        }
+        return configFilePath;
+    }
+    
+    /**
+     * jar 文件运行目录
+     *
+     * @param
+     * @return
+     * @date 2021-03-03 下午6:33
+     */
+    public static String runningHome(Class<?> clazz) {
+        URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
+        String jarFilePath = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8);
+        if (jarFilePath.endsWith(".jar")) {
+            jarFilePath = jarFilePath.substring(0, jarFilePath.lastIndexOf("/") + 1);
+        }
+
+        File file = new File(jarFilePath);
+        return file.getAbsolutePath();
+    }
+}

+ 25 - 35
dagent/src/main/java/cn/reghao/autodop/dagent/DagentApp.java

@@ -12,6 +12,8 @@ import cn.reghao.autodop.dagent.mqttsub.DagentTopicListener;
 import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzDispatcher;
 import cn.reghao.autodop.dagent.mqttsub.impl.AppRpcClazzImpl;
 import cn.reghao.jdkutil.http.WebClient;
+import cn.reghao.jdkutil.serializer.JsonConverter;
+import cn.reghao.jdkutil.text.TextFile;
 import com.sun.net.httpserver.HttpServer;
 import lombok.extern.slf4j.Slf4j;
 import org.eclipse.paho.client.mqttv3.MqttException;
@@ -19,6 +21,7 @@ import oshi.SystemInfo;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.util.Random;
 
 @Slf4j
 public class DagentApp {
@@ -26,32 +29,15 @@ public class DagentApp {
 	static DagentTopicListener dagentTopicListener;
 	static NodeEventClazzPubImpl nodeEventClazzPub;
 
-	static void startMqtt() throws MqttException {
-		mqttClient.add(MsgQueue.dagentTopic(Machine.ID), dagentTopicListener);
-		DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
-		mqttClient.connect(connActionListener);
-	}
-
 	static void pubDagentShutdown() {
 		nodeEventClazzPub.nodeShutdown();
 		log.info("Dagent 停止");
 	}
 
-	/**
-	 * 优雅关闭爬虫(进程级别的关闭)
-	 *
-	 * @date 2019-08-16 下午3:45
-	 */
 	static void shutdownGracefully() {
 		Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(), "main-shutdown-hook"));
 	}
 
-	/**
-	 * 1.可以处理 kill -2 或 kill -15
-	 * 2.无法处理 kill -9 和机器断电的情况
-	 *
-	 * @date 2019-08-16 下午4:07
-	 */
 	static class ShutdownHook implements Runnable {
 		@Override
 		public void run() {
@@ -60,28 +46,32 @@ public class DagentApp {
 		}
 	}
 
-	/**
-	 * 1.停止从 UrlScheduler 中获取数据
-	 * 2.停止向 DataProducer 提交数据
-	 * 3.线程池停止接受新任务,并等待当前执行的任务完成
-	 *
-	 * @param
-	 * @return
-	 * @date 2021-04-02 下午11:26
-	 */
 	static void stop() {
+		pubDagentShutdown();
 		log.info("资源清理完成,结束 DagentApp...");
 	}
 
-	public static void main(String[] args) throws MqttException, IOException {
-		shutdownGracefully();
+	static void singleInstance() {
+		int min = 30000;
+		int max = 50000;
+		int range = max-min+1;
+		int port = new Random().nextInt(range) + min;
+		try {
+			HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0).start();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
 
-		MqttProperties mqttProperties = new MqttProperties();
-		mqttProperties.setBroker("tcp://localhost:1883");
+	public static void main(String[] args) throws MqttException {
+		if (args.length != 1) {
+			log.error("必须指定配置文件...");
+			return;
+		}
+		String configFilePath = ConfigFile.configFilePath(args[0], DagentApp.class);
+		String json = new TextFile().readFile(configFilePath);
+		MqttProperties mqttProperties = JsonConverter.jsonToObject(json, MqttProperties.class);
 		mqttProperties.setClientId(AppId.dagent.name());
-		mqttProperties.setUsername("dev");
-		mqttProperties.setPassword("Dev@123456");
-
 		mqttClient = new AsyncMqttClient(mqttProperties);
 
 		IAppRpcClazz appRpcClazz = new AppRpcClazzImpl();
@@ -95,7 +85,7 @@ public class DagentApp {
 		DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
 		mqttClient.connect(connActionListener);
 
-		HttpServer.create(new InetSocketAddress("127.0.0.1", 9790), 0).start();
-		//pubDagentShutdown();
+		shutdownGracefully();
+		singleInstance();
 	}
 }

+ 34 - 0
dagent/src/main/java/cn/reghao/autodop/dagent/JdkAppContext.java

@@ -0,0 +1,34 @@
+package cn.reghao.autodop.dagent;
+
+import com.sun.net.httpserver.HttpServer;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Random;
+
+/**
+ * @author reghao
+ * @date 2021-10-31 13:38:08
+ */
+public class JdkAppContext {
+    public void start(Runnable hook) {
+        shutdownGracefully(hook);
+        singleInstance();
+    }
+
+    void shutdownGracefully(Runnable hook) {
+        Runtime.getRuntime().addShutdownHook(new Thread(hook, "main-shutdown-hook"));
+    }
+
+    void singleInstance() {
+        int min = 30000;
+        int max = 50000;
+        int range = max-min+1;
+        int port = new Random().nextInt(range) + min;
+        try {
+            HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0).start();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 1 - 1
dmaster/bin/shutdown.sh

@@ -1,4 +1,4 @@
 #!/bin/bash
 
-pid=`jps | grep autodop-dmaster.jar | awk '{print $1}'`
+pid=`jps | grep dmaster.jar | awk '{print $1}'`
 kill -15 ${pid}

+ 3 - 1
dmaster/bin/start.sh

@@ -1,3 +1,5 @@
 #!/bin/bash
 
-nohup java -jar /opt/apps/dmaster/autodop-dmaster.jar > console.log 2>&1 &
+app_dir=`pwd`
+app_name='dmaster.jar'
+nohup java -jar ${app_dir}"/"${app_name} > console.log 2>&1 &

+ 7 - 0
dmaster/pom.xml

@@ -274,6 +274,13 @@
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <version>2.3.9.RELEASE</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
                 <configuration>
                     <outputDirectory>${project.build.outputDir}</outputDirectory>
                 </configuration>

+ 0 - 26
dmaster/src/main/java/cn/reghao/autodop/dmaster/spring/config/OssProperties.java

@@ -1,26 +0,0 @@
-package cn.reghao.autodop.dmaster.spring.config;
-
-import lombok.Data;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.stereotype.Component;
-
-/**
- * @author reghao
- * @date 2019-08-27 00:17:55
- */
-@Data
-@Component
-@ConfigurationProperties(prefix = "oss")
-public class OssProperties {
-    @Value("${oss.host}")
-    private String host;
-    @Value("${oss.key}")
-    private String key;
-    @Value("${oss.secret}")
-    private String secret;
-    @Value("${oss.bucket}")
-    private String bucket;
-    @Value("${oss.folder}")
-    private String folder;
-}

+ 0 - 167
dmaster/src/main/java/cn/reghao/autodop/dmaster/util/AliOss.java

@@ -1,167 +0,0 @@
-package cn.reghao.autodop.dmaster.util;
-
-import cn.reghao.autodop.dmaster.spring.config.OssProperties;
-import com.aliyun.oss.OSSClient;
-import com.aliyun.oss.model.*;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.*;
-import java.nio.file.attribute.BasicFileAttributes;
-import java.time.LocalDate;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * 阿里云 OSS 工具类
- *
- * @author reghao
- * @date 2019-08-27 00:17:55
- */
-@Slf4j
-@Service
-public class AliOss {
-    private OssProperties ossProperties;
-    private OSSClient client;
-
-    // TODO 使用工厂方法获取实例
-    public AliOss(OssProperties ossProperties) {
-        this.ossProperties = ossProperties;
-        client = new OSSClient(ossProperties.getHost(), ossProperties.getKey(), ossProperties.getSecret());
-    }
-
-    /**
-     * 备份已有文件/目录后再上传
-     *
-     * @param
-     * @return
-     * @date 2019-08-28 下午2:03
-     */
-    public void backupAndUpload(String ossPath, String filePath) {
-        String ossPath1 = ossProperties.getFolder() + ossPath;
-        if (exists(ossPath1)) {
-            String dst = ossPath1 + "-" + LocalDate.now().toString();
-            log.info("备份 {} 到 {} ", ossPath1, dst);
-            move(ossPath1, dst, true);
-        }
-
-        File file = new File(filePath);
-        if (file.isDirectory()) {
-            uploadDir(ossPath1, filePath);
-        } else {
-            uploadFile(ossPath1, file);
-        }
-    }
-
-    /**
-     * 移动文件或目录
-     *
-     * @param
-     * @return
-     * @date 2019-08-28 下午2:04
-     */
-    public void move(String src, String dst, boolean flag) {
-        List<String> keys = new ArrayList<>();
-        String prefix = src + "/";;
-        while (prefix != null) {
-            ObjectListing objs = client.listObjects(ossProperties.getBucket(), prefix);
-            objs.getObjectSummaries().forEach(object -> {
-                String key = object.getKey();
-                String target = dst + key.split(src)[1];
-
-                client.copyObject(ossProperties.getBucket(), key, ossProperties.getBucket(), target);
-                keys.add(key);
-            });
-
-            prefix = objs.getNextMarker();
-        }
-
-        if (flag) {
-            delete(keys);
-        }
-    }
-
-    /**
-     * 批量删除
-     *
-     * @param
-     * @return
-     * @date 2019-08-28 下午4:38
-     */
-    public void delete(List<String> keys) {
-        DeleteObjectsResult deleteResult =
-                client.deleteObjects(new DeleteObjectsRequest(ossProperties.getBucket()).withKeys(keys));
-    }
-
-    /**
-     * 上传文件
-     *
-     * @param
-     * @return
-     * @date 2019-08-28 下午2:21
-     */
-    public void uploadFile(String ossPath, File file) {
-        client.putObject(ossProperties.getBucket(), ossPath, file);
-    }
-
-    /**
-     * 上传文件夹
-     *
-     * @param
-     * @return
-     * @date 2019-08-28 下午8:33
-     */
-    public void uploadDir(String ossBasePath, String dirPath) {
-        Path path = Paths.get(dirPath);
-        try {
-            Files.walkFileTree(path, new FileVisitor<Path>() {
-                @Override
-                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
-                    return FileVisitResult.CONTINUE;
-                }
-
-                // 处理目录中的文件
-                @Override
-                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
-                    File tmp = file.toFile();
-                    String ossPath = ossBasePath + tmp.getPath().split(dirPath)[1];
-                    uploadFile(ossPath, tmp);
-                    return FileVisitResult.CONTINUE;
-                }
-
-                @Override
-                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
-                    log.info("无法查看 {} ", file.toString());
-                    return FileVisitResult.CONTINUE;
-                }
-
-                @Override
-                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
-                    return FileVisitResult.CONTINUE;
-                }
-            });
-        } catch (IOException ioe) {
-            ioe.printStackTrace();
-        }
-    }
-
-    /**
-     * 判断 OSS 中是否存在待上传的文件/目录
-     *
-     * @param
-     * @return
-     * @date 2019-08-28 下午2:40
-     */
-    private boolean exists(String src) {
-        String prefix = src + "/";
-        ObjectListing objects = client.listObjects(ossProperties.getBucket(), prefix);
-        int size = objects.getObjectSummaries().size();
-        if (size == 0) {
-            return false;
-        } else {
-            return true;
-        }
-    }
-}

+ 0 - 6
dmaster/src/main/resources/application-dev.yml

@@ -20,9 +20,3 @@ mosquitto:
 #  username: test
 #  password: Test@123456
 #  clientId: dmaster
-oss:
-  host: http://static.reghao.icu
-  key: minioadmin
-  secret: minioadmin
-  bucket: autodop
-  folder: dev/1/2/3

+ 1 - 7
dmaster/src/main/resources/application-dev0.yml

@@ -10,10 +10,4 @@ mosquitto:
   broker: tcp://mosquitto.alpha.iquizoo.com:1883
   username: test
   password: Test@123456
-  clientId: dmaster
-oss:
-  host: http://static.reghao.icu
-  key: minioadmin
-  secret: minioadmin
-  bucket: autodop
-  folder: test/1/2/3
+  clientId: dmaster

+ 1 - 7
dmaster/src/main/resources/application-prod.yml

@@ -10,10 +10,4 @@ mosquitto:
   broker: tcp://192.168.0.211:1883
   username: test
   password: Test@123456
-  clientId: dmaster
-oss:
-  host: http://static.reghao.icu
-  key: minioadmin
-  secret: minioadmin
-  bucket: autodop
-  folder: prod/1/2/3
+  clientId: dmaster

+ 6 - 12
dmaster/src/main/resources/application-test.yml

@@ -1,19 +1,13 @@
 spring:
   datasource:
-    url: jdbc:mysql://192.168.0.77:3306/iquizoo_devops_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
-    username: azy_test
-    password: Azy@123456
+    url: jdbc:mysql://192.168.0.50:3306/tnb_devops_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
+    username: test
+    password: Test@123456
   data:
     mongodb:
-      uri: mongodb://192.168.0.220/devops_tdb
+      uri: mongodb://192.168.0.50/tnb_devops_tdb
 mosquitto:
-  broker: tcp://s75.iquizoo.com:1883
+  broker: tcp://192.168.0.62:1883
   username: test
   password: Test@123456
-  clientId: dmaster
-oss:
-  host: http://static.reghao.icu
-  key: minioadmin
-  secret: minioadmin
-  bucket: autodop
-  folder: test/1/2/3
+  clientId: dmaster

+ 13 - 0
dmaster/src/main/resources/application-test0.yml

@@ -0,0 +1,13 @@
+spring:
+  datasource:
+    url: jdbc:mysql://192.168.0.77:3306/iquizoo_devops_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
+    username: azy_test
+    password: Azy@123456
+  data:
+    mongodb:
+      uri: mongodb://192.168.0.220/devops_tdb
+mosquitto:
+  broker: tcp://s75.iquizoo.com:1883
+  username: test
+  password: Test@123456
+  clientId: dmaster