This is an automated email from the ASF dual-hosted git repository. wujimin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 7052c7d4429422f215c4648be65b4d94e97a185d Author: bao.liu <[email protected]> AuthorDate: Sat Jan 6 09:18:54 2018 +0800 [SCB-184]Provide starters for Spring Cloud to using service-center and config-center --- .../src/test/resources/application.yml | 11 ++- .../spring-boot-starter-discovery/pom.xml | 38 +-------- .../starter/discovery/CseDiscoveryClient.java | 40 ++++----- .../discovery/CseDiscoveryClientConfiguration.java | 20 +---- .../starter/discovery/CseDiscoveryProperties.java | 48 ----------- .../discovery/CseRibbonClientConfiguration.java | 96 ++++++++++------------ ...figuration.java => CseRibbonConfiguration.java} | 70 ++++++++-------- .../starter/discovery/CseRoutesProperties.java | 42 ---------- .../discovery/CseServerListCacheWrapper.java | 36 -------- .../starter/discovery/CseServerWrapper.java | 40 --------- .../discovery/ServiceCombDiscoveryException.java | 26 ------ .../starter/discovery/ServiceCombServerList.java | 64 +++++++-------- .../src/main/resources/META-INF/spring.factories | 6 +- .../discovery/ServiceCombServerListTest.java | 62 -------------- .../starter/discovery/TestCseDiscoveryClient.java | 68 +++++++++++++++ .../discovery/TestServiceCombServerList.java | 59 +++++++++++++ 16 files changed, 265 insertions(+), 461 deletions(-) diff --git a/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/resources/application.yml b/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/resources/application.yml index 2d867a7..ef24bce 100644 --- a/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/resources/application.yml +++ b/demo/demo-spring-boot-discovery/demo-spring-boot-zuul-proxy/src/test/resources/application.yml @@ -17,16 +17,15 @@ server: port: 0 -spring: - cloud: - cse: - host: 127.0.0.1 - port: 9980 + zuul: routes: gateway: serviceId: discoveryServer - +discoveryServer: + ribbon: + eureka: + enabled: false hystrix: command: default: diff --git a/spring-boot-starter/spring-boot-starter-discovery/pom.xml b/spring-boot-starter/spring-boot-starter-discovery/pom.xml index 7a3030f..df6c9ff 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/pom.xml +++ b/spring-boot-starter/spring-boot-starter-discovery/pom.xml @@ -27,10 +27,6 @@ <dependencies> <dependency> - <groupId>org.apache.tomcat.embed</groupId> - <artifactId>tomcat-embed-logging-juli</artifactId> - </dependency> - <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> @@ -76,39 +72,7 @@ </dependency> <dependency> <groupId>io.servicecomb</groupId> - <artifactId>common-rest</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>java-chassis-core</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>provider-pojo</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>provider-rest-common</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>provider-jaxrs</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>provider-springmvc</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>transport-rest-client</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>transport-rest-servlet</artifactId> - </dependency> - <dependency> - <groupId>io.servicecomb</groupId> - <artifactId>handler-loadbalance</artifactId> + <artifactId>service-registry</artifactId> </dependency> </dependencies> </project> diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClient.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClient.java index 9a63eda..0af0274 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClient.java +++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClient.java @@ -18,25 +18,24 @@ package io.servicecomb.springboot.starter.discovery; import java.util.ArrayList; import java.util.List; - -import javax.inject.Inject; +import java.util.Map; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; -import io.servicecomb.core.provider.consumer.ConsumerProviderManager; -import io.servicecomb.core.provider.consumer.ReferenceConfig; +import io.servicecomb.foundation.common.cache.VersionedCache; import io.servicecomb.foundation.common.net.URIEndpointObject; import io.servicecomb.serviceregistry.RegistryUtils; import io.servicecomb.serviceregistry.api.registry.Microservice; import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance; import io.servicecomb.serviceregistry.client.ServiceRegistryClient; +import io.servicecomb.serviceregistry.definition.DefinitionConst; +import io.servicecomb.serviceregistry.discovery.DiscoveryContext; +import io.servicecomb.serviceregistry.discovery.DiscoveryTree; public class CseDiscoveryClient implements DiscoveryClient { - - @Inject - private ConsumerProviderManager consumerProviderManager; + private DiscoveryTree discoveryTree = new DiscoveryTree(); @Override public String description() { @@ -45,21 +44,18 @@ public class CseDiscoveryClient implements DiscoveryClient { @Override public List<ServiceInstance> getInstances(final String serviceId) { - List<ServiceInstance> instances = new ArrayList<>(); - ServiceRegistryClient client = RegistryUtils.getServiceRegistryClient(); - String appId = RegistryUtils.getAppId(); - ReferenceConfig referenceConfig = consumerProviderManager.getReferenceConfig(serviceId); - String versionRule = referenceConfig.getMicroserviceVersionRule(); - String cseServiceID = client.getMicroserviceId(appId, serviceId, versionRule); - List<MicroserviceInstance> cseServices = client.getMicroserviceInstance(cseServiceID, cseServiceID); - if (null != cseServices && !cseServices.isEmpty()) { - for (MicroserviceInstance instance : cseServices) { - List<String> eps = instance.getEndpoints(); - for (String ep : eps) { - URIEndpointObject uri = new URIEndpointObject(ep); - instances.add(new DefaultServiceInstance(instance.getServiceId(), uri.getHostOrIp(), - uri.getPort(), false)); - } + DiscoveryContext context = new DiscoveryContext(); + context.setInputParameters(serviceId); + 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; diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClientConfiguration.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClientConfiguration.java index e8768b0..19b38b7 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClientConfiguration.java +++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryClientConfiguration.java @@ -17,33 +17,17 @@ package io.servicecomb.springboot.starter.discovery; import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration; -import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; - -import io.servicecomb.core.provider.consumer.ConsumerProviderManager; +import org.springframework.core.annotation.Order; @AutoConfigureBefore(NoopDiscoveryClientAutoConfiguration.class) @Configuration public class CseDiscoveryClientConfiguration { - - @Bean - public CseDiscoveryProperties cseDiscoveryProperties() { - return new CseDiscoveryProperties(); - } - - @Bean - @ConditionalOnBean(ZuulProperties.class) - public CseRoutesProperties cseRoutesProperties(ConsumerProviderManager manager) { - return new CseRoutesProperties(manager); - } - @Bean - @Primary + @Order(5000) public DiscoveryClient cseDiscoveryClient() { return new CseDiscoveryClient(); } diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryProperties.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryProperties.java deleted file mode 100644 index 96773f8..0000000 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseDiscoveryProperties.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties("spring.cloud.cse") -public class CseDiscoveryProperties { - - /** Hostname to use when accessing server */ - private String host; - - /** Port to register the service under (defaults to listening port) */ - private String port; - - public CseDiscoveryProperties() { - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } - - public String getPort() { - return port; - } - - public void setPort(String port) { - this.port = port; - } -} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/ribbon/io/servicecomb/springboot/starter/discovery/CseRibbonClientConfiguration.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRibbonClientConfiguration.java similarity index 80% rename from spring-boot-starter/spring-boot-starter-discovery/src/main/java/ribbon/io/servicecomb/springboot/starter/discovery/CseRibbonClientConfiguration.java rename to spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRibbonClientConfiguration.java index cffa70c..fabb086 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/ribbon/io/servicecomb/springboot/starter/discovery/CseRibbonClientConfiguration.java +++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRibbonClientConfiguration.java @@ -1,51 +1,45 @@ -/* - * 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 ribbon.io.servicecomb.springboot.starter.discovery; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; - -import io.servicecomb.springboot.starter.discovery.CseRoutesProperties; -import io.servicecomb.springboot.starter.discovery.ServiceCombServerList; - -/** - * Custom {@link org.springframework.cloud.netflix.ribbon.RibbonClient} configuration must not be - * scanned by spring. - * - * @see <a href="http://cloud.spring.io/spring-cloud-static/Camden.SR4/#_customizing_the_ribbon_client"> - * Customizing the Ribbon Client </a> - */ -@Configuration -@ConditionalOnBean(ZuulProperties.class) -public class CseRibbonClientConfiguration { - - @Bean - ServerList<Server> ribbonServerList( - IClientConfig config, - CseRoutesProperties cseRoutesProperties) { - - ServiceCombServerList serverList = new ServiceCombServerList(cseRoutesProperties); - serverList.initWithNiwsConfig(config); - return serverList; - } -} +/* + * 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 io.servicecomb.springboot.starter.discovery; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.cloud.netflix.ribbon.RibbonClientConfiguration; +import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.Server; +import com.netflix.loadbalancer.ServerList; + +/** + * Custom {@link org.springframework.cloud.netflix.ribbon.RibbonClient} configuration must not be + * scanned by spring. + * + * @see <a href="http://cloud.spring.io/spring-cloud-static/Camden.SR4/#_customizing_the_ribbon_client"> + * Customizing the Ribbon Client </a> + */ +public class CseRibbonClientConfiguration { + @Bean + public ServerList<Server> ribbonServerList( + IClientConfig config) { + ServiceCombServerList serverList = new ServiceCombServerList(); + serverList.initWithNiwsConfig(config); + return serverList; + } +} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/RibbonCseAutoConfiguration.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRibbonConfiguration.java similarity index 92% rename from spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/RibbonCseAutoConfiguration.java rename to spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRibbonConfiguration.java index ba92c70..deb06d1 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/RibbonCseAutoConfiguration.java +++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRibbonConfiguration.java @@ -1,36 +1,34 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; -import org.springframework.cloud.netflix.ribbon.RibbonClients; -import org.springframework.cloud.netflix.ribbon.SpringClientFactory; -import org.springframework.context.annotation.Configuration; - -import ribbon.io.servicecomb.springboot.starter.discovery.CseRibbonClientConfiguration; - -@Configuration -@EnableConfigurationProperties -@ConditionalOnBean(SpringClientFactory.class) -@AutoConfigureAfter(RibbonAutoConfiguration.class) -@RibbonClients(defaultConfiguration = CseRibbonClientConfiguration.class) -public class RibbonCseAutoConfiguration { - -} +/* + * 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 io.servicecomb.springboot.starter.discovery; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; +import org.springframework.cloud.netflix.ribbon.RibbonClients; +import org.springframework.cloud.netflix.ribbon.SpringClientFactory; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties +@ConditionalOnBean(SpringClientFactory.class) +@AutoConfigureAfter(RibbonAutoConfiguration.class) +@RibbonClients(defaultConfiguration = CseRibbonClientConfiguration.class) +public class CseRibbonConfiguration { + +} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRoutesProperties.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRoutesProperties.java deleted file mode 100644 index 15db9b2..0000000 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseRoutesProperties.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -import org.springframework.beans.factory.annotation.Autowired; - -import io.servicecomb.core.provider.consumer.ConsumerProviderManager; -import io.servicecomb.core.provider.consumer.ReferenceConfig; -import io.servicecomb.serviceregistry.RegistryUtils; - -public final class CseRoutesProperties { - - private final ConsumerProviderManager consumerProviderManager; - - @Autowired - CseRoutesProperties(ConsumerProviderManager consumerProviderManager) { - this.consumerProviderManager = consumerProviderManager; - } - - String getVersionRule(String serviceName) { - ReferenceConfig referenceConfig = consumerProviderManager.getReferenceConfig(serviceName); - return referenceConfig.getMicroserviceVersionRule(); - } - - String getAppID() { - return RegistryUtils.getAppId(); - } -} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseServerListCacheWrapper.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseServerListCacheWrapper.java deleted file mode 100644 index 73f2c9d..0000000 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseServerListCacheWrapper.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -import com.netflix.loadbalancer.Server; - -import io.servicecomb.core.Transport; -import io.servicecomb.loadbalance.ServerListCache; -import io.servicecomb.serviceregistry.cache.CacheEndpoint; - -public class CseServerListCacheWrapper extends ServerListCache { - - public CseServerListCacheWrapper(String appId, String microserviceName, String microserviceVersionRule, - String transportName) { - super(appId, microserviceName, microserviceVersionRule, transportName); - } - - @Override - protected Server createEndpoint(Transport transport, CacheEndpoint cacheEndpoint) { - return new CseServerWrapper(transport, cacheEndpoint); - } -} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseServerWrapper.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseServerWrapper.java deleted file mode 100644 index b3d7c6c..0000000 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/CseServerWrapper.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -import io.servicecomb.core.Transport; -import io.servicecomb.foundation.common.net.URIEndpointObject; -import io.servicecomb.loadbalance.CseServer; -import io.servicecomb.serviceregistry.cache.CacheEndpoint; - -public class CseServerWrapper extends CseServer { - - public CseServerWrapper(Transport transport, CacheEndpoint cacheEndpoint) { - super(transport, cacheEndpoint); - } - - // used in LoadBalancerContext - public String getHost() { - URIEndpointObject host = (URIEndpointObject) getEndpoint().getAddress(); - return host.getHostOrIp(); - } - - public int getPort() { - URIEndpointObject host = (URIEndpointObject) getEndpoint().getAddress(); - return host.getPort(); - } -} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombDiscoveryException.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombDiscoveryException.java deleted file mode 100644 index 4848513..0000000 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombDiscoveryException.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -public class ServiceCombDiscoveryException extends RuntimeException { - private static final long serialVersionUID = 3806741463767943277L; - - public ServiceCombDiscoveryException(String message) { - super(message); - } -} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerList.java b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerList.java index 74f0ba5..5edb8e5 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerList.java +++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerList.java @@ -16,65 +16,57 @@ */ package io.servicecomb.springboot.starter.discovery; +import java.util.ArrayList; import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import java.util.Map; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; import com.netflix.loadbalancer.Server; -import io.servicecomb.loadbalance.ServerListCache; +import io.servicecomb.foundation.common.cache.VersionedCache; +import io.servicecomb.foundation.common.net.URIEndpointObject; +import io.servicecomb.serviceregistry.RegistryUtils; +import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance; +import io.servicecomb.serviceregistry.definition.DefinitionConst; +import io.servicecomb.serviceregistry.discovery.DiscoveryContext; +import io.servicecomb.serviceregistry.discovery.DiscoveryTree; public class ServiceCombServerList extends AbstractServerList<Server> { - private static final Logger logger = LoggerFactory.getLogger(ServiceCombServerList.class); - - private final CseRoutesProperties config; - - private ServerListCache serverListCache; + private DiscoveryTree discoveryTree = new DiscoveryTree(); private String serviceId; - public ServiceCombServerList(CseRoutesProperties config) { - this.config = config; + public ServiceCombServerList() { } @Override public List<Server> getInitialListOfServers() { - return servers(); + DiscoveryContext context = new DiscoveryContext(); + context.setInputParameters(serviceId); + VersionedCache serversVersionedCache = discoveryTree.discovery(context, + RegistryUtils.getAppId(), + serviceId, + DefinitionConst.VERSION_RULE_ALL); + Map<String, MicroserviceInstance> servers = serversVersionedCache.data(); + List<Server> instances = new ArrayList<>(servers.size()); + for (MicroserviceInstance s : servers.values()) { + for (String endpoint : s.getEndpoints()) { + URIEndpointObject uri = new URIEndpointObject(endpoint); + instances.add(new Server(uri.getHostOrIp(), uri.getPort())); + } + } + return instances; } @Override public List<Server> getUpdatedListOfServers() { - return servers(); - } - - private List<Server> servers() { - if (serverListCache == null) { - throw new ServiceCombDiscoveryException("Service list is not initialized"); - } - - logger.info("Looking for service with app id: {}, service id: {}, version rule: {}", - config.getAppID(), - serviceId, - config.getVersionRule(serviceId)); - - List<Server> endpoints = serverListCache.getLatestEndpoints(); - - logger.info("Found service endpoints {}", endpoints); - return endpoints; + return getInitialListOfServers(); } @Override public void initWithNiwsConfig(IClientConfig iClientConfig) { - serviceId = iClientConfig.getClientName(); - - serverListCache = new CseServerListCacheWrapper( - config.getAppID(), - serviceId, - config.getVersionRule(serviceId), - "rest"); + this.serviceId = iClientConfig.getClientName(); } } diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/main/resources/META-INF/spring.factories b/spring-boot-starter/spring-boot-starter-discovery/src/main/resources/META-INF/spring.factories index 6f7488c..f0f5612 100644 --- a/spring-boot-starter/spring-boot-starter-discovery/src/main/resources/META-INF/spring.factories +++ b/spring-boot-starter/spring-boot-starter-discovery/src/main/resources/META-INF/spring.factories @@ -17,4 +17,8 @@ io.servicecomb.springboot.starter.provider.EnableServiceComb=\ io.servicecomb.springboot.starter.discovery.CseDiscoveryClientConfiguration,\ - io.servicecomb.springboot.starter.discovery.RibbonCseAutoConfiguration + io.servicecomb.springboot.starter.discovery.CseRibbonConfiguration +org.springframework.cloud.client.discovery.EnableDiscoveryClient=\ + io.servicecomb.springboot.starter.discovery.CseDiscoveryClientConfiguration +org.springframework.cloud.netflix.ribbon.RibbonClient=\ + io.servicecomb.springboot.starter.discovery.CseRibbonConfiguration \ No newline at end of file diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerListTest.java b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerListTest.java deleted file mode 100644 index 47c9650..0000000 --- a/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/ServiceCombServerListTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 io.servicecomb.springboot.starter.discovery; - -import static com.seanyinx.github.unit.scaffolding.AssertUtils.expectFailing; -import static io.servicecomb.core.Const.DEFAULT_VERSION_RULE; -import static org.hamcrest.core.Is.is; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.seanyinx.github.unit.scaffolding.Randomness; - -import io.servicecomb.core.provider.consumer.ConsumerProviderManager; -import io.servicecomb.core.provider.consumer.ReferenceConfig; - -public class ServiceCombServerListTest { - - private final ReferenceConfig referenceConfig = mock(ReferenceConfig.class); - - private final ConsumerProviderManager manager = mock(ConsumerProviderManager.class); - - private final CseRoutesProperties properties = new CseRoutesProperties(manager); - - private final ServiceCombServerList serverList = new ServiceCombServerList(properties); - - private String serviceId = Randomness.uniquify("serviceId"); - - @Before - public void setUp() throws Exception { - when(manager.getReferenceConfig(serviceId)).thenReturn(referenceConfig); - when(referenceConfig.getMicroserviceVersionRule()).thenReturn(DEFAULT_VERSION_RULE); - } - - @Test - public void blowsUpWhenServerListNotInitialized() { - try { - serverList.getInitialListOfServers(); - expectFailing(ServiceCombDiscoveryException.class); - } catch (ServiceCombDiscoveryException e) { - Assert.assertThat(e.getMessage(), is("Service list is not initialized")); - } - } -} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/TestCseDiscoveryClient.java b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/TestCseDiscoveryClient.java new file mode 100644 index 0000000..511ee0b --- /dev/null +++ b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/TestCseDiscoveryClient.java @@ -0,0 +1,68 @@ +package io.servicecomb.springboot.starter.discovery; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.cloud.client.discovery.DiscoveryClient; + +import io.servicecomb.serviceregistry.RegistryUtils; +import io.servicecomb.serviceregistry.api.registry.Microservice; +import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance; +import io.servicecomb.serviceregistry.client.ServiceRegistryClient; +import io.servicecomb.serviceregistry.discovery.DiscoveryContext; +import io.servicecomb.serviceregistry.discovery.DiscoveryTree; +import io.servicecomb.serviceregistry.discovery.DiscoveryTreeNode; +import mockit.Expectations; +import mockit.Injectable; +import mockit.Mocked; + +public class TestCseDiscoveryClient { + @Test + public void testCseDiscoveryClient(@Mocked RegistryUtils registryUtils, + @Injectable ServiceRegistryClient serviceRegistryClient, + @Mocked DiscoveryTree discoveryTree, + @Injectable DiscoveryTreeNode versionedCache) { + List<Microservice> microserviceList = new ArrayList<>(); + Microservice service1 = new Microservice(); + service1.setServiceName("service1"); + microserviceList.add(service1); + Microservice server2 = new Microservice(); + 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); + + new Expectations() { + { + RegistryUtils.getServiceRegistryClient(); + result = serviceRegistryClient; + serviceRegistryClient.getAllMicroservices(); + result = microserviceList; + discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, anyString); + result = versionedCache; + versionedCache.data(); + result = servers; + } + }; + + DiscoveryClient client = new CseDiscoveryClient(); + Assert.assertEquals("Spring Cloud CSE Discovery Client", client.description()); + Assert.assertEquals(null, client.getLocalServiceInstance()); + Assert.assertEquals(2, client.getServices().size()); + Assert.assertEquals("server2", client.getServices().get(1)); + Assert.assertEquals(2, client.getInstances("service1-instance1").size()); + Assert.assertEquals(4444, client.getInstances("service1-instance1").get(1).getPort()); + } +} diff --git a/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java new file mode 100644 index 0000000..a1eb68e --- /dev/null +++ b/spring-boot-starter/spring-boot-starter-discovery/src/test/java/io/servicecomb/springboot/starter/discovery/TestServiceCombServerList.java @@ -0,0 +1,59 @@ +package io.servicecomb.springboot.starter.discovery; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.Server; + +import io.servicecomb.serviceregistry.RegistryUtils; +import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance; +import io.servicecomb.serviceregistry.discovery.DiscoveryContext; +import io.servicecomb.serviceregistry.discovery.DiscoveryTree; +import io.servicecomb.serviceregistry.discovery.DiscoveryTreeNode; +import mockit.Expectations; +import mockit.Injectable; +import mockit.Mocked; + +public class TestServiceCombServerList { + @Test + public void testServiceCombServerList(@Injectable IClientConfig iClientConfig, + @Mocked RegistryUtils registryUtils, + @Mocked DiscoveryTree discoveryTree, + @Injectable DiscoveryTreeNode versionedCache) { + 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); + + new Expectations() { + { + iClientConfig.getClientName(); + result = "serviceId1"; + + RegistryUtils.getAppId(); + result = "app"; + discoveryTree.discovery((DiscoveryContext) any, anyString, anyString, anyString); + result = versionedCache; + versionedCache.data(); + result = servers; + } + }; + + ServiceCombServerList list = new ServiceCombServerList(); + list.initWithNiwsConfig(iClientConfig); + List<Server> serverList = list.getInitialListOfServers(); + Assert.assertEquals(2, serverList.size()); + Assert.assertEquals(4444, serverList.get(1).getPort()); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
