<ul dir="auto">
<li>[√] I have searched the <a href="https://github.com/apache/dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li>[√] I have checked the <a href="https://github.com/apache/dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: 2.7.6-2.7.8</li>
<li>Operating System version: Centos 3.10.0-862.el7.x86_64</li>
<li>Java version: 1.8.0</li>
</ul>
<h3 dir="auto">Steps to reproduce this issue</h3>
<ol dir="auto">
<li>设置虚IP<br>
有一台机器因为某些原因，在eth0上设置了虚IP（10.19.15.115,真实IP是10.19.15.111）。</li>
</ol>
<ul dir="auto">
<li>执行 ip a输出如下：</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="...
    eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether xx:xx:xx:xx:dc:cc brd ff:ff:ff:ff:ff:ff
    inet 10.19.15.111/22 brd 10.19.15.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.19.15.115/32 scope global eth0
       valid_lft forever preferred_lft forever
...
"><pre class="notranslate"><code class="notranslate">...
    eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether xx:xx:xx:xx:dc:cc brd ff:ff:ff:ff:ff:ff
    inet 10.19.15.111/22 brd 10.19.15.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.19.15.115/32 scope global eth0
       valid_lft forever preferred_lft forever
...

</code></pre></div>
<ul dir="auto">
<li>执行 ifconfig 输出如下：</li>
</ul>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 10.19.15.111  netmask 255.255.252.0  broadcast 10.19.15.255
        ether xx:xx:xx:xx:dc:cc  txqueuelen 1000  (Ethernet)"><pre class="notranslate"><code class="notranslate">eth0: flags=4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu 1500
        inet 10.19.15.111  netmask 255.255.252.0  broadcast 10.19.15.255
        ether xx:xx:xx:xx:dc:cc  txqueuelen 1000  (Ethernet)
</code></pre></div>
<ol start="2" dir="auto">
<li>编写代码</li>
</ol>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="public static void main(String[] args) {
        InetAddress localAddress = NetUtils.getLocalAddress();
        System.out.println(localAddress);
}"><pre class="notranslate"><code class="notranslate">public static void main(String[] args) {
        InetAddress localAddress = NetUtils.getLocalAddress();
        System.out.println(localAddress);
}
</code></pre></div>
<ol start="3" dir="auto">
<li>运行程序</li>
</ol>
<p dir="auto">Pls. provide [GitHub address] to reproduce this issue.</p>
<h3 dir="auto">Expected Result</h3>
<p dir="auto"><code class="notranslate">/10.19.15.111</code></p>
<h3 dir="auto">Actual Result</h3>
<p dir="auto">What actually happens?</p>
<p dir="auto"><code class="notranslate">/10.19.15.115</code></p>
<p dir="auto">此问题在2.7.5以及之前是能够获取期望的正确的IP的。<br>
因此我认为 <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="570438009" data-permission-text="Title is private" data-url="https://github.com/apache/dubbo/issues/5795" data-hovercard-type="issue" data-hovercard-url="/apache/dubbo/issues/5795/hovercard" href="https://github.com/apache/dubbo/issues/5795">#5795</a> 引入了一个 Breakable change：让Netutils没有机会调用<code class="notranslate">java.net.InetAddress#getLocalHost()</code>从hostname获取对应的IP地址：</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Returns the address of the local host. This is achieved by retrieving
     * the name of the host from the system, then resolving that name into
     * an {@code InetAddress}.
java.net.InetAddress#getLocalHost()"><pre class="notranslate"><code class="notranslate">Returns the address of the local host. This is achieved by retrieving
     * the name of the host from the system, then resolving that name into
     * an {@code InetAddress}.
