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

v1.5
xueli.xue 8 years ago
parent e229a824ea
commit c86d7d1c8e

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

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

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

@ -7,6 +7,7 @@ import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.regex.Pattern;
/**
* get ip
@ -15,36 +16,112 @@ import java.util.Enumeration;
public class IpUtil {
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;
/**
* valid address
* @param address
* @return
*/
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());
}
/**
* ip
* @return ip
* get first valid addredd
* @return
*/
public static String getIp() {
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 {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
InetAddress address = null;
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
address = addresses.nextElement();
if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) {
return address.getHostAddress();
if (interfaces != null) {
while (interfaces.hasMoreElements()) {
try {
NetworkInterface network = interfaces.nextElement();
Enumeration<InetAddress> addresses = network.getInetAddresses();
if (addresses != null) {
while (addresses.hasMoreElements()) {
try {
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);
}
}
}
logger.info("xxl job getHostAddress fail");
} 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;
} catch (Throwable t) {
logger.error("xxl job getHostAddress error, {}", t);
}
return address.getHostAddress();
}
/**
* get ip:port
* @param port
* @return
*/
public static String getIpPort(int port){
String ip = getIp();
if (ip==null) {
return null;
}
return ip.concat(":").concat(String.valueOf(port));
}
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(getIpPort(8080));
}
}

Loading…
Cancel
Save