diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java index d051717d8d..8aabf5f5d9 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/IpUtils.java @@ -12,6 +12,7 @@ import com.epmet.commons.tools.constant.StrConstant; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.cloud.commons.util.InetUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -127,12 +128,12 @@ public class IpUtils { * 遍历本地网卡的方式,在云平台虚拟机取不到真正的ip * @return */ - InetAddress localAddress = InetAddress.getLocalHost(); - if (isValidAddress(localAddress)) { - return localAddress.getHostAddress(); + InetUtils inetUtils = SpringContextUtils.getBean(InetUtils.class); + if (inetUtils == null || inetUtils.findFirstNonLoopbackHostInfo() == null) { + logger.warn("Could not get local host ip address"); + } else { + SERVER_IP = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); } - logger.warn("Could not get local host ip address"); - return null; } catch (Exception e) { logger.error("IpUtils getServerIp exception ", e); }