[代码讨论]方法UtilAll.getIP()是否应该返回localhost?

rocketmq/common/src/main/java/org/apache/rocketmq/common/UtilAll.java

Lines 604 to 619 in 7278653

while (allNetInterfaces.hasMoreElements()) { NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement(); Enumeration addresses = netInterface.getInetAddresses(); while (addresses.hasMoreElements()) { ip = (InetAddress) addresses.nextElement(); if (ip != null && ip instanceof Inet4Address) { byte[] ipByte = ip.getAddress(); if (ipByte.length == 4) { if (ipCheck(ipByte)) { if (!isInternalIP(ipByte)) { return ipByte; } else if (internalIP == null) {

这个方法基本上是遍历网络接口并找到一个非专用ip。如果找不到,则返回到它在第一个netInterface中找到的任何ip,在我的设置中,它是localhost(来自lo0)。

它被用来创建唯一的id,所以这不是一个好的行为,对吧? 让局域网ip的优先级高于本地主机怎么样?

原提问者GitHub用户RagingSpud

展开
收起
芬奇福贵 2023-05-26 10:12:27 71 分享 版权
1 条回答
写回答
取消 提交回答
  • 同意,并且当ip为127.0.0.1时,isInternalIP函数应返回true。

    原回答者GitHub用户ShadowySpirits

    2023-05-26 16:40:58
    赞同 展开评论

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

还有其他疑问?
咨询AI助理