bugFix/fix is available timeout (#12796)

* 1、Fix the issue of timeout for all calls to the isAvailable method after the zookeeper is disconnected
2、Move the isAvailable of NopServiceDiscovery into its own implementation class

* 1、Fix the issue of timeout for all calls to the isAvailable method after the zookeeper is disconnected
2、Move the isAvailable of NopServiceDiscovery into its own implementation class

* 1、submit empty after format error

* 1、move destory to implementation class

* 1、update comment description
This commit is contained in:
xieshouyu 2023-07-29 20:12:51 +08:00 committed by GitHub
parent 97227e4e8e
commit 2488b08d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 6 deletions

View File

@ -55,4 +55,10 @@ public class NopServiceDiscovery extends AbstractServiceDiscovery {
public List<ServiceInstance> getInstances(String serviceName) throws NullPointerException {
return null;
}
@Override
public boolean isAvailable() {
// NopServiceDiscovery is designed for compatibility, check availability is meaningless, just return true
return true;
}
}

View File

@ -21,6 +21,7 @@ import java.util.Set;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.lang.Prioritized;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.registry.RegistryService;
import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
@ -94,6 +95,13 @@ public interface ServiceDiscovery extends RegistryService, Prioritized {
return getUrl().getParameter(REGISTRY_DELAY_NOTIFICATION_KEY, 5000);
}
/**
* Get services is the default way for service discovery to be available
*/
default boolean isAvailable() {
return !isDestroy() && CollectionUtils.isNotEmpty(getServices());
}
/**
* A human-readable description of the implementation
*

View File

@ -110,7 +110,7 @@ public class ServiceDiscoveryRegistry extends FailbackRegistry {
*/
protected ServiceDiscovery createServiceDiscovery(URL registryURL) {
return getServiceDiscovery(registryURL.addParameter(INTERFACE_KEY, ServiceDiscovery.class.getName())
.removeParameter(REGISTRY_TYPE_KEY));
.removeParameter(REGISTRY_TYPE_KEY));
}
/**
@ -278,11 +278,8 @@ public class ServiceDiscoveryRegistry extends FailbackRegistry {
@Override
public boolean isAvailable() {
if (serviceDiscovery instanceof NopServiceDiscovery) {
// NopServiceDiscovery is designed for compatibility, check availability is meaningless, just return true
return true;
}
return !serviceDiscovery.isDestroy() && !serviceDiscovery.getServices().isEmpty();
//serviceDiscovery isAvailable has a default method, which can be used as a reference when implementing
return serviceDiscovery.isAvailable();
}
@Override

View File

@ -34,6 +34,7 @@ import org.apache.dubbo.common.function.ThrowableConsumer;
import org.apache.dubbo.common.function.ThrowableFunction;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.registry.client.AbstractServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
@ -179,6 +180,18 @@ public class ZookeeperServiceDiscovery extends AbstractServiceDiscovery {
});
}
@Override
public boolean isAvailable() {
//Fix the issue of timeout for all calls to the isAvailable method after the zookeeper is disconnected
return !isDestroy() && isConnected() && CollectionUtils.isNotEmpty(getServices());
}
private boolean isConnected() {
if (curatorFramework == null || curatorFramework.getZookeeperClient() == null) {
return false;
}
return curatorFramework.getZookeeperClient().isConnected();
}
private void doInServiceRegistry(ThrowableConsumer<org.apache.curator.x.discovery.ServiceDiscovery> consumer) {
ThrowableConsumer.execute(serviceDiscovery, s -> consumer.accept(s));