diff --git a/spring-cloud-alibaba-dubbo/pom.xml b/spring-cloud-alibaba-dubbo/pom.xml index 5dbf37ac..5d51c191 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,6 +135,14 @@ true + + + org.springframework.cloud + spring-cloud-starter-consul-discovery + ${spring-cloud-consul.version} + true + + org.springframework.cloud @@ -103,43 +150,7 @@ 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 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 e02d1083..30678ed1 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,6 +85,26 @@ public class SpringCloudRegistry extends FailbackRegistry { private static final String WILDCARD = "*"; + /** + * The interval in second of lookup service names(only for Dubbo-OPS) + */ + private static final long ALL_SERVICES_LOOKUP_INTERVAL = getLong("dubbo.all.services.lookup.interval", 30); + + /** + * 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 static final ScheduledExecutorService registeredServicesLookupScheduler = newSingleThreadScheduledExecutor(new NamedThreadFactory("dubbo-registered-services-lookup-")); + + /** + * The {@link ScheduledExecutorService Scheduler} to lookup all services (only for Dubbo-OPS) + */ + private static volatile ScheduledExecutorService allServicesLookupScheduler; + private final Logger logger = LoggerFactory.getLogger(getClass()); /** @@ -99,26 +120,6 @@ public class SpringCloudRegistry extends FailbackRegistry { private final RegistrationFactory registrationFactory; - /** - * The {@link ScheduledExecutorService Scheduler} to lookup the registered services - */ - private final ScheduledExecutorService registeredServicesLookupScheduler; - - /** - * The {@link ScheduledExecutorService Scheduler} to lookup all services (only for Dubbo-OPS) - */ - private 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); - - /** - * The interval in second of lookup regigered service instances - */ - private static final long REGISTERED_SERVICES_LOOKUP_INTERVAL = Long.getLong("dubbo.registered.services.lookup.interval", 30); - public SpringCloudRegistry(URL url, ApplicationContext applicationContext) { super(url); this.applicationContext = applicationContext; @@ -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(); + + Class superClass = superType.resolve(); + + if (Object.class.equals(superClass)) { + break; + } + + resolvableType = superType; } - ResolvableType superType = resolvableType.getSuperType(); - - Class superClass = superType.resolve(); - - if (Object.class.equals(superClass)) { - break; - } - - 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 00000000..89c19acf --- /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 c18edcd5..f90bb175 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