From 8e9398d24fd22e61faa7edcc26aa1e30c6d05cac Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Tue, 26 Feb 2019 16:24:55 +0800 Subject: [PATCH] Polish : /spring-cloud-incubator/spring-cloud-alibaba#386 : supports Spring Cloud Consul --- spring-cloud-alibaba-dubbo/pom.xml | 79 +++++++++------- .../dubbo/registry/SpringCloudRegistry.java | 80 ++++++++-------- .../consul/ConsulRegistrationFactory.java | 92 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 4 files changed, 181 insertions(+), 73 deletions(-) create mode 100644 spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/hashicorp/consul/ConsulRegistrationFactory.java diff --git a/spring-cloud-alibaba-dubbo/pom.xml b/spring-cloud-alibaba-dubbo/pom.xml index 5dbf37ac2..5d51c191d 100644 --- a/spring-cloud-alibaba-dubbo/pom.xml +++ b/spring-cloud-alibaba-dubbo/pom.xml @@ -18,6 +18,7 @@ 0.2.1.RELEASE 0.0.2 2.1.0.RELEASE + 2.1.0.RELEASE 4.0.1 @@ -55,6 +56,44 @@ true + + org.springframework.boot + spring-boot-actuator-autoconfigure + true + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.springframework.boot + spring-boot + true + + + + org.springframework.boot + spring-boot-autoconfigure + true + + + + + org.springframework.cloud + spring-cloud-commons + true + + + + org.springframework.cloud + spring-cloud-context + true + + + org.springframework.cloud @@ -96,50 +135,22 @@ true - + org.springframework.cloud - spring-cloud-starter-alibaba-nacos-discovery - true - - - - org.springframework.boot - spring-boot-actuator-autoconfigure - true - - - - org.springframework.boot - spring-boot-configuration-processor - true - - - - org.springframework.boot - spring-boot - true - - - - org.springframework.boot - spring-boot-autoconfigure - true - - - - - org.springframework.cloud - spring-cloud-commons + spring-cloud-starter-consul-discovery + ${spring-cloud-consul.version} true + org.springframework.cloud - spring-cloud-context + spring-cloud-starter-alibaba-nacos-discovery true + org.springframework.cloud spring-cloud-starter-openfeign diff --git a/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/SpringCloudRegistry.java b/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/SpringCloudRegistry.java index e02d10836..30678ed1f 100644 --- a/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/SpringCloudRegistry.java +++ b/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/SpringCloudRegistry.java @@ -49,6 +49,7 @@ import static com.alibaba.dubbo.common.Constants.CONFIGURATORS_CATEGORY; import static com.alibaba.dubbo.common.Constants.CONSUMERS_CATEGORY; import static com.alibaba.dubbo.common.Constants.PROVIDERS_CATEGORY; import static com.alibaba.dubbo.common.Constants.ROUTERS_CATEGORY; +import static java.lang.Long.getLong; import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; import static org.springframework.beans.BeanUtils.instantiateClass; import static org.springframework.core.ResolvableType.forInstance; @@ -84,40 +85,40 @@ public class SpringCloudRegistry extends FailbackRegistry { private static final String WILDCARD = "*"; - private final Logger logger = LoggerFactory.getLogger(getClass()); - /** - * The separator for service name + * The interval in second of lookup service names(only for Dubbo-OPS) */ - private static final String SERVICE_NAME_SEPARATOR = ":"; - - private final ApplicationContext applicationContext; - - private final ServiceRegistry serviceRegistry; + private static final long ALL_SERVICES_LOOKUP_INTERVAL = getLong("dubbo.all.services.lookup.interval", 30); - private final DiscoveryClient discoveryClient; - - private final RegistrationFactory registrationFactory; + /** + * The interval in second of lookup regigered service instances + */ + private static final long REGISTERED_SERVICES_LOOKUP_INTERVAL = getLong("dubbo.registered.services.lookup.interval", 300); /** * The {@link ScheduledExecutorService Scheduler} to lookup the registered services */ - private final ScheduledExecutorService registeredServicesLookupScheduler; + private static final ScheduledExecutorService registeredServicesLookupScheduler = newSingleThreadScheduledExecutor(new NamedThreadFactory("dubbo-registered-services-lookup-")); /** * The {@link ScheduledExecutorService Scheduler} to lookup all services (only for Dubbo-OPS) */ - private volatile ScheduledExecutorService allServicesLookupScheduler; + private static volatile ScheduledExecutorService allServicesLookupScheduler; - /** - * The interval in second of lookup service names(only for Dubbo-OPS) - */ - private static final long ALL_SERVICES_LOOKUP_INTERVAL = Long.getLong("dubbo.all.services.lookup.interval", 30); + private final Logger logger = LoggerFactory.getLogger(getClass()); /** - * The interval in second of lookup regigered service instances + * The separator for service name */ - private static final long REGISTERED_SERVICES_LOOKUP_INTERVAL = Long.getLong("dubbo.registered.services.lookup.interval", 30); + private static final String SERVICE_NAME_SEPARATOR = ":"; + + private final ApplicationContext applicationContext; + + private final ServiceRegistry serviceRegistry; + + private final DiscoveryClient discoveryClient; + + private final RegistrationFactory registrationFactory; public SpringCloudRegistry(URL url, ApplicationContext applicationContext) { super(url); @@ -126,7 +127,6 @@ public class SpringCloudRegistry extends FailbackRegistry { this.registrationFactory = buildRegistrationFactory(serviceRegistry, applicationContext.getClassLoader()); this.discoveryClient = applicationContext.getBean(DiscoveryClient.class); applicationContext.getClassLoader(); - this.registeredServicesLookupScheduler = newSingleThreadScheduledExecutor(new NamedThreadFactory("dubbo-registered-services-lookup-")); } private RegistrationFactory buildRegistrationFactory(ServiceRegistry serviceRegistry, @@ -143,7 +143,7 @@ public class SpringCloudRegistry extends FailbackRegistry { Class factoryClass = resolveClassName(factoryClassName, classLoader); ResolvableType registrationFactoryType = forType(factoryClass); Class actualRegistrationClass = resolveGenericClass(registrationFactoryType, RegistrationFactory.class, 0); - if (actualRegistrationClass.equals(registrationClass)) { + if (registrationClass.equals(actualRegistrationClass)) { registrationFactory = (RegistrationFactory) instantiateClass(registrationFactoryType.getRawClass()); break; } @@ -172,30 +172,34 @@ public class SpringCloudRegistry extends FailbackRegistry { ResolvableType resolvableType = implementedType; - OUTER: - while (true) { + try { + OUTER: + while (true) { - ResolvableType[] interfaceTypes = resolvableType.getInterfaces(); + ResolvableType[] interfaceTypes = resolvableType.getInterfaces(); - for (ResolvableType interfaceType : interfaceTypes) { - if (interfaceType.resolve().equals(interfaceClass)) { - resolvableType = interfaceType; - break OUTER; + for (ResolvableType interfaceType : interfaceTypes) { + if (interfaceType.resolve().equals(interfaceClass)) { + resolvableType = interfaceType; + break OUTER; + } } - } - ResolvableType superType = resolvableType.getSuperType(); + ResolvableType superType = resolvableType.getSuperType(); - Class superClass = superType.resolve(); + Class superClass = superType.resolve(); - if (Object.class.equals(superClass)) { - break; + if (Object.class.equals(superClass)) { + break; + } + + resolvableType = superType; } - resolvableType = superType; + } catch (Throwable e) { + resolvableType = ResolvableType.forType(void.class); } - return resolvableType.resolveGeneric(index); } @@ -232,9 +236,9 @@ public class SpringCloudRegistry extends FailbackRegistry { shutdownServiceNamesLookup(); } - if (registeredServicesLookupScheduler != null) { - registeredServicesLookupScheduler.shutdown(); - } +// if (registeredServicesLookupScheduler != null) { +// registeredServicesLookupScheduler.shutdown(); +// } } @Override diff --git a/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/hashicorp/consul/ConsulRegistrationFactory.java b/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/hashicorp/consul/ConsulRegistrationFactory.java new file mode 100644 index 000000000..89c19acfe --- /dev/null +++ b/spring-cloud-alibaba-dubbo/src/main/java/org/springframework/cloud/alibaba/dubbo/registry/hashicorp/consul/ConsulRegistrationFactory.java @@ -0,0 +1,92 @@ +/* + * 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.springframework.cloud.alibaba.dubbo.registry.hashicorp.consul; + +import com.alibaba.dubbo.common.URL; + +import com.ecwid.consul.v1.agent.model.NewService; +import org.springframework.cloud.alibaba.dubbo.registry.AbstractRegistrationFactory; +import org.springframework.cloud.alibaba.dubbo.registry.RegistrationFactory; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties; +import org.springframework.cloud.consul.discovery.ConsulServerUtils; +import org.springframework.cloud.consul.serviceregistry.ConsulRegistration; +import org.springframework.context.ApplicationContext; + +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * {@link ConsulRegistration} {@link RegistrationFactory} implementation + * + * @author Mercy + */ +public class ConsulRegistrationFactory extends AbstractRegistrationFactory { + + @Override + public ConsulRegistration create(String serviceName, URL url, ApplicationContext applicationContext) { + ServiceInstance serviceInstance = createServiceInstance(serviceName, url); + + Map metadata = getMetadata(serviceInstance); + List tags = createTags(metadata); + + NewService service = new NewService(); + service.setId(serviceInstance.getInstanceId()); + service.setName(serviceInstance.getServiceId()); + service.setAddress(serviceInstance.getHost()); + service.setPort(serviceInstance.getPort()); + service.setMeta(metadata); + service.setTags(tags); + + ConsulDiscoveryProperties properties = applicationContext.getBean(ConsulDiscoveryProperties.class); + + ConsulRegistration registration = new ConsulRegistration(service, properties); + return registration; + } + + /** + * @param metadata + * @return + * @see ConsulServerUtils#getMetadata(java.util.List) + */ + private List createTags(Map metadata) { + List tags = new LinkedList<>(); + for (Map.Entry entry : metadata.entrySet()) { + String tag = entry.getKey() + "=" + entry.getValue(); + tags.add(tag); + + } + return tags; + } + + private Map getMetadata(ServiceInstance serviceInstance) { + Map metadata = serviceInstance.getMetadata(); + Set removedKeys = new LinkedHashSet<>(); + for (String key : metadata.keySet()) { + if (key.contains(".")) { + removedKeys.add(key); + } + } + for (String removedKey : removedKeys) { + metadata.remove(removedKey); + } + return metadata; + } +} diff --git a/spring-cloud-alibaba-dubbo/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-dubbo/src/main/resources/META-INF/spring.factories index c18edcd54..f90bb175a 100644 --- a/spring-cloud-alibaba-dubbo/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alibaba-dubbo/src/main/resources/META-INF/spring.factories @@ -11,4 +11,5 @@ org.springframework.context.ApplicationContextInitializer=\ org.springframework.cloud.alibaba.dubbo.registry.RegistrationFactory=\ org.springframework.cloud.alibaba.dubbo.registry.DefaultRegistrationFactory,\ org.springframework.cloud.alibaba.dubbo.registry.netflix.eureka.EurekaRegistrationFactory,\ - org.springframework.cloud.alibaba.dubbo.registry.apache.zookeeper.ZookeeperRegistrationFactory \ No newline at end of file + org.springframework.cloud.alibaba.dubbo.registry.apache.zookeeper.ZookeeperRegistrationFactory,\ + org.springframework.cloud.alibaba.dubbo.registry.hashicorp.consul.ConsulRegistrationFactory \ No newline at end of file