Skip to content

Commit

Permalink
Polish apache#3984 : Add Nacos implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jun 7, 2019
1 parent f8d029b commit 41c25bb
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 14 deletions.
23 changes: 23 additions & 0 deletions dubbo-bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
</dependency>

<!-- Test dependencies -->

<!-- Zookeeper dependencies for testing -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
Expand All @@ -39,6 +41,27 @@
<scope>test</scope>
</dependency>

<!-- Nacos dependencies for testing -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-nacos</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) throws Exception {
.application("dubbo-consumer-demo")
.next()
.registry()
.address("zookeeper://127.0.0.1:2181?registry-type=service")
.address("nacos://127.0.0.1:8848?registry-type=service&subscribed-services=dubbo-provider-demo")
.next()
.reference("ref")
.interfaceClass(EchoService.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) throws IOException {
.application("dubbo-provider-demo")
.next()
.registry()
.address("zookeeper://127.0.0.1:2181?registry-type=service")
.address("nacos://127.0.0.1:8848?registry-type=service")
.next()
.protocol()
.name("dubbo")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.common.utils;

import org.junit.jupiter.api.Test;

import java.util.List;

import static java.util.Arrays.asList;

/**
* {@link DefaultPage}
*
* @since 2.7.3
*/
public class DefaultPageTest {

@Test
public void test() {
List<Integer> data = asList(1, 2, 3, 4, 5);
DefaultPage page = new DefaultPage(0, 1, data.subList(0, 1), data.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
import static org.apache.dubbo.common.utils.CollectionUtils.isEmpty;
import static org.apache.dubbo.common.utils.CollectionUtils.isNotEmpty;
import static org.apache.dubbo.common.utils.StringUtils.isBlank;
import static org.apache.dubbo.common.utils.StringUtils.split;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getExportedServicesRevision;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceURLsParams;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getProviderHost;
Expand Down Expand Up @@ -105,7 +104,7 @@ public ServiceOrientedRegistry(URL registryURL) {
private Set<String> buildSubscribedServices(URL url) {
String subscribedServiceNames = url.getParameter(SUBSCRIBED_SERVICE_NAMES_KEY);
return isBlank(subscribedServiceNames) ? emptySet() :
unmodifiableSet(of(split(subscribedServiceNames, ','))
unmodifiableSet(of(subscribedServiceNames.split(","))
.map(String::trim)
.filter(StringUtils::isNotEmpty)
.collect(toSet()));
Expand Down Expand Up @@ -228,13 +227,15 @@ protected void subscribeURLs(URL subscribedURL, NotifyListener listener, String
listener.notify(subscribedURLs);
}


private List<URL> getSubscribedURLs(URL subscribedURL, Collection<ServiceInstance> instances) {

List<URL> subscribedURLs = new LinkedList<>();

// local service instances could be mutable
List<ServiceInstance> serviceInstances = new ArrayList<>(instances);
List<ServiceInstance> serviceInstances = instances.stream()
.filter(ServiceInstance::isEnabled)
.filter(ServiceInstance::isHealthy)
.collect(Collectors.toList());

/**
* A caches all revisions of exported services in different {@link ServiceInstance}s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.registry.client.ServiceDiscovery;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.event.ServiceInstancesChangedEvent;
import org.apache.dubbo.registry.client.event.listener.ServiceInstancesChangedListener;
import org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils;

import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;

import java.util.Collections;
import java.util.HashSet;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.dubbo.common.function.ThrowableConsumer.execute;
import static org.apache.dubbo.registry.nacos.util.NacosNamingServiceUtils.createNamingService;
Expand All @@ -48,23 +51,25 @@ public class NacosServiceDiscovery implements ServiceDiscovery {

private final Logger logger = LoggerFactory.getLogger(getClass());

private final NamingService namingService;
private final URL connectionURL;

private final String group;

private NamingService namingService;

public NacosServiceDiscovery(URL connectionURL) {
this.namingService = createNamingService(connectionURL);
this.connectionURL = connectionURL;
this.group = getGroup(connectionURL);
}

@Override
public void start() {
// DO NOTHING
this.namingService = createNamingService(connectionURL);
}

@Override
public void stop() {
// DO NOTHING
this.namingService = null;
}

@Override
Expand Down Expand Up @@ -99,13 +104,33 @@ public Set<String> getServices() {
}

@Override
public List<ServiceInstance> getInstances(String serviceName) throws NullPointerException {
return Collections.emptyList();
public List<ServiceInstance> getInstances(String serviceName) throws NullPointerException {
return ThrowableFunction.execute(namingService, service ->
service.selectInstances(serviceName, true)
.stream().map(NacosNamingServiceUtils::toServiceInstance)
.collect(Collectors.toList())
);
}

@Override
public void addServiceInstancesChangedListener(String serviceName, ServiceInstancesChangedListener listener)
throws NullPointerException, IllegalArgumentException {
execute(namingService, service -> {
service.subscribe(serviceName, e -> { // Register Nacos EventListener
if (e instanceof NamingEvent) {
NamingEvent event = (NamingEvent) e;
handleEvent(event, listener);
}
});
});
}

private void handleEvent(NamingEvent event, ServiceInstancesChangedListener listener) {
String serviceName = event.getServiceName();
Collection<ServiceInstance> serviceInstances = event.getInstances()
.stream()
.map(NacosNamingServiceUtils::toServiceInstance)
.collect(Collectors.toList());
listener.onEvent(new ServiceInstancesChangedEvent(serviceName, serviceInstances));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.registry.client.DefaultServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstance;

import com.alibaba.nacos.api.NacosFactory;
Expand Down Expand Up @@ -65,6 +66,22 @@ public static Instance toInstance(ServiceInstance serviceInstance) {
return instance;
}

/**
* Convert the {@link Instance} to {@link ServiceInstance}
*
* @param instance {@link Instance}
* @return non-null
* @since 2.7.3
*/
public static ServiceInstance toServiceInstance(Instance instance) {
DefaultServiceInstance serviceInstance = new DefaultServiceInstance(instance.getInstanceId(),
instance.getServiceName(), instance.getIp(), instance.getPort());
serviceInstance.setMetadata(instance.getMetadata());
serviceInstance.setEnabled(instance.isEnabled());
serviceInstance.setHealthy(instance.isHealthy());
return serviceInstance;
}

/**
* The group of {@link NamingService} to register
*
Expand Down

0 comments on commit 41c25bb

Please sign in to comment.