You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "nevin9939 (GitHub)" <gi...@apache.org> on 2019/04/03 05:55:06 UTC

[GitHub] [incubator-dubbo] nevin9939 opened issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

### Environment

* Dubbo version: 2.7.1
* Operating System version: xxx
* Java version: 1.8.181

### Steps to reproduce this issue

1. 服务都是部署在阿里云的ECS上
2. hosts中修改机器名执行本机的内网IP
3. dubbo注册到zookeeper的服务IP是外网IP
4. 原整个环境的dubbo服务均提供内网服务,禁止外网的服务和请求进入,造成服务调不通

查看源代码发现在 request #3520 中调整为优先获取外网IP,造成了当机器同时存在内网IP和外网IP时,期望只提供内网IP服务时就无法做到,个人认为最好有个配置开关,有用户决定是提供内网服务还是外网服务



[ 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

[GitHub] [incubator-dubbo] nevin9939 commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "nevin9939 (GitHub)" <gi...@apache.org>.
@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

[GitHub] [incubator-dubbo] nevin9939 commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "nevin9939 (GitHub)" <gi...@apache.org>.
@lexburner 
我也写完了 (*T_T*) , 看了下我跟你的区别主要在首选网卡的选择,跟操作系统同步,这样可以防止优先获取到虚拟机和蓝牙桥接网卡,你可以加进去
```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

[GitHub] [incubator-dubbo] lexburner commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "lexburner (GitHub)" <gi...@apache.org>.
@nevin9939 I send a pull request, could you pls review it and try it to see whether meet your requirement?  Thanks.

[ 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


[GitHub] [incubator-dubbo] nevin9939 commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "nevin9939 (GitHub)" <gi...@apache.org>.
I can try it, not a lot of spare time.

[ 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


[GitHub] [incubator-dubbo] ralf0131 commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "ralf0131 (GitHub)" <gi...@apache.org>.
+1 to you proposal, would you like to send a pull request to fix this?

[ 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


[GitHub] [incubator-dubbo] lexburner commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "lexburner (GitHub)" <gi...@apache.org>.
@nevin9939 Dubbo Ecosystem encourages contributor to commit their code since I am a committer, I can review your change, could send a pr for these changes? 

[ 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


[GitHub] [incubator-dubbo] nevin9939 commented on issue #3802: 当机器同时存在内网和外网IP时,无法发布内网IP服务

Posted by "nevin9939 (GitHub)" <gi...@apache.org>.
I also finished coding  (*T_T*) . Looked at the difference between me and you mainly in the choice of preferred network card, synchronized with the operating system, this can prevent priority access to the virtual machine and Bluetooth bridge network card. 
```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