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.3

Operating System version: Centos 7

Java version: 1.8

Spring Boot version: 2.1.7.RELEASE

Spring Cloud version: Greenwich.SR5

Nacos version: 1.3.1

Nacos集群、Dubbo生产者、Dubbo消费者均部署在K8s环境中

Steps to reproduce this issue

正常启动Nacos集群、生产者、消费者

滚动更新生产者服务，确保新IP已注册，老的IP已执行Unregister

查看消费者日志，出现异常。但是此时可以正常调用生产者服务

Expected Result

消费者可以正常切换到生产者新的IP

Actual Result

正常启动Nacos集群、生产者、消费者

滚动更新生产者服务，确保新IP已注册，老的IP已执行Unregister

新的IP已注册

老的IP已执行Unregister

查看消费者日志，发现报错如下

几分钟后，会循环打印下面截图所示错误日志

If there is an exception, please attach the exception trace:

2020-07-17 14:48:09,908 [com.alibaba.nacos.naming.client.listener] ERROR [c.a.c.d.s.DubboMetadataServi
ceInvocationHandler] DubboMetadataServiceInvocationHandler.java:59 - [failed 5 times] Failed to invoke
the method getExportedURLs in the service org.apache.dubbo.rpc.service.GenericService. Tried 3 times
of the providers [172.30.2.17:20880] (1/1) from the registry localhost:9090 on the consumer 172.30.3.1
59 using the dubbo version 2.7.3. Last error is: Failed to invoke remote method: $invoke, provider: du
bbo://172.30.2.17:20880/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=
com-petkit-message-service&bind.ip=172.30.2.17&bind.port=20880&check=false&deprecated=false&dubbo=2.0.
2&dynamic=true&generic=true&group=com-petkit-sms-service&interface=com.alibaba.cloud.dubbo.service.Dub
boMetadataService&lazy=false&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllEx
portedURLs&pid=6&qos.enable=false&register=true&register.ip=172.30.3.159&release=2.7.3&remote.applicat
ion=com-petkit-sms-service&revision=2.1.1.RELEASE&side=consumer&sticky=false&timestamp=1594901114548&v
ersion=1.0.0, cause: message can not send, because channel is closed . url:dubbo://172.30.2.17:20880/c
om.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=com-petkit-message-servic
e&bind.ip=172.30.2.17&bind.port=20880&check=false&codec=dubbo&deprecated=false&dubbo=2.0.2&dynamic=tru
e&generic=true&group=com-petkit-sms-service&heartbeat=60000&interface=com.alibaba.cloud.dubbo.service.
DubboMetadataService&lazy=false&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAl
lExportedURLs&pid=6&qos.enable=false&register=true&register.ip=172.30.3.159&release=2.7.3&remote.appli
cation=com-petkit-sms-service&revision=2.1.1.RELEASE&side=consumer&sticky=false&timestamp=159490111454
8&version=1.0.0
org.apache.dubbo.rpc.RpcException: Failed to invoke the method getExportedURLs in the service org.apac
he.dubbo.rpc.service.GenericService. Tried 3 times of the providers [172.30.2.17:20880] (1/1) from the
registry localhost:9090 on the consumer 172.30.3.159 using the dubbo version 2.7.3. Last error is: Fa
iled to invoke remote method: $invoke, provider: dubbo://172.30.2.17:20880/com.alibaba.cloud.dubbo.ser
vice.DubboMetadataService?anyhost=true&application=com-petkit-message-service&bind.ip=172.30.2.17&bind
.port=20880&check=false&deprecated=false&dubbo=2.0.2&dynamic=true&generic=true&group=com-petkit-sms-se
rvice&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&lazy=false&methods=getAllServiceK
eys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=6&qos.enable=false&register=true&reg
ister.ip=172.30.3.159&release=2.7.3&remote.application=com-petkit-sms-service&revision=2.1.1.RELEASE&s
ide=consumer&sticky=false&timestamp=1594901114548&version=1.0.0, cause: message can not send, because
channel is closed . url:dubbo://172.30.2.17:20880/com.alibaba.cloud.dubbo.service.DubboMetadataService
?anyhost=true&application=com-petkit-message-service&bind.ip=172.30.2.17&bind.port=20880&check=false&c
odec=dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=true&group=com-petkit-sms-service&heartbe
at=60000&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&lazy=false&methods=getAllServi
ceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=6&qos.enable=false&register=true&
register.ip=172.30.3.159&release=2.7.3&remote.application=com-petkit-sms-service&revision=2.1.1.RELEAS
E&side=consumer&sticky=false&timestamp=1594901114548&version=1.0.0
at org.apache.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:113)
at org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:248)
at org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:78)
at org.apache.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:55)
at org.apache.dubbo.common.bytecode.proxy6.$invoke(proxy6.java)
at com.alibaba.cloud.dubbo.service.DubboMetadataServiceInvocationHandler.invoke(DubboMetadataServiceInvocationHandler.java:54)
at com.sun.proxy.$Proxy138.getExportedURLs(Unknown Source)
at com.alibaba.cloud.dubbo.registry.AbstractSpringCloudRegistry.getExportedURLs(AbstractSpringCloudRegistry.java:338)
at com.alibaba.cloud.dubbo.registry.AbstractSpringCloudRegistry.subscribeDubboServiceURL(AbstractSpringCloudRegistry.java:272)
at com.alibaba.cloud.dubbo.registry.AbstractSpringCloudRegistry$1.onApplicationEvent(AbstractSpringCloudRegistry.java:195)
at com.alibaba.cloud.dubbo.registry.AbstractSpringCloudRegistry$1.onApplicationEvent(AbstractSpringCloudRegistry.java:188)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359)
at com.alibaba.cloud.dubbo.autoconfigure.DubboServiceDiscoveryAutoConfiguration.dispatchServiceInstancesChangedEvent(DubboServiceDiscoveryAutoConfiguration.java:168)
at com.alibaba.cloud.dubbo.autoconfigure.DubboServiceDiscoveryAutoConfiguration.access$200(DubboServiceDiscoveryAutoConfiguration.java:105)
at com.alibaba.cloud.dubbo.autoconfigure.DubboServiceDiscoveryAutoConfiguration$NacosConfiguration.lambda$subscribeEventListener$1(DubboServiceDiscoveryAutoConfiguration.java:551)
at com.alibaba.nacos.client.naming.core.EventDispatcher$Notifier.run(EventDispatcher.java:128)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.dubbo.remoting.RemotingException: message can not send, because channel is closed . url:dubbo://172.30.2.17:20880/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=com-petkit-message-service&bind.ip=172.30.2.17&bind.port=20880&check=false&codec=dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=true&group=com-petkit-sms-service&heartbeat=60000&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&lazy=false&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=6&qos.enable=false&register=true&register.ip=172.30.3.159&release=2.7.3&remote.application=com-petkit-sms-service&revision=2.1.1.RELEASE&side=consumer&sticky=false&timestamp=1594901114548&version=1.0.0
at org.apache.dubbo.remoting.transport.AbstractClient.send(AbstractClient.java:175)
at org.apache.dubbo.remoting.transport.AbstractPeer.send(AbstractPeer.java:53)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeChannel.request(HeaderExchangeChannel.java:118)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeClient.request(HeaderExchangeClient.java:84)
at org.apache.dubbo.rpc.protocol.dubbo.ReferenceCountExchangeClient.request(ReferenceCountExchangeClient.java:80)
at org.apache.dubbo.rpc.protocol.dubbo.DubboInvoker.doInvoke(DubboInvoker.java:97)
at org.apache.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:155)
at org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker.invoke(AsyncToSyncInvoker.java:52)
at org.apache.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:78)
at org.apache.dubbo.rpc.filter.GenericImplFilter.invoke(GenericImplFilter.java:119)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:82)
at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:92)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:82)
at org.apache.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:54)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:82)
at org.apache.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:58)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:82)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$CallbackRegistrationInvoker.invoke(ProtocolFilterWrapper.java:157)
at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
at org.apache.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:82)
... 22 common frames omitted
2020-07-17 14:48:42,077 [dubbo-client-idleCheck-thread-1] INFO [o.a.d.r.exchange.support.header.ReconnectTimerTask] ReconnectTimerTask.java:48 - [DUBBO] Initial connection to HeaderExchangeClient [channel=org.apache.dubbo.remoting.transport.netty4.NettyClient [172.30.3.159:0 -> /172.30.2.17:20880]], dubbo version: 2.7.3, current host: 172.30.3.159
2020-07-17 14:48:45,084 [dubbo-client-idleCheck-thread-1] ERROR [o.a.d.r.exchange.support.header.ReconnectTimerTask] ReconnectTimerTask.java:51 - [DUBBO] Fail to connect to HeaderExchangeClient [channel=org.apache.dubbo.remoting.transport.netty4.NettyClient [172.30.3.159:0 -> /172.30.2.17:20880]], dubbo version: 2.7.3, current host: 172.30.3.159
org.apache.dubbo.remoting.RemotingException: client(url: dubbo://172.30.2.17:20880/com.alibaba.cloud.dubbo.service.DubboMetadataService?anyhost=true&application=com-petkit-message-service&bind.ip=172.30.2.17&bind.port=20880&check=false&codec=dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=true&group=com-petkit-sms-service&heartbeat=60000&interface=com.alibaba.cloud.dubbo.service.DubboMetadataService&lazy=false&methods=getAllServiceKeys,getServiceRestMetadata,getExportedURLs,getAllExportedURLs&pid=6&qos.enable=false&register=true&register.ip=172.30.3.159&release=2.7.3&remote.application=com-petkit-sms-service&revision=2.1.1.RELEASE&side=consumer&sticky=false&timestamp=1594901114548&version=1.0.0) failed to connect to server /172.30.2.17:20880 client-side timeout 3000ms (elapsed: 3002ms) from netty client 172.30.3.159 using dubbo version 2.7.3
at org.apache.dubbo.remoting.transport.netty4.NettyClient.doConnect(NettyClient.java:171)
at org.apache.dubbo.remoting.transport.AbstractClient.connect(AbstractClient.java:190)
at org.apache.dubbo.remoting.transport.AbstractClient.reconnect(AbstractClient.java:246)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeClient.reconnect(HeaderExchangeClient.java:155)
at org.apache.dubbo.remoting.exchange.support.header.ReconnectTimerTask.doTask(ReconnectTimerTask.java:49)
at org.apache.dubbo.remoting.exchange.support.header.AbstractTimerTask.run(AbstractTimerTask.java:87)
at org.apache.dubbo.common.timer.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:648)
at org.apache.dubbo.common.timer.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:727)
at org.apache.dubbo.common.timer.HashedWheelTimer$Worker.run(HashedWheelTimer.java:449)
at java.lang.Thread.run(Thread.java:748)

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

