dubbo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nevin9939 (GitHub) <git...@apache.org>
Subject [GitHub] [incubator-dubbo] nevin9939 commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务
Date Wed, 10 Apr 2019 09:15:45 GMT
@lexburner 
我也写完了 (*T_T*) , 看了下我跟你的区别主要在首选网卡的选择,跟操作系统同步,这样可以防止优先获取到虚拟机和蓝牙桥接网卡,你可以加进去。还有,我认为默认值应该是false,因为照原来的逻辑,获取的都是内网IP,这样用户就不会受升级的变化而困扰,去添加额外的配置
```java
    private static InetAddress getLocalAddress0() {
        InetAddress candidateAddress = null;
        int interfacesIndex = 0;
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            if (null != interfaces) {
                while (interfaces.hasMoreElements()) {
                    try {
                        NetworkInterface network = interfaces.nextElement();
                        // Remove loopback interface, subinterface, not running interface
                        if (network.isLoopback() || network.isVirtual() || !network.isUp())
{
                            continue;
                        }
                        Enumeration<InetAddress> addresses = network.getInetAddresses();
                        while (addresses.hasMoreElements()) {
                            try {
                                Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
                                if (addressOp.isPresent()) {
                                    InetAddress tempAddress = addressOp.get();
                                    if (candidateAddress == null || interfacesIndex > network.getIndex())
{
                                        // Priority is given to the network card with a small
index value,
                                        // which can exclude the IP of the self-built virtual
machine.
                                        candidateAddress = tempAddress;
                                        interfacesIndex = network.getIndex();
                                    }
                                }
                            } catch (Throwable e) {
                                logger.warn(e);
                            }
                        }
                    } catch (Throwable e) {
                        logger.warn(e);
                    }
                }
            }
            if(candidateAddress == null){
                InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
                if (jdkSuppliedAddress == null) {
                    throw new UnknownHostException("The JDK InetAddress.getLocalHost() method
unexpectedly returned null.");
                }
                Optional<InetAddress> addressOp = toValidAddress(jdkSuppliedAddress);
                if (addressOp.isPresent()) {
                    candidateAddress = addressOp.get();
                }
            }
        } catch (Throwable e) {
            logger.warn(e);
        }
        return candidateAddress;
    }
```

[ Full content available at: https://github.com/apache/incubator-dubbo/issues/3802 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org
Mime
View raw message