add AbstractServiceDiscovery

This commit is contained in:
ken.lj 2020-08-27 15:57:06 +08:00
parent 346166e674
commit fd0e6d80d5
3 changed files with 42 additions and 18 deletions

View File

@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.registry.client;
public abstract class AbstractServiceDiscovery implements ServiceDiscovery {
protected ServiceInstance serviceInstance;
@Override
public ServiceInstance getLocalInstance() {
return serviceInstance;
}
@Override
public void register(ServiceInstance serviceInstance) throws RuntimeException {
}
@Override
public void update(ServiceInstance serviceInstance) throws RuntimeException {
this.serviceInstance = serviceInstance;
}
}

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.function.ThrowableFunction;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.registry.client.AbstractServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
@ -47,7 +48,7 @@ import static org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.toIns
* @see ServiceDiscovery
* @since 2.7.5
*/
public class NacosServiceDiscovery implements ServiceDiscovery {
public class NacosServiceDiscovery extends AbstractServiceDiscovery {
private final Logger logger = LoggerFactory.getLogger(getClass());
@ -57,8 +58,6 @@ public class NacosServiceDiscovery implements ServiceDiscovery {
private URL registryURL;
private ServiceInstance instance;
@Override
public void initialize(URL registryURL) throws Exception {
this.namingService = createNamingService(registryURL);
@ -73,7 +72,7 @@ public class NacosServiceDiscovery implements ServiceDiscovery {
@Override
public void register(ServiceInstance serviceInstance) throws RuntimeException {
this.instance = serviceInstance;
this.serviceInstance = serviceInstance;
execute(namingService, service -> {
Instance instance = toInstance(serviceInstance);
service.registerInstance(instance.getServiceName(), group, instance);
@ -82,7 +81,7 @@ public class NacosServiceDiscovery implements ServiceDiscovery {
@Override
public void update(ServiceInstance serviceInstance) throws RuntimeException {
this.instance = serviceInstance;
this.serviceInstance = serviceInstance;
// TODO: Nacos should support
unregister(serviceInstance);
register(serviceInstance);
@ -137,11 +136,6 @@ public class NacosServiceDiscovery implements ServiceDiscovery {
return registryURL;
}
@Override
public ServiceInstance getLocalInstance() {
return instance;
}
private void handleEvent(NamingEvent event, ServiceInstancesChangedListener listener) {
String serviceName = event.getServiceName();
List<ServiceInstance> serviceInstances = event.getInstances()

View File

@ -24,6 +24,7 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.DefaultPage;
import org.apache.dubbo.common.utils.Page;
import org.apache.dubbo.event.EventDispatcher;
import org.apache.dubbo.registry.client.AbstractServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
@ -51,7 +52,7 @@ import static org.apache.dubbo.registry.zookeeper.util.CuratorFrameworkUtils.bui
* Zookeeper {@link ServiceDiscovery} implementation based on
* <a href="https://curator.apache.org/curator-x-discovery/index.html">Apache Curator X Discovery</a>
*/
public class ZookeeperServiceDiscovery implements ServiceDiscovery {
public class ZookeeperServiceDiscovery extends AbstractServiceDiscovery {
private final Logger logger = LoggerFactory.getLogger(getClass());
@ -65,8 +66,6 @@ public class ZookeeperServiceDiscovery implements ServiceDiscovery {
private org.apache.curator.x.discovery.ServiceDiscovery<ZookeeperInstance> serviceDiscovery;
private ServiceInstance serviceInstance;
/**
* The Key is watched Zookeeper path, the value is an instance of {@link CuratorWatcher}
*/
@ -90,11 +89,6 @@ public class ZookeeperServiceDiscovery implements ServiceDiscovery {
serviceDiscovery.close();
}
@Override
public ServiceInstance getLocalInstance() {
return serviceInstance;
}
public void register(ServiceInstance serviceInstance) throws RuntimeException {
this.serviceInstance = serviceInstance;
doInServiceRegistry(serviceDiscovery -> {