java.net.InetAddress#getLocalHost()
</code></pre></div>
<p dir="auto">例如改成如下方式？（2.7.5以及以前是先调用<code class="notranslate">InetAddress.getLocalHost()</code>的）</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" private static InetAddress getLocalAddress0() {
        InetAddress localAddress = null;
        
     //to provide a chance to retrieve ip from hostname. maybe add an system property like 'dubbo.network.hostname.first'
     if (&quot;true&quot;.equals(System.getProperty(&quot;dubbo.network.hostname.first&quot;))) {       
        try {
            localAddress = InetAddress.getLocalHost();
            Optional&lt;InetAddress&gt; addressOp = toValidAddress(localAddress);
            if (addressOp.isPresent()) {
                return addressOp.get();
            }
        } catch (Throwable e) {
            logger.warn(e);
        }
      }
     //以上代码放在后面可能没有机会执行了。

        // @since 2.7.6, choose the {@link NetworkInterface} first
        try {
            NetworkInterface networkInterface = findNetworkInterface();
            Enumeration&lt;InetAddress&gt; addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                Optional&lt;InetAddress&gt; addressOp = toValidAddress(addresses.nextElement());
                if (addressOp.isPresent()) {
                    try {
                        if (addressOp.get().isReachable(100)) {
                            return addressOp.get();
                        }
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        } catch (Throwable e) {
            logger.warn(e);
        }
        return localAddress;
    }"><pre class="notranslate"><code class="notranslate"> private static InetAddress getLocalAddress0() {
        InetAddress localAddress = null;
        
     //to provide a chance to retrieve ip from hostname. maybe add an system property like 'dubbo.network.hostname.first'
     if ("true".equals(System.getProperty("dubbo.network.hostname.first"))) {       
        try {
            localAddress = InetAddress.getLocalHost();
            Optional&lt;InetAddress&gt; addressOp = toValidAddress(localAddress);
            if (addressOp.isPresent()) {
                return addressOp.get();
            }
        } catch (Throwable e) {
            logger.warn(e);
        }
      }
     //以上代码放在后面可能没有机会执行了。

        // @since 2.7.6, choose the {@link NetworkInterface} first
        try {
            NetworkInterface networkInterface = findNetworkInterface();
            Enumeration&lt;InetAddress&gt; addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                Optional&lt;InetAddress&gt; addressOp = toValidAddress(addresses.nextElement());
                if (addressOp.isPresent()) {
                    try {
                        if (addressOp.get().isReachable(100)) {
                            return addressOp.get();
                        }
                    } catch (IOException e) {
                        // ignore
                    }
                }
            }
        } catch (Throwable e) {
            logger.warn(e);
        }
        return localAddress;
    }
</code></pre></div>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have searched the <a href="https://github.com/apache/dubbo/issues">issues</a> of this repository and believe that this is not a duplicate.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> I have checked the <a href="https://github.com/apache/dubbo/blob/master/FAQ.md">FAQ</a> of this repository and believe that this is not a duplicate.</li>
</ul>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>Dubbo version: 2.7.6-SNAPSHOT(aliyun maven镜像，也用源码的 2.7.6.release分支测试过)</li>
<li>Operating System version: win10</li>
<li>Java version: 1.8.0_171</li>
</ul>
<h3 dir="auto">Steps to reproduce this issue</h3>
<p dir="auto">测试代码：<a href="https://github.com/apache/dubbo-samples/tree/master/java/dubbo-samples-nacos/dubbo-samples-nacos-registry">https://github.com/apache/dubbo-samples/tree/master/java/dubbo-samples-nacos/dubbo-samples-nacos-registry</a></p>
<p dir="auto">dubbo版本修改为"2.7.6.SNAPSHOT"</p>
<p dir="auto">Pls. provide [GitHub address] to reproduce this issue.</p>
<h3 dir="auto">Expected Result</h3>
<h3 dir="auto">Actual Result</h3>
<p dir="auto">(IP地址手动改成了 127.0.0.1)</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Exception in thread &quot;main&quot; org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annotatedConsumer': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: Failed to check the status of the service org.apache.dubbo.samples.api.GreetingService. No provider available for the service org.apache.dubbo.samples.api.GreetingService:1.0.0 from the url nacos://localhost:8848/org.apache.dubbo.registry.RegistryService?application=nacos-registry-demo-consumer&amp;dubbo=2.0.2&amp;init=false&amp;interface=org.apache.dubbo.samples.api.GreetingService&amp;methods=sayHello&amp;pid=19028&amp;register.ip=127.0.0.1&amp;release=2.7.6-SNAPSHOT&amp;revision=1.0.0&amp;side=consumer&amp;sticky=false&amp;timeout=3000&amp;timestamp=1584584637337&amp;version=1.0.0 to the consumer 127.0.0.1 use dubbo version 2.7.6-SNAPSHOT
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:146)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.&lt;init&gt;(AnnotationConfigApplicationContext.java:84)
	at org.apache.dubbo.samples.ConsumerBootstrap.main(ConsumerBootstrap.java:32)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service org.apache.dubbo.samples.api.GreetingService. No provider available for the service org.apache.dubbo.samples.api.GreetingService:1.0.0 from the url nacos://localhost:8848/org.apache.dubbo.registry.RegistryService?application=nacos-registry-demo-consumer&amp;dubbo=2.0.2&amp;init=false&amp;interface=org.apache.dubbo.samples.api.GreetingService&amp;methods=sayHello&amp;pid=19028&amp;register.ip=127.0.0.1&amp;release=2.7.6-SNAPSHOT&amp;revision=1.0.0&amp;side=consumer&amp;sticky=false&amp;timeout=3000&amp;timestamp=1584584637337&amp;version=1.0.0 to the consumer 127.0.0.1 use dubbo version 2.7.6-SNAPSHOT
	at org.apache.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:349)
	at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:258)
	at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:158)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.getOrCreateProxy(ReferenceAnnotationBeanPostProcessor.java:274)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:143)
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.getInjectedObject(AbstractAnnotationBeanPostProcessor.java:359)
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor$AnnotatedFieldElement.inject(AbstractAnnotationBeanPostProcessor.java:539)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:142)
	... 12 more"><pre class="notranslate"><code class="notranslate">Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'annotatedConsumer': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: Failed to check the status of the service org.apache.dubbo.samples.api.GreetingService. No provider available for the service org.apache.dubbo.samples.api.GreetingService:1.0.0 from the url nacos://localhost:8848/org.apache.dubbo.registry.RegistryService?application=nacos-registry-demo-consumer&amp;dubbo=2.0.2&amp;init=false&amp;interface=org.apache.dubbo.samples.api.GreetingService&amp;methods=sayHello&amp;pid=19028&amp;register.ip=127.0.0.1&amp;release=2.7.6-SNAPSHOT&amp;revision=1.0.0&amp;side=consumer&amp;sticky=false&amp;timeout=3000&amp;timestamp=1584584637337&amp;version=1.0.0 to the consumer 127.0.0.1 use dubbo version 2.7.6-SNAPSHOT
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:146)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.&lt;init&gt;(AnnotationConfigApplicationContext.java:84)
	at org.apache.dubbo.samples.ConsumerBootstrap.main(ConsumerBootstrap.java:32)
