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 4 commits
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
5 changes: 5 additions & 0 deletions java-chassis-dependencies/pom.xml
Expand Up @@ -1040,6 +1040,11 @@
<artifactId>metrics-prometheus</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-common</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
37 changes: 37 additions & 0 deletions java-chassis-spring-boot/pom.xml
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-chassis</artifactId>
<groupId>org.apache.servicecomb</groupId>
<version>1.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>java-chassis-spring-boot</artifactId>

<modules>
<module>spring-boot-common</module>
<module>spring-boot-starter</module>
<module>spring-boot2-starter-parent</module>
</modules>

</project>
55 changes: 55 additions & 0 deletions java-chassis-spring-boot/spring-boot-common/pom.xml
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-chassis-parent</artifactId>
<groupId>org.apache.servicecomb</groupId>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../../parent</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-common</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>service-registry</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,71 @@
/*
* 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.servicecomb.springboot.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.servicecomb.core.Const;
Copy link
Contributor

Choose a reason for hiding this comment

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

remove java-chassis-core dependency
define a new const "rest" or just hard code is enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter;
import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractInstanceDiscoveryFilter implements DiscoveryFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInstanceDiscoveryFilter.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<Object> 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(createInstance(serviceName, uri));
}
}

return new DiscoveryTreeNode()
.subName(parent, serviceName)
.data(instances);
}

abstract protected Object createInstance(String name, URIEndpointObject uri);
};
Expand Up @@ -14,57 +14,49 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.servicecomb.springboot2.starter.discovery;
package org.apache.servicecomb.springboot.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.servicecomb.foundation.common.cache.VersionedCache;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
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.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.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

public class CseDiscoveryClient implements DiscoveryClient {
public class DiscoveryClientUtil<T> {

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

@Override
public String description() {
return "Spring Cloud CSE Discovery Client";
public DiscoveryClientUtil(DiscoveryFilter filter){
this.filter = filter;
}

@Override
public List<ServiceInstance> getInstances(final String serviceId) {
public List<T> getInstances(final String serviceId) {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);

DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId, key -> {
return new DiscoveryTree();
DiscoveryTree tree = new DiscoveryTree();
tree.addFilter(filter);
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
public List<String> getServices() {
ServiceRegistryClient client = RegistryUtils.getServiceRegistryClient();
List<Microservice> services = client.getAllMicroservices();
Expand Down
Expand Up @@ -22,7 +22,7 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
<relativePath>../../parent</relativePath>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
<name>Java Chassis::Spring Boot Starter::Parent</name>
Expand Down
Expand Up @@ -75,5 +75,13 @@
<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-boot-common</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,48 @@
/*
* 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.servicecomb.springboot.starter.discovery;

import java.util.List;

import org.apache.servicecomb.springboot.common.DiscoveryClientUtil;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

public class CseDiscoveryClient implements DiscoveryClient {
private DiscoveryClientUtil<ServiceInstance>
discoveryClientUtil = new DiscoveryClientUtil<>(new InstanceDiscoveryFilter());

@Override
public String description() {
return "Spring Cloud CSE Discovery Client";
}

@Override
public List<ServiceInstance> getInstances(final String serviceId) {
return discoveryClientUtil.getInstances(serviceId);
}

@Deprecated
public ServiceInstance getLocalServiceInstance() {
return null;
}

@Override
public List<String> getServices() {
return discoveryClientUtil.getServices();
}
}
@@ -0,0 +1,12 @@
package org.apache.servicecomb.springboot.starter.discovery;

import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
import org.apache.servicecomb.springboot.common.AbstractInstanceDiscoveryFilter;
import org.springframework.cloud.client.DefaultServiceInstance;

class InstanceDiscoveryFilter extends AbstractInstanceDiscoveryFilter {
@Override
protected Object createInstance(String name, URIEndpointObject uri) {
return new DefaultServiceInstance(name, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled());
}
}
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