IP工具类优化,本地IP地址静态缓存;

This commit is contained in:
xueli.xue 2017-01-21 22:36:49 +08:00
parent e229a824ea
commit c86d7d1c8e
4 changed files with 101 additions and 24 deletions

View File

@ -700,6 +700,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
- 4、Jetty升级版本至8.2.0.v20160908 - 4、Jetty升级版本至8.2.0.v20160908
- 5、已推送V1.5.0和V1.5.1至Maven中央仓库 - 5、已推送V1.5.0和V1.5.1至Maven中央仓库
#### 6.10 版本 V1.5.2 特性
- 1、IP工具类优化IP静态缓存
#### 规划中 #### 规划中
- 1、集群执行器选择规则自定义单点=选择第一个,随机=随机选择一个; - 1、集群执行器选择规则自定义单点=选择第一个,随机=随机选择一个;
@ -712,9 +714,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
- 5、兼容oracle - 5、兼容oracle
- 6、跨天日志write问题修复 - 6、跨天日志write问题修复
- 7、rolling日志 - 7、rolling日志
- 8、IP工具类优化IP静态缓存 - 8、执行器server启动注册逻辑调整
- 9、执行器server启动注册逻辑调整 - 9、通过listener或stop方法容器销毁时销毁线程Daemon方式有时不太理想
- 10、通过listener或stop方法容器销毁时销毁线程Daemon方式有时不太理想
## 七、其他 ## 七、其他

View File

@ -24,10 +24,9 @@ public class XxlJobLogCallbackServer {
Server server = null; Server server = null;
public void start(int callBackPort) throws Exception { public void start(int callBackPort) throws Exception {
// init address
String ip = IpUtil.getIp(); // init address
trigger_log_address = ip.concat(":").concat(String.valueOf(callBackPort)); trigger_log_address = IpUtil.getIpPort(callBackPort);
final int port = Integer.valueOf(callBackPort); final int port = Integer.valueOf(callBackPort);
new Thread(new Runnable() { new Thread(new Runnable() {

View File

@ -94,7 +94,7 @@ public class XxlJobExecutor implements ApplicationContextAware {
public void run() { public void run() {
while (true) { while (true) {
try { try {
String address = IpUtil.getIp().concat(":").concat(String.valueOf(port)); String address = IpUtil.getIpPort(port);
registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address); registHelper.registry(RegistHelper.RegistType.EXECUTOR.name(), appName, address);
TimeUnit.SECONDS.sleep(RegistHelper.TIMEOUT); TimeUnit.SECONDS.sleep(RegistHelper.TIMEOUT);
} catch (Exception e) { } catch (Exception e) {

View File

@ -7,6 +7,7 @@ import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.regex.Pattern;
/** /**
* get ip * get ip
@ -15,36 +16,112 @@ import java.util.Enumeration;
public class IpUtil { public class IpUtil {
private static final Logger logger = LoggerFactory.getLogger(IpUtil.class); private static final Logger logger = LoggerFactory.getLogger(IpUtil.class);
public static final String ANYHOST = "0.0.0.0";
public static final String LOCALHOST = "127.0.0.1";
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
private static volatile InetAddress LOCAL_ADDRESS = null;
/** /**
* 获取本机ip * valid address
* @return ip * @param address
* @return
*/ */
public static String getIp() { private static boolean isValidAddress(InetAddress address) {
if (address == null || address.isLoopbackAddress())
return false;
String name = address.getHostAddress();
return (name != null
&& ! ANYHOST.equals(name)
&& ! LOCALHOST.equals(name)
&& IP_PATTERN.matcher(name).matches());
}
/**
* get first valid addredd
* @return
*/
private static InetAddress getFirstValidAddress() {
InetAddress localAddress = null;
try {
localAddress = InetAddress.getLocalHost();
if (isValidAddress(localAddress)) {
return localAddress;
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
try { try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
InetAddress address = null; if (interfaces != null) {
while (interfaces.hasMoreElements()) { while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement(); try {
Enumeration<InetAddress> addresses = ni.getInetAddresses(); NetworkInterface network = interfaces.nextElement();
Enumeration<InetAddress> addresses = network.getInetAddresses();
if (addresses != null) {
while (addresses.hasMoreElements()) { while (addresses.hasMoreElements()) {
address = addresses.nextElement(); try {
if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) { InetAddress address = addresses.nextElement();
if (isValidAddress(address)) {
return address;
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
}
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
}
}
} catch (Throwable e) {
logger.warn("Failed to retriving ip address, " + e.getMessage(), e);
}
logger.error("Could not get local host ip address, will use 127.0.0.1 instead.");
return localAddress;
}
/**
* get address
* @return
*/
private static InetAddress getAddress() {
if (LOCAL_ADDRESS != null)
return LOCAL_ADDRESS;
InetAddress localAddress = getFirstValidAddress();
LOCAL_ADDRESS = localAddress;
return localAddress;
}
/**
* get ip
* @return
*/
public static String getIp(){
InetAddress address = getAddress();
if (address==null) {
return null;
}
return address.getHostAddress(); return address.getHostAddress();
} }
}
} /**
logger.info("xxl job getHostAddress fail"); * get ip:port
return null; * @param port
} catch (Throwable t) { * @return
logger.error("xxl job getHostAddress error, {}", t); */
public static String getIpPort(int port){
String ip = getIp();
if (ip==null) {
return null; return null;
} }
return ip.concat(":").concat(String.valueOf(port));
} }
public static void main(String[] args) throws UnknownHostException { public static void main(String[] args) throws UnknownHostException {
System.out.println(InetAddress.getLocalHost().getCanonicalHostName());
System.out.println(InetAddress.getLocalHost().getHostName());
System.out.println(getIp()); System.out.println(getIp());
System.out.println(getIpPort(8080));
} }
} }