فهرست منبع

通过监听 TCP 端口来保证只有一个实例

reghao 4 سال پیش
والد
کامیت
0f22cbfddb
6فایلهای تغییر یافته به همراه53 افزوده شده و 56 حذف شده
  1. 0 6
      dagent/bin/console.log
  2. 2 1
      dagent/bin/shutdown.sh
  3. 2 2
      dagent/bin/start.sh
  4. 1 1
      dagent/pom.xml
  5. 23 15
      dagent/src/main/java/cn/reghao/autodop/dagent/DagentApp.java
  6. 25 31
      dmaster/pom.xml

+ 0 - 6
dagent/bin/console.log

@@ -1,6 +0,0 @@
-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

+ 2 - 1
dagent/bin/shutdown.sh

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

+ 2 - 2
dagent/bin/start.sh

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

+ 1 - 1
dagent/pom.xml

@@ -24,7 +24,7 @@
     </dependencies>
 
     <build>
-        <finalName>${project.artifactId}</finalName>
+        <finalName>${project.parent.artifactId}-${project.artifactId}</finalName>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>

+ 23 - 15
dagent/src/main/java/cn/reghao/autodop/dagent/DagentApp.java

@@ -6,6 +6,7 @@ import cn.reghao.autodop.common.mqtt.MqttProperties;
 import cn.reghao.autodop.common.msg.MsgQueue;
 import cn.reghao.autodop.common.msg.pub.dto.node.constant.AppId;
 import cn.reghao.autodop.common.msg.rpc.clazz.IAppRpcClazz;
+import cn.reghao.autodop.common.util.ExceptionUtil;
 import cn.reghao.autodop.dagent.machine.NodeEventClazzPubImpl;
 import cn.reghao.autodop.dagent.mqttsub.DagentConnActionListener;
 import cn.reghao.autodop.dagent.mqttsub.DagentTopicListener;
@@ -14,17 +15,15 @@ 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;
 import oshi.SystemInfo;
 
 import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.util.Random;
+import java.net.ServerSocket;
 
 @Slf4j
 public class DagentApp {
+	static ServerSocket serverSocket;
 	static AsyncMqttClient mqttClient;
 	static DagentTopicListener dagentTopicListener;
 	static NodeEventClazzPubImpl nodeEventClazzPub;
@@ -47,23 +46,34 @@ public class DagentApp {
 	}
 
 	static void stop() {
+		try {
+			serverSocket.close();
+		} catch (IOException e) {
+			String errmsg = ExceptionUtil.errorMsg(e);
+			log.error("关闭 54321 端口出错 {}", errmsg);
+		}
+
 		pubDagentShutdown();
-		log.info("资源清理完成,结束 DagentApp...");
+		log.info("资源清理完成,结束 autodop-dagent...");
 	}
 
-	static void singleInstance() {
-		int min = 30000;
-		int max = 50000;
-		int range = max-min+1;
-		int port = new Random().nextInt(range) + min;
+	/**
+	 * 通过监听一个端口来保证一台机器上只运行一个 autodop-dagent 实例
+	 *
+	 * @param
+	 * @return
+	 * @date 2021-11-02 下午3:01
+	 */
+	static void singleInstance() throws Exception {
 		try {
-			HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0).start();
+			serverSocket = new ServerSocket(54321);
 		} catch (IOException e) {
-			e.printStackTrace();
+			throw new Exception("已有一个 autodop-dagent 实例在运行, 一台机器上只能运行一个 autodop-dagent 实例.");
 		}
 	}
 
-	public static void main(String[] args) throws MqttException {
+	public static void main(String[] args) throws Exception {
+		singleInstance();
 		if (args.length != 1) {
 			log.error("必须指定配置文件...");
 			return;
@@ -84,8 +94,6 @@ public class DagentApp {
 		mqttClient.add(MsgQueue.dagentTopic(Machine.ID), dagentTopicListener);
 		DagentConnActionListener connActionListener = new DagentConnActionListener(mqttClient, nodeEventClazzPub);
 		mqttClient.connect(connActionListener);
-
 		shutdownGracefully();
-		singleInstance();
 	}
 }

+ 25 - 31
dmaster/pom.xml

@@ -34,6 +34,11 @@
             <version>1.0.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
@@ -51,37 +56,32 @@
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter</artifactId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-            <scope>true</scope>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-configuration-processor</artifactId>
-            <optional>true</optional>
+            <artifactId>spring-boot-starter-data-mongodb</artifactId>
         </dependency>
 
         <dependency>
-            <groupId>javax.validation</groupId>
-            <artifactId>validation-api</artifactId>
-            <version>2.0.1.Final</version>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-security</artifactId>
+            <artifactId>spring-boot-starter-cache</artifactId>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-test</artifactId>
-            <scope>test</scope>
+            <artifactId>spring-boot-starter-security</artifactId>
         </dependency>
 
         <dependency>
@@ -89,34 +89,34 @@
             <artifactId>spring-boot-starter-thymeleaf</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.thymeleaf.extras</groupId>
-            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
-        </dependency>
-
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-actuator</artifactId>
+            <artifactId>spring-boot-devtools</artifactId>
+            <optional>true</optional>
+            <scope>true</scope>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-jpa</artifactId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
         </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
         </dependency>
 
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-websocket</artifactId>
+            <groupId>javax.validation</groupId>
+            <artifactId>validation-api</artifactId>
+            <version>2.0.1.Final</version>
         </dependency>
 
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-cache</artifactId>
+            <groupId>org.thymeleaf.extras</groupId>
+            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
         </dependency>
 
         <dependency>
@@ -147,12 +147,6 @@
             <version>2.9.2</version>
         </dependency>
 
-        <dependency>
-            <groupId>com.aliyun.oss</groupId>
-            <artifactId>aliyun-sdk-oss</artifactId>
-            <version>2.8.3</version>
-        </dependency>
-
         <dependency>
             <groupId>org.apache.maven.shared</groupId>
             <artifactId>maven-invoker</artifactId>
@@ -227,7 +221,7 @@
     </profiles>
 
     <build>
-        <finalName>${project.artifactId}</finalName>
+        <finalName>${project.parent.artifactId}-${project.artifactId}</finalName>
         <resources>
             <resource>
                 <directory>src/main/resources</directory>