mirror of
https://gitee.com/wangkang/udf.git
synced 2026-11-04 20:23:49 +00:00
更新
This commit is contained in:
+1
-1
@@ -96,7 +96,7 @@ public class DbCacheServiceRegistration implements IServiceRegistration {
|
||||
public void init() throws UnknownHostException, JsonProcessingException {
|
||||
//获得IP和端口
|
||||
this.applicationName = env.getProperty("spring.application.name");
|
||||
this.ip = CoreUtil.getIpAddress();
|
||||
this.ip = CoreUtil.getLocalHostLANAddress().getHostAddress();
|
||||
this.port = env.getProperty("server.port");
|
||||
this.managementPort = env.getProperty("management.server.port");
|
||||
//放入缓存
|
||||
|
||||
+31
-18
@@ -21,9 +21,9 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import java.io.*;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@@ -457,32 +457,45 @@ public class CoreUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得IP地址
|
||||
* 获得局域网IP地址
|
||||
*
|
||||
* @return String
|
||||
* @return InetAddress
|
||||
* @throws UnknownHostException
|
||||
*/
|
||||
public static String getIpAddress() {
|
||||
public static InetAddress getLocalHostLANAddress() throws UnknownHostException {
|
||||
try {
|
||||
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
|
||||
InetAddress ip = null;
|
||||
while (allNetInterfaces.hasMoreElements()) {
|
||||
NetworkInterface netInterface = allNetInterfaces.nextElement();
|
||||
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
|
||||
continue;
|
||||
} else {
|
||||
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
ip = addresses.nextElement();
|
||||
if (ip != null && ip instanceof Inet4Address) {
|
||||
return ip.getHostAddress();
|
||||
InetAddress candidateAddress = null;
|
||||
// 遍历所有的网络接口
|
||||
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
|
||||
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
||||
// 在所有的接口下再遍历IP
|
||||
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
|
||||
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
|
||||
if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
|
||||
if (inetAddr.isSiteLocalAddress()) {
|
||||
// 如果是site-local地址,就是它了
|
||||
return inetAddr;
|
||||
} else if (candidateAddress == null) {
|
||||
// site-local类型的地址未被发现,先记录候选地址
|
||||
candidateAddress = inetAddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (candidateAddress != null) {
|
||||
return candidateAddress;
|
||||
}
|
||||
// 如果没有发现 non-loopback地址.只能用最次选的方案
|
||||
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
|
||||
if (jdkSuppliedAddress == null) {
|
||||
throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
|
||||
}
|
||||
return jdkSuppliedAddress;
|
||||
} catch (Exception e) {
|
||||
throw new SystemRuntimeException(e);
|
||||
UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e);
|
||||
unknownHostException.initCause(e);
|
||||
throw unknownHostException;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user