Issue 1:
[√] I have searched the issues of this repository and believe that this is not a duplicate.

[√] I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

Dubbo version: 2.7.6-2.7.8

Operating System version: Centos 3.10.0-862.el7.x86_64

Java version: 1.8.0

Steps to reproduce this issue

设置虚IP

有一台机器因为某些原因，在eth0上设置了虚IP（10.19.15.115,真实IP是10.19.15.111）。

执行 ip a输出如下：

...
eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> 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
...

执行 ifconfig 输出如下：

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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)

编写代码

public static void main(String[] args) {
InetAddress localAddress = NetUtils.getLocalAddress();
System.out.println(localAddress);
}

运行程序

Pls. provide [GitHub address] to reproduce this issue.

Expected Result

/10.19.15.111

Actual Result

What actually happens?

/10.19.15.115

此问题在2.7.5以及之前是能够获取期望的正确的IP的。

因此我认为 #5795 引入了一个 Breakable change：让Netutils没有机会调用java.net.InetAddress#getLocalHost()从hostname获取对应的IP地址：

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()

例如改成如下方式？（2.7.5以及以前是先调用InetAddress.getLocalHost()的）

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<InetAddress> 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<InetAddress> addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
Optional<InetAddress> 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;
}

Issue 2:
I have searched the issues of this repository and believe that this is not a duplicate.

I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

Dubbo version: 2.7.6-SNAPSHOT(aliyun maven镜像，也用源码的 2.7.6.release分支测试过)

Operating System version: win10

Java version: 1.8.0_171

Steps to reproduce this issue

测试代码：https://github.com/apache/dubbo-samples/tree/master/java/dubbo-samples-nacos/dubbo-samples-nacos-registry

dubbo版本修改为"2.7.6.SNAPSHOT"

Pls. provide [GitHub address] to reproduce this issue.

Expected Result

Actual Result

(IP地址手动改成了 127.0.0.1)

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&dubbo=2.0.2&init=false&interface=org.apache.dubbo.samples.api.GreetingService&methods=sayHello&pid=19028&register.ip=127.0.0.1&release=2.7.6-SNAPSHOT&revision=1.0.0&side=consumer&sticky=false&timeout=3000&timestamp=1584584637337&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.<init>(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&dubbo=2.0.2&init=false&interface=org.apache.dubbo.samples.api.GreetingService&methods=sayHello&pid=19028&register.ip=127.0.0.1&release=2.7.6-SNAPSHOT&revision=1.0.0&side=consumer&sticky=false&timeout=3000&timestamp=1584584637337&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

备注

nacos中已存在 provider注册的服务 serviceName = "providers:org.apache.dubbo.samples.api.GreetingService:1.0.0:"

启动consumer时，NacosRegistry#doSubscribe(...)为了兼容会创建2个 serviceNames

providers:org.apache.dubbo.samples.api.GreetingService:1.0.0:

providers:org.apache.dubbo.samples.api.GreetingService:1.0.0

并且进行了 NacosRegistry#subscribeEventListener(...) 监听和订阅 nacos。

但是因为 provider 其实并不存在 providers:org.apache.dubbo.samples.api.GreetingService:1.0.0，

所以（nacos 回调的 instances = 0）NacosRegistry#notifySubscriber(URL , NotifyListener , Collection<Instance> )中会创建一个 empty 的 protocol。

又因为只有1个provider，所以RegistryDirectory#refreshInvoker()中满足了 forbidden 的条件，所以invokers被destroy....