Browse Source

执行器端口支持随机生成(小于等于0时),避免端口定义冲突

xuxueli 8 years ago
parent
commit
41328f4795

+ 3 - 2
doc/XXL-JOB官方文档.md View File

@@ -1053,6 +1053,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
1053 1053
 - 7、底层系统日志级别规范调整,清理遗留代码;
1054 1054
 - 8、修改JobThread捕获Error错误不更新JobLog的问题; 
1055 1055
 - 9、任务注解调整为 “@JobHandler”,与任务注解统一;
1056
+- 10、执行器端口支持随机生成(小于等于0时),避免端口定义冲突;
1056 1057
 
1057 1058
 ### TODO LIST
1058 1059
 - 1、任务权限管理:执行器为粒度分配权限,核心操作校验权限;
@@ -1075,8 +1076,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
1075 1076
 - 18、JobHandler提供 init/destroy 方法,支持自定义任务线程销毁逻辑;
1076 1077
 - 19、cron表达式的最大长度调整,兼容复杂类型cron;
1077 1078
 - 20、执行器回调地址/日志地址格式兼容,是否已"/"结尾均支持;
1078
-- 21、执行器端口支持随机生成,避免端口冲突
1079
-- 22、任务单机多线程:提升任务单机并行处理能力;
1079
+- 21、任务单机多线程:提升任务单机并行处理能力
1080
+
1080 1081
 
1081 1082
 ## 七、其他
1082 1083
 

+ 6 - 1
xxl-job-core/src/main/java/com/xxl/job/core/executor/XxlJobExecutor.java View File

@@ -9,6 +9,7 @@ import com.xxl.job.core.log.XxlJobFileAppender;
9 9
 import com.xxl.job.core.rpc.netcom.NetComClientProxy;
10 10
 import com.xxl.job.core.rpc.netcom.NetComServerFactory;
11 11
 import com.xxl.job.core.thread.JobThread;
12
+import com.xxl.job.core.util.NetUtil;
12 13
 import org.slf4j.Logger;
13 14
 import org.slf4j.LoggerFactory;
14 15
 import org.springframework.beans.BeansException;
@@ -28,7 +29,7 @@ public class XxlJobExecutor implements ApplicationContextAware {
28 29
 
29 30
     // ---------------------- param ----------------------
30 31
     private String ip;
31
-    private int port = 9999;
32
+    private int port;
32 33
     private String appName;
33 34
     private String adminAddresses;
34 35
     private String accessToken;
@@ -121,6 +122,10 @@ public class XxlJobExecutor implements ApplicationContextAware {
121 122
     // ---------------------- executor-server(jetty) ----------------------
122 123
     private NetComServerFactory serverFactory = new NetComServerFactory();
123 124
     private void initExecutorServer(int port, String ip, String appName, String accessToken) throws Exception {
125
+        // valid param
126
+        port = port>0?port: NetUtil.findAvailablePort(9999);
127
+
128
+        // start server
124 129
         NetComServerFactory.putService(ExecutorBiz.class, new ExecutorBizImpl());   // rpc-service, base on jetty
125 130
         NetComServerFactory.setAccessToken(accessToken);
126 131
         serverFactory.start(port, ip, appName); // jetty + registry

+ 70 - 0
xxl-job-core/src/main/java/com/xxl/job/core/util/NetUtil.java View File

@@ -0,0 +1,70 @@
1
+package com.xxl.job.core.util;
2
+
3
+import org.slf4j.Logger;
4
+import org.slf4j.LoggerFactory;
5
+
6
+import java.io.IOException;
7
+import java.net.ServerSocket;
8
+
9
+/**
10
+ * net util
11
+ *
12
+ * @author xuxueli 2017-11-29 17:00:25
13
+ */
14
+public class NetUtil {
15
+    private static Logger logger = LoggerFactory.getLogger(NetUtil.class);
16
+
17
+    /**
18
+     * find avaliable port
19
+     *
20
+     * @param defaultPort
21
+     * @return
22
+     */
23
+    public static int findAvailablePort(int defaultPort) {
24
+        int portTmp = defaultPort;
25
+        while (portTmp < 65535) {
26
+            if (!isPortUsed(portTmp)) {
27
+                return portTmp;
28
+            } else {
29
+                portTmp++;
30
+            }
31
+        }
32
+        portTmp = defaultPort--;
33
+        while (portTmp > 0) {
34
+            if (!isPortUsed(portTmp)) {
35
+                return portTmp;
36
+            } else {
37
+                portTmp--;
38
+            }
39
+        }
40
+        throw new IllegalStateException("no available port.");
41
+    }
42
+
43
+    /**
44
+     * check port used
45
+     *
46
+     * @param port
47
+     * @return
48
+     */
49
+    public static boolean isPortUsed(int port) {
50
+        boolean used = false;
51
+        ServerSocket serverSocket = null;
52
+        try {
53
+            serverSocket = new ServerSocket(port);
54
+            used = false;
55
+        } catch (IOException e) {
56
+            logger.info(">>>>>>>>>>> xxl-job, port[{}] is in use.", port);
57
+            used = true;
58
+        } finally {
59
+            if (serverSocket != null) {
60
+                try {
61
+                    serverSocket.close();
62
+                } catch (IOException e) {
63
+                    logger.info("");
64
+                }
65
+            }
66
+        }
67
+        return used;
68
+    }
69
+
70
+}