mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #537 from mercyblitz/master
Dubbo Spring Cloud Bugfix and Refactor
This commit is contained in:
commit
9e80ea0cd9
@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.autoconfigure;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.alibaba.dubbo.env.DubboCloudProperties;
|
||||
@ -28,6 +29,9 @@ import org.springframework.cloud.alibaba.dubbo.service.parameter.RequestParamSer
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertyResolver;
|
||||
|
||||
/**
|
||||
* Spring Boot Auto-Configuration class for Dubbo Service
|
||||
@ -55,81 +59,15 @@ public class DubboServiceAutoConfiguration {
|
||||
static class ParameterResolversConfiguration {
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Bugfix code for an issue : https://github.com/apache/incubator-dubbo-spring-boot-project/issues/459
|
||||
// *
|
||||
// * @param environment {@link ConfigurableEnvironment}
|
||||
// * @return a Bean of {@link PropertyResolver}
|
||||
// */
|
||||
// @Primary
|
||||
// @Bean(name = BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME)
|
||||
// public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnvironment environment) {
|
||||
// ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
|
||||
// @Override
|
||||
// protected void customizePropertySources(MutablePropertySources propertySources) {
|
||||
// Map<String, Object> dubboScanProperties = PropertySourcesUtils.getSubProperties(environment, DUBBO_SCAN_PREFIX);
|
||||
// propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
|
||||
// }
|
||||
// };
|
||||
// ConfigurationPropertySources.attach(propertyResolver);
|
||||
// return new DelegatingPropertyResolver(propertyResolver);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private static class DelegatingPropertyResolver implements PropertyResolver {
|
||||
//
|
||||
// private final PropertyResolver delegate;
|
||||
//
|
||||
// DelegatingPropertyResolver(PropertyResolver delegate) {
|
||||
// Assert.notNull(delegate, "The delegate of PropertyResolver must not be null");
|
||||
// this.delegate = delegate;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public boolean containsProperty(String key) {
|
||||
// return delegate.containsProperty(key);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Nullable
|
||||
// public String getProperty(String key) {
|
||||
// return delegate.getProperty(key);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String getProperty(String key, String defaultValue) {
|
||||
// return delegate.getProperty(key, defaultValue);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// @Nullable
|
||||
// public <T> T getProperty(String key, Class<T> targetType) {
|
||||
// return delegate.getProperty(key, targetType);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
|
||||
// return delegate.getProperty(key, targetType, defaultValue);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String getRequiredProperty(String key) throws IllegalStateException {
|
||||
// return delegate.getRequiredProperty(key);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
|
||||
// return delegate.getRequiredProperty(key, targetType);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String resolvePlaceholders(String text) {
|
||||
// return delegate.resolvePlaceholders(text);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
|
||||
// return delegate.resolveRequiredPlaceholders(text);
|
||||
// }
|
||||
// }
|
||||
/**
|
||||
* Build a primary {@link PropertyResolver} bean to {@link Autowired @Autowired}
|
||||
*
|
||||
* @param environment {@link Environment}
|
||||
* @return alias bean for {@link Environment}
|
||||
*/
|
||||
@Bean
|
||||
@Primary
|
||||
public PropertyResolver primaryPropertyResolver(Environment environment) {
|
||||
return environment;
|
||||
}
|
||||
}
|
@ -18,7 +18,6 @@ package org.springframework.cloud.alibaba.dubbo.registry;
|
||||
|
||||
import org.apache.dubbo.common.Constants;
|
||||
import org.apache.dubbo.common.URL;
|
||||
import org.apache.dubbo.common.utils.UrlUtils;
|
||||
import org.apache.dubbo.registry.NotifyListener;
|
||||
import org.apache.dubbo.registry.RegistryFactory;
|
||||
import org.apache.dubbo.registry.support.FailbackRegistry;
|
||||
@ -29,20 +28,21 @@ import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singleton;
|
||||
import static org.apache.dubbo.common.Constants.PROVIDER_SIDE;
|
||||
import static org.apache.dubbo.common.Constants.SIDE_KEY;
|
||||
import static org.springframework.util.ObjectUtils.isEmpty;
|
||||
import static org.springframework.util.StringUtils.hasText;
|
||||
|
||||
/**
|
||||
* Abstract Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose protocol is "spring-cloud"
|
||||
@ -144,13 +144,8 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
private void filterServiceNames(Collection<String> serviceNames) {
|
||||
filter(serviceNames, new Filter<String>() {
|
||||
@Override
|
||||
public boolean accept(String serviceName) {
|
||||
return supports(serviceName);
|
||||
}
|
||||
});
|
||||
private Set<String> filterServiceNames(Collection<String> serviceNames) {
|
||||
return new LinkedHashSet<>(filter(serviceNames, this::supports));
|
||||
}
|
||||
|
||||
protected abstract boolean supports(String serviceName);
|
||||
@ -185,8 +180,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
*/
|
||||
protected Set<String> getServiceNamesForOps(URL url) {
|
||||
Set<String> serviceNames = getAllServiceNames();
|
||||
filterServiceNames(serviceNames);
|
||||
return serviceNames;
|
||||
return filterServiceNames(serviceNames);
|
||||
}
|
||||
|
||||
protected abstract String getServiceName(URL url);
|
||||
@ -206,7 +200,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
}
|
||||
|
||||
protected List<ServiceInstance> getServiceInstances(String serviceName) {
|
||||
return discoveryClient.getInstances(serviceName);
|
||||
return hasText(serviceName) ? discoveryClient.getInstances(serviceName) : emptyList();
|
||||
}
|
||||
|
||||
private void subscribe(final URL url, final NotifyListener listener, final Collection<String> serviceNames) {
|
||||
@ -227,67 +221,9 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
*/
|
||||
protected abstract void notifySubscriber(URL url, NotifyListener listener, List<ServiceInstance> serviceInstances);
|
||||
|
||||
protected void filterHealthyInstances(Collection<ServiceInstance> instances) {
|
||||
filter(instances, new Filter<ServiceInstance>() {
|
||||
@Override
|
||||
public boolean accept(ServiceInstance data) {
|
||||
// TODO check the details of status
|
||||
// return serviceRegistry.getStatus(new DubboRegistration(data)) != null;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
protected <T> Collection<T> filter(Collection<T> collection, Predicate<T> filter) {
|
||||
return collection.stream()
|
||||
.filter(filter)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
protected List<URL> buildURLs(URL consumerURL, Collection<ServiceInstance> serviceInstances) {
|
||||
if (serviceInstances.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<URL> urls = new LinkedList<URL>();
|
||||
for (ServiceInstance serviceInstance : serviceInstances) {
|
||||
URL url = buildURL(serviceInstance);
|
||||
if (UrlUtils.isMatch(consumerURL, url)) {
|
||||
urls.add(url);
|
||||
}
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
|
||||
private URL buildURL(ServiceInstance serviceInstance) {
|
||||
URL url = new URL(serviceInstance.getMetadata().get(Constants.PROTOCOL_KEY),
|
||||
serviceInstance.getHost(),
|
||||
serviceInstance.getPort(),
|
||||
serviceInstance.getMetadata());
|
||||
return url;
|
||||
}
|
||||
|
||||
private <T> void filter(Collection<T> collection, Filter<T> filter) {
|
||||
Iterator<T> iterator = collection.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
T data = iterator.next();
|
||||
if (!filter.accept(data)) { // remove if not accept
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T[] of(T... values) {
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* A filter
|
||||
*/
|
||||
public interface Filter<T> {
|
||||
|
||||
/**
|
||||
* Tests whether or not the specified data should be accepted.
|
||||
*
|
||||
* @param data The data to be tested
|
||||
* @return <code>true</code> if and only if <code>data</code>
|
||||
* should be accepted
|
||||
*/
|
||||
boolean accept(T data);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -178,5 +178,17 @@
|
||||
|
||||
</profiles>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
@ -35,4 +35,13 @@
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -80,4 +80,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -30,4 +30,13 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -36,4 +36,12 @@
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
Loading…
x
Reference in New Issue
Block a user