Caused by: java.lang.IllegalStateException: Failed to check the status of the service org.apache.dubbo.samples.api.GreetingService. No provider available for the service org.apache.dubbo.samples.api.GreetingService:1.0.0 from the url nacos://localhost:8848/org.apache.dubbo.registry.RegistryService?application=nacos-registry-demo-consumer&amp;dubbo=2.0.2&amp;init=false&amp;interface=org.apache.dubbo.samples.api.GreetingService&amp;methods=sayHello&amp;pid=19028&amp;register.ip=127.0.0.1&amp;release=2.7.6-SNAPSHOT&amp;revision=1.0.0&amp;side=consumer&amp;sticky=false&amp;timeout=3000&amp;timestamp=1584584637337&amp;version=1.0.0 to the consumer 127.0.0.1 use dubbo version 2.7.6-SNAPSHOT
	at org.apache.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:349)
	at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:258)
	at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:158)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.getOrCreateProxy(ReferenceAnnotationBeanPostProcessor.java:274)
	at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:143)
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.getInjectedObject(AbstractAnnotationBeanPostProcessor.java:359)
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor$AnnotatedFieldElement.inject(AbstractAnnotationBeanPostProcessor.java:539)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor.postProcessPropertyValues(AbstractAnnotationBeanPostProcessor.java:142)
	... 12 more
</code></pre></div>
<h3 dir="auto">备注</h3>
<ol dir="auto">
<li>
<p dir="auto">nacos中已存在 provider注册的服务 serviceName = "providers:org.apache.dubbo.samples.api.GreetingService:1.0.0:"</p>
</li>
<li>
<p dir="auto">启动consumer时，<code class="notranslate">NacosRegistry#doSubscribe(...)</code>为了兼容会创建2个 serviceNames</p>
</li>
</ol>
<ul dir="auto">
<li>providers:org.apache.dubbo.samples.api.GreetingService:1.0.0:</li>
<li>providers:org.apache.dubbo.samples.api.GreetingService:1.0.0</li>
</ul>
<p dir="auto">并且进行了  <code class="notranslate">NacosRegistry#subscribeEventListener(...)</code> 监听和订阅 nacos。</p>
<p dir="auto">但是因为 provider 其实并不存在 <code class="notranslate">providers:org.apache.dubbo.samples.api.GreetingService:1.0.0</code>，<br>
所以（nacos 回调的 instances = 0）<code class="notranslate">NacosRegistry#notifySubscriber(URL , NotifyListener , Collection&lt;Instance&gt; )</code>中会创建一个 empty 的 protocol。<br>
又因为只有1个provider，所以<code class="notranslate">RegistryDirectory#refreshInvoker()</code>中满足了 forbidden 的条件，所以invokers被destroy....</p>