mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish : /spring-cloud-incubator/spring-cloud-alibaba#386 : supports Spring Cloud Consul
This commit is contained in:
parent
21f3451f7b
commit
8e9398d24f
@ -18,6 +18,7 @@
|
||||
<dubbo-spring-boot.version>0.2.1.RELEASE</dubbo-spring-boot.version>
|
||||
<dubbo-registry-nacos.version>0.0.2</dubbo-registry-nacos.version>
|
||||
<spring-cloud-zookeeper.version>2.1.0.RELEASE</spring-cloud-zookeeper.version>
|
||||
<spring-cloud-consul.version>2.1.0.RELEASE</spring-cloud-consul.version>
|
||||
<curator.version>4.0.1</curator.version>
|
||||
</properties>
|
||||
|
||||
@ -55,6 +56,44 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Eureka Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
@ -96,6 +135,14 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Consul -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
|
||||
<version>${spring-cloud-consul.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Nacos Service Discovery -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
@ -103,43 +150,7 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Open Feign -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
|
@ -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<Registration> 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
|
||||
|
@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public class ConsulRegistrationFactory extends AbstractRegistrationFactory<ConsulRegistration> {
|
||||
|
||||
@Override
|
||||
public ConsulRegistration create(String serviceName, URL url, ApplicationContext applicationContext) {
|
||||
ServiceInstance serviceInstance = createServiceInstance(serviceName, url);
|
||||
|
||||
Map<String, String> metadata = getMetadata(serviceInstance);
|
||||
List<String> 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<String> createTags(Map<String, String> metadata) {
|
||||
List<String> tags = new LinkedList<>();
|
||||
for (Map.Entry<String, String> entry : metadata.entrySet()) {
|
||||
String tag = entry.getKey() + "=" + entry.getValue();
|
||||
tags.add(tag);
|
||||
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
private Map<String, String> getMetadata(ServiceInstance serviceInstance) {
|
||||
Map<String, String> metadata = serviceInstance.getMetadata();
|
||||
Set<String> removedKeys = new LinkedHashSet<>();
|
||||
for (String key : metadata.keySet()) {
|
||||
if (key.contains(".")) {
|
||||
removedKeys.add(key);
|
||||
}
|
||||
}
|
||||
for (String removedKey : removedKeys) {
|
||||
metadata.remove(removedKey);
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
}
|
@ -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
|
||||
org.springframework.cloud.alibaba.dubbo.registry.apache.zookeeper.ZookeeperRegistrationFactory,\
|
||||
org.springframework.cloud.alibaba.dubbo.registry.hashicorp.consul.ConsulRegistrationFactory
|
Loading…
x
Reference in New Issue
Block a user