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 all 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>
51 changes: 51 additions & 0 deletions java-chassis-spring-boot/spring-boot-common/pom.xml
@@ -0,0 +1,51 @@
<?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>
</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>
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 abstract class AbstractDiscoveryClient {

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

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

@Override
public List<ServiceInstance> getInstances(final String serviceId) {
public <T> List<T> doGetInstances(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
@@ -0,0 +1,78 @@
/*
* 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.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 class InstanceDiscoveryFilter implements DiscoveryFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(InstanceDiscoveryFilter.class);

public interface InstanceFactory {
Object createInstance(String name, URIEndpointObject uri);
}

InstanceFactory instanceFactory;

public InstanceDiscoveryFilter(InstanceFactory factory){
instanceFactory = factory;
}

@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("rest")) {
LOGGER.info("Endpoint {} is not supported in Spring Cloud, ignoring.", endpoint);
continue;
}
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(instanceFactory.createInstance(serviceName, uri));
}
}

return new DiscoveryTreeNode()
.subName(parent, serviceName)
.data(instances);
}
};
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,9 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>service-registry</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>spring-boot-common</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,49 @@
/*
* 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.foundation.common.net.URIEndpointObject;
import org.apache.servicecomb.springboot.common.AbstractDiscoveryClient;
import org.apache.servicecomb.springboot.common.InstanceDiscoveryFilter;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

public class ScbDiscoveryClient extends AbstractDiscoveryClient implements DiscoveryClient {

public ScbDiscoveryClient() {
super(new InstanceDiscoveryFilter((String name, URIEndpointObject uri) ->
new DefaultServiceInstance(name, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled())));
}

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

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

public List<ServiceInstance> getInstances(String serviceId) {
return super.doGetInstances(serviceId);
}
}
Expand Up @@ -26,11 +26,11 @@

@AutoConfigureBefore(SimpleDiscoveryClientAutoConfiguration.class)
@Configuration
public class CseDiscoveryClientConfiguration {
public class ScbDiscoveryClientConfiguration {
@Bean
@Order(5000)
@ConditionalOnProperty(value = "servicecomb.discoveryClient.enabled", havingValue = "true", matchIfMissing = true)
public DiscoveryClient cseDiscoveryClient() {
return new CseDiscoveryClient();
return new ScbDiscoveryClient();
}
}
Expand Up @@ -29,7 +29,7 @@
* @see <a href="http://cloud.spring.io/spring-cloud-static/Camden.SR4/#_customizing_the_ribbon_client">
* Customizing the Ribbon Client </a>
*/
public class CseRibbonClientConfiguration {
public class ScbRibbonClientConfiguration {
@Bean
public ServerList<Server> ribbonServerList(
IClientConfig config) {
Expand Down
Expand Up @@ -28,7 +28,7 @@
@EnableConfigurationProperties
@ConditionalOnBean(SpringClientFactory.class)
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = CseRibbonClientConfiguration.class)
public class CseRibbonConfiguration {
@RibbonClients(defaultConfiguration = ScbRibbonClientConfiguration.class)
public class ScbRibbonConfiguration {

}
Expand Up @@ -25,7 +25,7 @@

import com.netflix.loadbalancer.Server;

public class CseRibbonEndpointDiscoveryFilter extends AbstractEndpointDiscoveryFilter {
public class ScbRibbonEndpointDiscoveryFilter extends AbstractEndpointDiscoveryFilter {
@Override
protected String findTransportName(DiscoveryContext context, DiscoveryTreeNode parent) {
//only need rest endpoints
Expand Down
Expand Up @@ -35,7 +35,7 @@ public class ServiceCombServerList extends AbstractServerList<Server> {
private String serviceId;

public ServiceCombServerList() {
discoveryTree.addFilter(new CseRibbonEndpointDiscoveryFilter());
discoveryTree.addFilter(new ScbRibbonEndpointDiscoveryFilter());
}

@Override
Expand Down
Expand Up @@ -16,9 +16,9 @@
## ---------------------------------------------------------------------------

org.apache.servicecomb.springboot.starter.provider.EnableServiceComb=\
org.apache.servicecomb.springboot.starter.discovery.CseDiscoveryClientConfiguration,\
org.apache.servicecomb.springboot.starter.discovery.CseRibbonConfiguration
org.apache.servicecomb.springboot.starter.discovery.ScbDiscoveryClientConfiguration,\
org.apache.servicecomb.springboot.starter.discovery.ScbRibbonConfiguration
org.springframework.cloud.client.discovery.EnableDiscoveryClient=\
org.apache.servicecomb.springboot.starter.discovery.CseDiscoveryClientConfiguration
org.apache.servicecomb.springboot.starter.discovery.ScbDiscoveryClientConfiguration
org.springframework.cloud.netflix.ribbon.RibbonClient=\
org.apache.servicecomb.springboot.starter.discovery.CseRibbonConfiguration
org.apache.servicecomb.springboot.starter.discovery.ScbRibbonConfiguration