Operating System version: windows、ubuntu

Java version: 11

Spring Boot version：2.2.6.RELEASE

Spring Cloud version：Hoxton.RELEASE

Spring Cloud Alibaba version：2.2.1.RELEASE

Steps to reproduce this issue

如果现在有2个consumer，2个provider，如图：

现打算对部分接口进行不兼容升级：

3.升级之后，根据版本号匹配的原则，consumer1应该只会refer到provider1，原来的consumer2只会refer到provider2。

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

Expected Result

What do you expected from the above steps？

consumer1应该只会refer到provider1，原来的consumer2只会refer到provider2。

Actual Result

What actually happens?

从源码来看，服务引入的过程似乎和版本没有关系。

1、在AbstractSpringCloudRegistry.subscribeDubboServiceURL方法中，有这样一段：

List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url);
for (URL exportedURL : exportedURLs) {
String protocol = exportedURL.getProtocol();
List<URL> subscribedURLs = new LinkedList<>();
serviceInstances.forEach(serviceInstance -> {
Integer port = repository.getDubboProtocolPort(serviceInstance, protocol);
String host = serviceInstance.getHost();
if (port == null) {
if (logger.isWarnEnabled()) {
logger.warn(
"The protocol[{}] port of Dubbo service instance[host : {}] "
+ "can't be resolved",
protocol, host);
}
}
else {
URL subscribedURL = new URL(protocol, host, port,
exportedURL.getParameters());
subscribedURLs.add(subscribedURL);
}
});

allSubscribedURLs.addAll(subscribedURLs);
}

可以看出，该段代码从dubboMetadataService获取exportedURLs，然后遍历所有的provider serviceInstance，生成最终的allSubscribedURLs。

然后跟踪dubboMetadataService的代码，在DubboServiceMetadataRepository.initSubscribedDubboMetadataService中有这样一段：

Optional<ServiceInstance> optionalServiceInstance = metadataServiceInstanceSelector
.choose(discoveryClient.getInstances(serviceName));
if (!((Optional) optionalServiceInstance).isPresent()) {
return false;
}
ServiceInstance serviceInstance = optionalServiceInstance.get();
if (null == serviceInstance) {
return false;
}

这里仅仅是从discoveryClient.getInstances(serviceName)返回的provider实例中随机选取了一个（stream.findAny()）实例，然后用来作为获取dubboMetadataServiceURL的实例。这样看的话，consumer要根据版本号调用匹配版本号的provider似乎并不能实现，而在实际使用中也发现，在以下场景下：

如果consumer1在服务引用时，选择的dubboMetadataService是provider1时，最终consumer1引用的invokers会包含provider1和provider2，导致调用会有50%的几率成功。

如果consumer1在服务引用时，选择的dubboMetadataService是provider2时，最终consumer1引用的invokers是空的，导致调用会100%失败。