|
|
@ -2,8 +2,10 @@ package com.xxl.job.core.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -15,23 +17,24 @@ public class NetUtil {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(NetUtil.class);
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(NetUtil.class);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* find avaliable port
|
|
|
|
* find avaliable port by ip
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param defaultPort
|
|
|
|
* @param defaultPort
|
|
|
|
|
|
|
|
* @param ip
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static int findAvailablePort(int defaultPort) {
|
|
|
|
public static int findAvailablePort(int defaultPort,String ip) {
|
|
|
|
int portTmp = defaultPort;
|
|
|
|
int portTmp = defaultPort;
|
|
|
|
while (portTmp < 65535) {
|
|
|
|
while (portTmp < 65535) {
|
|
|
|
if (!isPortUsed(portTmp)) {
|
|
|
|
if (!isPortUsed(portTmp,ip)) {
|
|
|
|
return portTmp;
|
|
|
|
return portTmp;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
portTmp++;
|
|
|
|
portTmp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
portTmp = defaultPort--;
|
|
|
|
portTmp = --defaultPort;
|
|
|
|
while (portTmp > 0) {
|
|
|
|
while (portTmp > 0) {
|
|
|
|
if (!isPortUsed(portTmp)) {
|
|
|
|
if (!isPortUsed(portTmp,ip)) {
|
|
|
|
return portTmp;
|
|
|
|
return portTmp;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
portTmp--;
|
|
|
|
portTmp--;
|
|
|
@ -40,18 +43,34 @@ public class NetUtil {
|
|
|
|
throw new IllegalStateException("no available port.");
|
|
|
|
throw new IllegalStateException("no available port.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* find avaliable port
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param defaultPort
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static int findAvailablePort(int defaultPort) {
|
|
|
|
|
|
|
|
return findAvailablePort(defaultPort,null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* check port used
|
|
|
|
* check port used
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param port
|
|
|
|
* @param port
|
|
|
|
|
|
|
|
* @param ip 为空则为 InetAddress.anyLocalAddress()
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean isPortUsed(int port) {
|
|
|
|
public static boolean isPortUsed(int port,String ip) {
|
|
|
|
boolean used = false;
|
|
|
|
boolean used = false;
|
|
|
|
ServerSocket serverSocket = null;
|
|
|
|
ServerSocket serverSocket = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
serverSocket = new ServerSocket(port);
|
|
|
|
if(StringUtils.isEmpty(ip)){
|
|
|
|
used = false;
|
|
|
|
serverSocket = new ServerSocket(port);
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
serverSocket = new ServerSocket();
|
|
|
|
|
|
|
|
serverSocket.bind(new InetSocketAddress(ip,port));
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.debug(">>>>>>>>>>> xxl-job, port[{}] is in use.", port);
|
|
|
|
logger.debug(">>>>>>>>>>> xxl-job, port[{}] is in use.", port);
|
|
|
|
used = true;
|
|
|
|
used = true;
|
|
|
|