Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCB-986] Cache instances result in CseDiscoveryClient #990

Merged
merged 5 commits into from Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions spring-boot-starter/spring-boot-starter-discovery/pom.xml
Expand Up @@ -75,5 +75,9 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>service-registry</artifactId>
</dependency>
<dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need this new dependency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DiscoveryFilter interface is defined in this package

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no , you add new core dependence

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's for Const.RESTFUL

<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-core</artifactId>
</dependency>
</dependencies>
</project>
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;

import org.apache.servicecomb.core.Const;
import org.apache.servicecomb.foundation.common.cache.VersionedCache;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
Expand All @@ -29,12 +30,18 @@
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

public class CseDiscoveryClient implements DiscoveryClient {
private static final Logger LOGGER = LoggerFactory.getLogger(CseDiscoveryClient.class);

private Map<String, DiscoveryTree> discoveryTrees = new ConcurrentHashMapEx<>();

@Override
Expand All @@ -44,24 +51,55 @@ public String description() {

@Override
public List<ServiceInstance> getInstances(final String serviceId) {
class InstanceDiscoveryFilter implements DiscoveryFilter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put class InstanceDiscoveryFilter in "getInstances", make "getInstances" logic not clear

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will move to a separate class

@Override
public int getOrder() {
return Short.MAX_VALUE;
}

@Override
public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
return parent.children()
.computeIfAbsent(context.getInputParameters(), etn -> createDiscoveryTreeNode(context, parent));
}

@SuppressWarnings("unchecked")
protected DiscoveryTreeNode createDiscoveryTreeNode(DiscoveryContext context,
DiscoveryTreeNode parent) {
String serviceName = context.getInputParameters();
List<ServiceInstance> instances = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not align?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix

for (MicroserviceInstance instance : ((Map<String, MicroserviceInstance>) parent.data()).values()) {
for (String endpoint : instance.getEndpoints()) {
String scheme = endpoint.split(":", 2)[0];
if (!scheme.equalsIgnoreCase(Const.RESTFUL)) {
LOGGER.info("Endpoint {} is not supported in Spring Cloud, ignoring.", endpoint);
continue;
}
URIEndpointObject uri = new URIEndpointObject(endpoint);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zuul can not support highway
so we need to log and ignore highway endpoints

instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return new DiscoveryTreeNode()
.subName(parent, serviceName)
.data(instances);
}
};

DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);

DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId, key -> {
return new DiscoveryTree();
DiscoveryTree tree = new DiscoveryTree();
tree.addFilter(new InstanceDiscoveryFilter());
return tree;
});

VersionedCache serversVersionedCache = discoveryTree.discovery(context,
RegistryUtils.getAppId(),
serviceId,
DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<ServiceInstance> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return instances;

return serversVersionedCache.data();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened when serversVersionedCache is empty?

Copy link
Member Author

@yangbor yangbor Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK for the versionedCache to be empty. As it will not be NULL. When there are associated filters and the children data is null, an Exception will be raised.

}

@Override
Expand Down
Expand Up @@ -24,9 +24,8 @@
import org.apache.servicecomb.serviceregistry.RegistryUtils;
import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
import org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager;
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -39,9 +38,8 @@
public class TestCseDiscoveryClient {
@Test
public void testCseDiscoveryClient(@Mocked RegistryUtils registryUtils,
@Injectable ServiceRegistryClient serviceRegistryClient,
@Mocked DiscoveryTree discoveryTree,
@Injectable DiscoveryTreeNode versionedCache) {
@Injectable InstanceCacheManager instanceCacheManager,
@Injectable ServiceRegistryClient serviceRegistryClient) {
List<Microservice> microserviceList = new ArrayList<>();
Microservice service1 = new Microservice();
service1.setServiceName("service1");
Expand All @@ -50,26 +48,24 @@ public void testCseDiscoveryClient(@Mocked RegistryUtils registryUtils,
microserviceList.add(server2);
server2.setServiceName("server2");

Map<String, MicroserviceInstance> servers = new HashMap<>();

List<String> endpoints = new ArrayList<>();
endpoints.add("rest://localhost:3333");
endpoints.add("rest://localhost:4444");
MicroserviceInstance instance1 = new MicroserviceInstance();
instance1.setServiceId("service1");
instance1.setInstanceId("service1-instance1");
instance1.setEndpoints(endpoints);
servers.put("service1-instance1", instance1);

Map<String, MicroserviceInstance> data = new HashMap<>();
data.put("service1-instance1", instance1);
DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
new Expectations() {
{
RegistryUtils.getServiceRegistryClient();
result = serviceRegistryClient;
instanceCacheManager.getOrCreateVersionedCache(anyString, anyString, anyString);
result = parent;
serviceRegistryClient.getAllMicroservices();
result = microserviceList;
discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, anyString);
result = versionedCache;
versionedCache.data();
result = servers;
}
};

Expand Down
Expand Up @@ -90,6 +90,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>service-registry</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot2-starter</artifactId>
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;

import org.apache.servicecomb.core.Const;
import org.apache.servicecomb.foundation.common.cache.VersionedCache;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
Expand All @@ -29,12 +30,18 @@
import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient;
import org.apache.servicecomb.serviceregistry.definition.DefinitionConst;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

public class CseDiscoveryClient implements DiscoveryClient {
private static final Logger LOGGER = LoggerFactory.getLogger(CseDiscoveryClient.class);

private Map<String, DiscoveryTree> discoveryTrees = new ConcurrentHashMapEx<>();

@Override
Expand All @@ -44,24 +51,55 @@ public String description() {

@Override
public List<ServiceInstance> getInstances(final String serviceId) {
class InstanceDiscoveryFilter implements DiscoveryFilter {
@Override
public int getOrder() {
return Short.MAX_VALUE;
}

@Override
public DiscoveryTreeNode discovery(DiscoveryContext context, DiscoveryTreeNode parent) {
return parent.children()
.computeIfAbsent(context.getInputParameters(), etn -> createDiscoveryTreeNode(context, parent));
}

@SuppressWarnings("unchecked")
protected DiscoveryTreeNode createDiscoveryTreeNode(DiscoveryContext context,
DiscoveryTreeNode parent) {
String serviceName = context.getInputParameters();
List<ServiceInstance> instances = new ArrayList<>();
for (MicroserviceInstance instance : ((Map<String, MicroserviceInstance>) parent.data()).values()) {
for (String endpoint : instance.getEndpoints()) {
String scheme = endpoint.split(":", 2)[0];
if (!scheme.equalsIgnoreCase(Const.RESTFUL)) {
LOGGER.info("Endpoint {} is not supported in Spring Cloud, ignoring.", endpoint);
continue;
}
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return new DiscoveryTreeNode()
.subName(parent, serviceName)
.data(instances);
}
};

DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);

DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId, key -> {
return new DiscoveryTree();
DiscoveryTree tree = new DiscoveryTree();
tree.addFilter(new InstanceDiscoveryFilter());
return tree;
});

VersionedCache serversVersionedCache = discoveryTree.discovery(context,
RegistryUtils.getAppId(),
serviceId,
DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<ServiceInstance> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return instances;

return serversVersionedCache.data();
}

@Override
Expand Down