feat: supprt random port #98

This commit is contained in:
KFCFans 2020-11-14 10:55:05 +08:00
parent fe0291fd19
commit 19112db038
2 changed files with 43 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
import static java.util.Collections.emptyList;
@ -34,6 +35,10 @@ import static java.util.Collections.emptyList;
*/
@Slf4j
public class NetUtils {
// returned port range is [30000, 39999]
private static final int RND_PORT_START = 30000;
private static final int RND_PORT_END = 65535;
private static volatile String HOST_ADDRESS;
private static final String LOCALHOST_VALUE = "127.0.0.1";
@ -41,6 +46,10 @@ public class NetUtils {
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
private static final String ANYHOST_VALUE = "0.0.0.0";
public static int getRandomPort() {
return ThreadLocalRandom.current().nextInt(RND_PORT_START, RND_PORT_END);
}
/**
* 获取本机 IP 地址
* @return 本机 IP 地址

View File

@ -1,12 +1,12 @@
package com.github.kfcfans.powerjob.worker.common;
import com.github.kfcfans.powerjob.common.RemoteConstant;
import com.github.kfcfans.powerjob.common.utils.NetUtils;
import com.github.kfcfans.powerjob.worker.common.constants.StoreStrategy;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.Setter;
import java.util.Collections;
import java.util.List;
/**
@ -15,7 +15,7 @@ import java.util.List;
* @author tjq
* @since 2020/3/16
*/
@Data
@Setter
public class OhMyConfig {
/**
* 应用名称
@ -48,4 +48,35 @@ public class OhMyConfig {
* true -> 用于本地写单元测试调试 false -> 默认值标准模式
*/
private boolean enableTestMode = false;
public String getAppName() {
return appName;
}
public int getPort() {
if (port > 0) {
return port;
}
return NetUtils.getRandomPort();
}
public List<String> getServerAddress() {
return serverAddress;
}
public StoreStrategy getStoreStrategy() {
return storeStrategy;
}
public int getMaxResultLength() {
return maxResultLength;
}
public Object getUserContext() {
return userContext;
}
public boolean isEnableTestMode() {
return enableTestMode;
}
}