提交 b00ec32e authored 作者: xuxueli's avatar xuxueli

IP获取逻辑优化,优先遍历网卡来获取可用IP;

上级 7b373b66
...@@ -1318,6 +1318,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段 ...@@ -1318,6 +1318,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
- 7、[迭代中]docker镜像,并且推送docker镜像到中央仓库,更进一步实现产品开箱即用; - 7、[迭代中]docker镜像,并且推送docker镜像到中央仓库,更进一步实现产品开箱即用;
- 8、[迭代中]cron在线生成工具,如 "cronboot/cron.qqe2"; - 8、[迭代中]cron在线生成工具,如 "cronboot/cron.qqe2";
- 9、[迭代中]原生提供通用命令行任务Handler(Bean任务,"CommandJobHandler");业务方只需要提供命令行即可,可执行任意命令; - 9、[迭代中]原生提供通用命令行任务Handler(Bean任务,"CommandJobHandler");业务方只需要提供命令行即可,可执行任意命令;
- 10、IP获取逻辑优化,优先遍历网卡来获取可用IP;
### TODO LIST ### TODO LIST
- 1、任务分片路由:分片采用一致性Hash算法计算出尽量稳定的分片顺序,即使注册机器存在波动也不会引起分批分片顺序大的波动;目前采用IP自然排序,可以满足需求,待定; - 1、任务分片路由:分片采用一致性Hash算法计算出尽量稳定的分片顺序,即使注册机器存在波动也不会引起分批分片顺序大的波动;目前采用IP自然排序,可以满足需求,待定;
......
package com.xxl.job.core.util; package com.xxl.job.core.util;
import java.net.Inet6Address; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* get ip * get ip
*
* @author xuxueli 2016-5-22 11:38:05 * @author xuxueli 2016-5-22 11:38:05
*/ */
public class IpUtil { public class IpUtil {
...@@ -23,16 +21,17 @@ public class IpUtil { ...@@ -23,16 +21,17 @@ public class IpUtil {
private static final String LOCALHOST = "127.0.0.1"; private static final String LOCALHOST = "127.0.0.1";
public static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$"); public static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
private static volatile InetAddress LOCAL_ADDRESS = null; private static volatile String LOCAL_ADDRESS = null;
/** /**
* valid address * valid address
* @param address * @param address
* @return * @return boolean
*/ */
private static boolean isValidAddress(InetAddress address) { private static boolean isValidAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress()) if (address == null || address.isLoopbackAddress() || address.isLinkLocalAddress()) {
return false; return false;
}
String name = address.getHostAddress(); String name = address.getHostAddress();
return (name != null return (name != null
&& ! ANYHOST.equals(name) && ! ANYHOST.equals(name)
...@@ -42,9 +41,12 @@ public class IpUtil { ...@@ -42,9 +41,12 @@ public class IpUtil {
/** /**
* get first valid addredd * get first valid addredd
* @return *
* @return InetAddress
*/ */
private static InetAddress getFirstValidAddress() { private static InetAddress getFirstValidAddress() {
// NetworkInterface address
try { try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces != null) { if (interfaces != null) {
...@@ -72,11 +74,10 @@ public class IpUtil { ...@@ -72,11 +74,10 @@ public class IpUtil {
} catch (Throwable e) { } catch (Throwable e) {
logger.error("Failed to retriving ip address, " + e.getMessage(), e); logger.error("Failed to retriving ip address, " + e.getMessage(), e);
} }
// getLocalHost address
InetAddress localAddress = null;
try { try {
localAddress = InetAddress.getLocalHost(); InetAddress localAddress = InetAddress.getLocalHost();
if (isValidAddress(localAddress)) { if (isValidAddress(localAddress)) {
return localAddress; return localAddress;
} }
...@@ -85,38 +86,38 @@ public class IpUtil { ...@@ -85,38 +86,38 @@ public class IpUtil {
} }
logger.error("Could not get local host ip address, will use 127.0.0.1 instead."); logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
return localAddress; return null;
} }
/** /**
* get address * get address
* @return *
* @return String
*/ */
private static InetAddress getAddress() { private static String getAddress() {
if (LOCAL_ADDRESS != null) if (LOCAL_ADDRESS != null) {
return LOCAL_ADDRESS; return LOCAL_ADDRESS;
}
InetAddress localAddress = getFirstValidAddress(); InetAddress localAddress = getFirstValidAddress();
LOCAL_ADDRESS = localAddress; LOCAL_ADDRESS = localAddress.getHostAddress();
return localAddress; return LOCAL_ADDRESS;
} }
/** /**
* get ip * get ip
* @return *
* @return String
*/ */
public static String getIp(){ public static String getIp(){
InetAddress address = getAddress(); return getAddress();
if (address==null) {
return null;
}
return address.getHostAddress();
} }
/** /**
* get ip:port * get ip:port
*
* @param port * @param port
* @return * @return String
*/ */
public static String getIpPort(int port){ public static String getIpPort(int port){
String ip = getIp(); String ip = getIp();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论