Format Code

pull/796/head
mercyblitz 6 years ago
parent 532c1dd0f2
commit 12efadd61f

@ -16,17 +16,19 @@
*/
package com.alibaba.cloud.dubbo.actuate;
import com.alibaba.cloud.dubbo.actuate.endpoint.DubboRestMetadataEndpoint;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import com.alibaba.cloud.dubbo.actuate.endpoint.DubboRestMetadataEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* Dubbo Metadata Endpoints Auto-{@link Configuration}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.annotation.Endpoint")
@PropertySource(value = "classpath:/META-INF/dubbo/default/actuator-endpoints.properties")

@ -16,15 +16,17 @@
*/
package com.alibaba.cloud.dubbo.actuate.endpoint;
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
/**
* Dubbo Rest Metadata {@link Endpoint}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
@Endpoint(id = "dubborestmetadata")
public class DubboRestMetadataEndpoint {

@ -49,6 +49,7 @@ import static org.apache.dubbo.rpc.cluster.Constants.DEFAULT_RETRIES;
* </ol>
* <p>
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see FeignClient
* @see LoadBalanced
*/

@ -16,6 +16,13 @@
*/
package com.alibaba.cloud.dubbo.autoconfigure;
import com.alibaba.cloud.dubbo.annotation.DubboTransported;
import com.alibaba.cloud.dubbo.client.loadbalancer.DubboMetadataInitializerInterceptor;
import com.alibaba.cloud.dubbo.client.loadbalancer.DubboTransporterInterceptor;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.metadata.resolver.DubboTransportedAttributesResolver;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
@ -25,13 +32,6 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import com.alibaba.cloud.dubbo.annotation.DubboTransported;
import com.alibaba.cloud.dubbo.client.loadbalancer.DubboMetadataInitializerInterceptor;
import com.alibaba.cloud.dubbo.client.loadbalancer.DubboTransporterInterceptor;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.metadata.resolver.DubboTransportedAttributesResolver;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;

@ -20,11 +20,6 @@ import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import feign.Contract;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import com.alibaba.cloud.dubbo.metadata.DubboProtocolConfigSupplier;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.metadata.resolver.DubboServiceBeanMetadataResolver;
@ -34,6 +29,11 @@ import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.service.IntrospectiveDubboMetadataService;
import com.alibaba.cloud.dubbo.util.JSONUtils;
import feign.Contract;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@ -16,14 +16,12 @@
*/
package com.alibaba.cloud.dubbo.autoconfigure;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.openfeign.TargeterBeanPostProcessor;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@ -16,18 +16,16 @@
*/
package com.alibaba.cloud.dubbo.autoconfigure;
import com.alibaba.cloud.dubbo.env.DubboCloudProperties;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
import com.alibaba.cloud.dubbo.service.parameter.PathVariableServiceParameterResolver;
import com.alibaba.cloud.dubbo.service.parameter.RequestBodyServiceParameterResolver;
import com.alibaba.cloud.dubbo.service.parameter.RequestHeaderServiceParameterResolver;
import com.alibaba.cloud.dubbo.service.parameter.RequestParamServiceParameterResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.alibaba.cloud.dubbo.env.DubboCloudProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@ -50,17 +48,6 @@ public class DubboServiceAutoConfiguration {
return new DubboGenericServiceFactory();
}
@Configuration
@Import(value = {
DubboGenericServiceExecutionContextFactory.class,
RequestParamServiceParameterResolver.class,
RequestBodyServiceParameterResolver.class,
RequestHeaderServiceParameterResolver.class,
PathVariableServiceParameterResolver.class
})
static class ParameterResolversConfiguration {
}
/**
* Build a primary {@link PropertyResolver} bean to {@link Autowired @Autowired}
*
@ -72,4 +59,15 @@ public class DubboServiceAutoConfiguration {
public PropertyResolver primaryPropertyResolver(Environment environment) {
return environment;
}
@Configuration
@Import(value = {
DubboGenericServiceExecutionContextFactory.class,
RequestParamServiceParameterResolver.class,
RequestBodyServiceParameterResolver.class,
RequestHeaderServiceParameterResolver.class,
PathVariableServiceParameterResolver.class
})
static class ParameterResolversConfiguration {
}
}

@ -107,6 +107,23 @@ public class DubboServiceRegistrationAutoConfiguration {
attachDubboMetadataServiceMetadata(registration);
}
private void attachDubboMetadataServiceMetadata(Registration registration) {
if (registration == null) {
return;
}
synchronized (registration) {
Map<String, String> metadata = registration.getMetadata();
attachDubboMetadataServiceMetadata(metadata);
}
}
private void attachDubboMetadataServiceMetadata(Map<String, String> metadata) {
Map<String, String> serviceMetadata = dubboServiceMetadataRepository.getDubboMetadataServiceMetadata();
if (!isEmpty(serviceMetadata)) {
metadata.putAll(serviceMetadata);
}
}
@Configuration
@ConditionalOnBean(name = EUREKA_CLIENT_AUTO_CONFIGURATION_CLASS_NAME)
@Aspect
@ -169,21 +186,4 @@ public class DubboServiceRegistrationAutoConfiguration {
}
}
}
private void attachDubboMetadataServiceMetadata(Registration registration) {
if (registration == null) {
return;
}
synchronized (registration) {
Map<String, String> metadata = registration.getMetadata();
attachDubboMetadataServiceMetadata(metadata);
}
}
private void attachDubboMetadataServiceMetadata(Map<String, String> metadata) {
Map<String, String> serviceMetadata = dubboServiceMetadataRepository.getDubboMetadataServiceMetadata();
if (!isEmpty(serviceMetadata)) {
metadata.putAll(serviceMetadata);
}
}
}

@ -16,9 +16,9 @@
*/
package com.alibaba.cloud.dubbo.autoconfigure.condition;
import com.alibaba.cloud.dubbo.registry.SpringCloudRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import com.alibaba.cloud.dubbo.registry.SpringCloudRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.ConfigurableEnvironment;
@ -27,12 +27,13 @@ import org.springframework.util.StringUtils;
import java.util.Map;
import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getSubProperties;
import static com.alibaba.cloud.dubbo.registry.SpringCloudRegistryFactory.PROTOCOL;
import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getSubProperties;
/**
* Missing {@link SpringCloudRegistry} Property {@link Condition}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see SpringCloudRegistry
* @see Condition
*/

@ -17,6 +17,7 @@
package com.alibaba.cloud.dubbo.client.loadbalancer;
import org.apache.dubbo.rpc.service.GenericException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;

@ -17,6 +17,7 @@
package com.alibaba.cloud.dubbo.client.loadbalancer;
import org.apache.dubbo.rpc.service.GenericException;
import com.alibaba.cloud.dubbo.http.converter.HttpMessageConverterHolder;
import com.alibaba.cloud.dubbo.http.util.HttpMessageConverterResolver;
import com.alibaba.cloud.dubbo.metadata.RequestMetadata;

@ -70,6 +70,10 @@ public class DubboNonWebApplicationEnvironmentPostProcessor implements Environme
private final Logger logger = LoggerFactory.getLogger(DubboNonWebApplicationEnvironmentPostProcessor.class);
private static boolean isRestProtocol(String protocol) {
return REST_PROTOCOL.equalsIgnoreCase(protocol);
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
WebApplicationType webApplicationType = application.getWebApplicationType();
@ -200,8 +204,4 @@ public class DubboNonWebApplicationEnvironmentPostProcessor implements Environme
public int getOrder() { // Keep LOWEST_PRECEDENCE
return LOWEST_PRECEDENCE;
}
private static boolean isRestProtocol(String protocol) {
return REST_PROTOCOL.equalsIgnoreCase(protocol);
}
}

@ -17,6 +17,7 @@
package com.alibaba.cloud.dubbo.http;
import org.apache.dubbo.common.io.UnsafeByteArrayInputStream;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;

@ -49,6 +49,10 @@ public class DefaultHttpRequest implements HttpRequest {
this.headers.putAll(headers);
}
public static Builder builder() {
return new Builder();
}
private URI buildURI(String path, Map<String, List<String>> params) {
UriComponentsBuilder builder = fromPath(path)
.queryParams(new LinkedMultiValueMap<>(params));
@ -75,10 +79,6 @@ public class DefaultHttpRequest implements HttpRequest {
return headers;
}
public static Builder builder() {
return new Builder();
}
/**
* {@link HttpRequest} Builder
*/

@ -27,50 +27,50 @@ import java.util.Iterator;
*/
public abstract class AbstractHttpRequestMatcher implements HttpRequestMatcher {
/**
* Return the discrete items a request condition is composed of.
* <p>
* For example URL patterns, HTTP request methods, param expressions, etc.
*
* @return a collection of objects, never {@code null}
*/
protected abstract Collection<?> getContent();
/**
* Return the discrete items a request condition is composed of.
* <p>
* For example URL patterns, HTTP request methods, param expressions, etc.
*
* @return a collection of objects, never {@code null}
*/
protected abstract Collection<?> getContent();
/**
* The notation to use when printing discrete items of content.
* <p>
* For example {@code " || "} for URL patterns or {@code " && "} for param
* expressions.
*/
protected abstract String getToStringInfix();
/**
* The notation to use when printing discrete items of content.
* <p>
* For example {@code " || "} for URL patterns or {@code " && "} for param
* expressions.
*/
protected abstract String getToStringInfix();
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
return getContent().equals(((AbstractHttpRequestMatcher) other).getContent());
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
return getContent().equals(((AbstractHttpRequestMatcher) other).getContent());
}
@Override
public int hashCode() {
return getContent().hashCode();
}
@Override
public int hashCode() {
return getContent().hashCode();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("[");
for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext();) {
Object expression = iterator.next();
builder.append(expression.toString());
if (iterator.hasNext()) {
builder.append(getToStringInfix());
}
}
builder.append("]");
return builder.toString();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder("[");
for (Iterator<?> iterator = getContent().iterator(); iterator.hasNext(); ) {
Object expression = iterator.next();
builder.append(expression.toString());
if (iterator.hasNext()) {
builder.append(getToStringInfix());
}
}
builder.append("]");
return builder.toString();
}
}

@ -69,6 +69,24 @@ public class HttpRequestConsumersMatcher extends AbstractHttpRequestMatcher {
Collections.sort(this.expressions);
}
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes, String[] headers) {
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>();
if (headers != null) {
for (String header : headers) {
HeaderExpression expr = new HeaderExpression(header);
if ("Content-Type".equalsIgnoreCase(expr.name) && expr.value != null) {
for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) {
result.add(new ConsumeMediaTypeExpression(mediaType, expr.negated));
}
}
}
}
for (String consume : consumes) {
result.add(new ConsumeMediaTypeExpression(consume));
}
return result;
}
@Override
public boolean match(HttpRequest request) {
@ -93,24 +111,6 @@ public class HttpRequestConsumersMatcher extends AbstractHttpRequestMatcher {
return true;
}
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes, String[] headers) {
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<>();
if (headers != null) {
for (String header : headers) {
HeaderExpression expr = new HeaderExpression(header);
if ("Content-Type".equalsIgnoreCase(expr.name) && expr.value != null) {
for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) {
result.add(new ConsumeMediaTypeExpression(mediaType, expr.negated));
}
}
}
}
for (String consume : consumes) {
result.add(new ConsumeMediaTypeExpression(consume));
}
return result;
}
@Override
protected Collection<ConsumeMediaTypeExpression> getContent() {
return this.expressions;

@ -43,6 +43,14 @@ public class HttpRequestParamsMatcher extends AbstractHttpRequestMatcher {
this.expressions = parseExpressions(params);
}
private static Set<ParamExpression> parseExpressions(String... params) {
Set<ParamExpression> expressions = new LinkedHashSet<>();
for (String param : params) {
expressions.add(new ParamExpression(param));
}
return expressions;
}
@Override
public boolean match(HttpRequest request) {
if (CollectionUtils.isEmpty(expressions)) {
@ -56,14 +64,6 @@ public class HttpRequestParamsMatcher extends AbstractHttpRequestMatcher {
return false;
}
private static Set<ParamExpression> parseExpressions(String... params) {
Set<ParamExpression> expressions = new LinkedHashSet<>();
for (String param : params) {
expressions.add(new ParamExpression(param));
}
return expressions;
}
@Override
protected Collection<ParamExpression> getContent() {
return this.expressions;

@ -45,6 +45,17 @@ public class HttpRequestPathMatcher extends AbstractHttpRequestMatcher {
this.pathMatcher = new AntPathMatcher();
}
private static Set<String> prependLeadingSlash(String[] patterns) {
Set<String> result = new LinkedHashSet<>(patterns.length);
for (String pattern : patterns) {
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
pattern = "/" + pattern;
}
result.add(pattern);
}
return result;
}
@Override
public boolean match(HttpRequest request) {
List<String> matches = getMatchingPatterns(request);
@ -94,17 +105,6 @@ public class HttpRequestPathMatcher extends AbstractHttpRequestMatcher {
return uri.getPath();
}
private static Set<String> prependLeadingSlash(String[] patterns) {
Set<String> result = new LinkedHashSet<>(patterns.length);
for (String pattern : patterns) {
if (StringUtils.hasLength(pattern) && !pattern.startsWith("/")) {
pattern = "/" + pattern;
}
result.add(pattern);
}
return result;
}
@Override
protected Collection<String> getContent() {
return this.patterns;

@ -69,6 +69,24 @@ public class HttpRequestProducesMatcher extends AbstractHttpRequestMatcher {
Collections.sort(this.expressions);
}
private static Set<ProduceMediaTypeExpression> parseExpressions(String[] produces, String[] headers) {
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>();
if (headers != null) {
for (String header : headers) {
HeaderExpression expr = new HeaderExpression(header);
if (HttpHeaders.ACCEPT.equalsIgnoreCase(expr.name) && expr.value != null) {
for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) {
result.add(new ProduceMediaTypeExpression(mediaType, expr.negated));
}
}
}
}
for (String produce : produces) {
result.add(new ProduceMediaTypeExpression(produce));
}
return result;
}
@Override
public boolean match(HttpRequest request) {
@ -89,24 +107,6 @@ public class HttpRequestProducesMatcher extends AbstractHttpRequestMatcher {
return true;
}
private static Set<ProduceMediaTypeExpression> parseExpressions(String[] produces, String[] headers) {
Set<ProduceMediaTypeExpression> result = new LinkedHashSet<>();
if (headers != null) {
for (String header : headers) {
HeaderExpression expr = new HeaderExpression(header);
if (HttpHeaders.ACCEPT.equalsIgnoreCase(expr.name) && expr.value != null) {
for (MediaType mediaType : MediaType.parseMediaTypes(expr.value)) {
result.add(new ProduceMediaTypeExpression(mediaType, expr.negated));
}
}
}
}
for (String produce : produces) {
result.add(new ProduceMediaTypeExpression(produce));
}
return result;
}
@Override
protected Collection<ProduceMediaTypeExpression> getContent() {
return expressions;

@ -71,6 +71,63 @@ public class RequestMetadata {
headers(requestTemplate.headers());
}
/**
* Get the best matched {@link RequestMetadata} via specified {@link RequestMetadata}
*
* @param requestMetadataMap the source of {@link NavigableMap}
* @param requestMetadata the match object
* @return if not matched, return <code>null</code>
*/
public static RequestMetadata getBestMatch(NavigableMap<RequestMetadata, RequestMetadata> requestMetadataMap,
RequestMetadata requestMetadata) {
RequestMetadata key = requestMetadata;
RequestMetadata result = requestMetadataMap.get(key);
if (result == null) {
SortedMap<RequestMetadata, RequestMetadata> headMap = requestMetadataMap.headMap(key, true);
result = headMap.isEmpty() ? null : requestMetadataMap.get(headMap.lastKey());
}
return result;
}
private static void add(String key, String value, MultiValueMap<String, String> destination) {
destination.add(key, value);
}
private static <T extends Collection<String>> void addAll(Map<String, T> source,
MultiValueMap<String, String> destination) {
for (Map.Entry<String, T> entry : source.entrySet()) {
String key = entry.getKey();
for (String value : entry.getValue()) {
add(key, value, destination);
}
}
}
private static void mediaTypes(HttpHeaders httpHeaders, String headerName, Collection<String> destination) {
List<String> value = httpHeaders.get(headerName);
List<MediaType> mediaTypes = parseMediaTypes(value);
destination.addAll(toMediaTypeValues(mediaTypes));
}
private static List<String> toMediaTypeValues(List<MediaType> mediaTypes) {
List<String> list = new ArrayList<>(mediaTypes.size());
for (MediaType mediaType : mediaTypes) {
list.add(mediaType.toString());
}
return list;
}
private static List<MediaType> toMediaTypes(Collection<String> mediaTypeValues) {
if (mediaTypeValues.isEmpty()) {
return Collections.singletonList(MediaType.ALL);
}
return parseMediaTypes(new LinkedList<>(mediaTypeValues));
}
public String getMethod() {
return method;
}
@ -176,63 +233,6 @@ public class RequestMetadata {
return this;
}
/**
* Get the best matched {@link RequestMetadata} via specified {@link RequestMetadata}
*
* @param requestMetadataMap the source of {@link NavigableMap}
* @param requestMetadata the match object
* @return if not matched, return <code>null</code>
*/
public static RequestMetadata getBestMatch(NavigableMap<RequestMetadata, RequestMetadata> requestMetadataMap,
RequestMetadata requestMetadata) {
RequestMetadata key = requestMetadata;
RequestMetadata result = requestMetadataMap.get(key);
if (result == null) {
SortedMap<RequestMetadata, RequestMetadata> headMap = requestMetadataMap.headMap(key, true);
result = headMap.isEmpty() ? null : requestMetadataMap.get(headMap.lastKey());
}
return result;
}
private static void add(String key, String value, MultiValueMap<String, String> destination) {
destination.add(key, value);
}
private static <T extends Collection<String>> void addAll(Map<String, T> source,
MultiValueMap<String, String> destination) {
for (Map.Entry<String, T> entry : source.entrySet()) {
String key = entry.getKey();
for (String value : entry.getValue()) {
add(key, value, destination);
}
}
}
private static void mediaTypes(HttpHeaders httpHeaders, String headerName, Collection<String> destination) {
List<String> value = httpHeaders.get(headerName);
List<MediaType> mediaTypes = parseMediaTypes(value);
destination.addAll(toMediaTypeValues(mediaTypes));
}
private static List<String> toMediaTypeValues(List<MediaType> mediaTypes) {
List<String> list = new ArrayList<>(mediaTypes.size());
for (MediaType mediaType : mediaTypes) {
list.add(mediaType.toString());
}
return list;
}
private static List<MediaType> toMediaTypes(Collection<String> mediaTypeValues) {
if (mediaTypeValues.isEmpty()) {
return Collections.singletonList(MediaType.ALL);
}
return parseMediaTypes(new LinkedList<>(mediaTypeValues));
}
@Override
public boolean equals(Object o) {
if (this == o) {

@ -102,29 +102,17 @@ public class DubboServiceMetadataRepository implements SmartInitializingSingleto
* Monitor object for synchronization
*/
private final Object monitor = new Object();
private ApplicationEventPublisher applicationEventPublisher;
/**
* A {@link Set} of service names that had been initialized
*/
private final Set<String> initializedServices = new LinkedHashSet<>();
// =================================== Registration =================================== //
/**
* All exported {@link URL urls} {@link Map} whose key is the return value of {@link URL#getServiceKey()} method
* and value is the {@link List} of {@link URL URLs}
*/
private final MultiValueMap<String, URL> allExportedURLs = new LinkedMultiValueMap<>();
// ==================================================================================== //
// =================================== Subscription =================================== //
private volatile Set<String> subscribedServices = emptySet();
// =================================== Registration =================================== //
/**
* The subscribed {@link URL urls} {@link Map} of {@link DubboMetadataService},
* whose key is the return value of {@link URL#getServiceKey()} method and value is the {@link List} of
@ -135,14 +123,19 @@ public class DubboServiceMetadataRepository implements SmartInitializingSingleto
// ==================================================================================== //
// =================================== REST Metadata ================================== //
// =================================== Subscription =================================== //
/**
* A Map to store REST metadata temporary, its' key is the special service name for a Dubbo service,
* the value is a JSON content of JAX-RS or Spring MVC REST metadata from the annotated methods.
*/
private final Set<ServiceRestMetadata> serviceRestMetadata = new LinkedHashSet<>();
private ApplicationEventPublisher applicationEventPublisher;
// ==================================================================================== //
// =================================== REST Metadata ================================== //
private volatile Set<String> subscribedServices = emptySet();
/**
* Key is application name
* Value is Map<RequestMetadata, DubboRestServiceMetadata>
@ -178,6 +171,23 @@ public class DubboServiceMetadataRepository implements SmartInitializingSingleto
// ==================================================================================== //
private static <K, V> Map<K, V> getMap(Map<String, Map<K, V>> repository, String key) {
return getOrDefault(repository, key, newHashMap());
}
private static <K, V> V getOrDefault(Map<K, V> source, K key, V defaultValue) {
V value = source.get(key);
if (value == null) {
value = defaultValue;
source.put(key, value);
}
return value;
}
private static <K, V> Map<K, V> newHashMap() {
return new LinkedHashMap<>();
}
/**
* Initialize {@link #subscribedServices the subscribed services}
*
@ -556,23 +566,6 @@ public class DubboServiceMetadataRepository implements SmartInitializingSingleto
return metadata;
}
private static <K, V> Map<K, V> getMap(Map<String, Map<K, V>> repository, String key) {
return getOrDefault(repository, key, newHashMap());
}
private static <K, V> V getOrDefault(Map<K, V> source, K key, V defaultValue) {
V value = source.get(key);
if (value == null) {
value = defaultValue;
source.put(key, value);
}
return value;
}
private static <K, V> Map<K, V> newHashMap() {
return new LinkedHashMap<>();
}
private void excludeSelf(Set<String> subscribedServices) {
subscribedServices.remove(currentApplicationName);
}

@ -16,18 +16,19 @@
*/
package com.alibaba.cloud.dubbo.metadata.resolver;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.spring.ServiceBean;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import com.alibaba.cloud.dubbo.metadata.ServiceRestMetadata;
import feign.Contract;
import feign.Feign;
import feign.MethodMetadata;
import feign.Util;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.spring.ServiceBean;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.SmartInitializingSingleton;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import com.alibaba.cloud.dubbo.metadata.ServiceRestMetadata;
import org.springframework.util.ClassUtils;
import java.lang.reflect.Method;

@ -16,11 +16,11 @@
*/
package com.alibaba.cloud.dubbo.metadata.resolver;
import feign.Contract;
import com.alibaba.cloud.dubbo.annotation.DubboTransported;
import com.alibaba.cloud.dubbo.metadata.DubboTransportedMethodMetadata;
import com.alibaba.cloud.dubbo.metadata.MethodMetadata;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import feign.Contract;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.PropertyResolver;

@ -17,6 +17,7 @@
package com.alibaba.cloud.dubbo.metadata.resolver;
import org.apache.dubbo.config.spring.ServiceBean;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import com.alibaba.cloud.dubbo.metadata.ServiceRestMetadata;

@ -17,6 +17,7 @@
package com.alibaba.cloud.dubbo.openfeign;
import org.apache.dubbo.rpc.service.GenericService;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import java.lang.reflect.Method;

@ -16,18 +16,16 @@
*/
package com.alibaba.cloud.dubbo.openfeign;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceExecutionContextFactory;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import org.springframework.core.env.Environment;
import static java.lang.reflect.Proxy.newProxyInstance;
import static com.alibaba.cloud.dubbo.autoconfigure.DubboOpenFeignAutoConfiguration.TARGETER_CLASS_NAME;
import static java.lang.reflect.Proxy.newProxyInstance;
import static org.springframework.util.ClassUtils.getUserClass;
import static org.springframework.util.ClassUtils.isPresent;
import static org.springframework.util.ClassUtils.resolveClassName;

@ -78,6 +78,10 @@ class TargeterInvocationHandler implements InvocationHandler {
this.contextFactory = contextFactory;
}
private static <T> T cast(Object object) {
return (T) object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
/**
@ -168,8 +172,4 @@ class TargeterInvocationHandler implements InvocationHandler {
return feignMethodMetadataMap;
}
private static <T> T cast(Object object) {
return (T) object;
}
}

@ -16,11 +16,11 @@
*/
package com.alibaba.cloud.dubbo.registry;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationEventPublisher;

@ -59,6 +59,10 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
public SpringCloudRegistryFactory() {
}
public static void setApplicationContext(ConfigurableApplicationContext applicationContext) {
SpringCloudRegistryFactory.applicationContext = applicationContext;
}
protected void init() {
if (initialized || applicationContext == null) {
return;
@ -75,8 +79,4 @@ public class SpringCloudRegistryFactory implements RegistryFactory {
return new SpringCloudRegistry(url, discoveryClient, dubboServiceMetadataRepository,
dubboMetadataConfigServiceProxy, jsonUtils, applicationContext);
}
public static void setApplicationContext(ConfigurableApplicationContext applicationContext) {
SpringCloudRegistryFactory.applicationContext = applicationContext;
}
}

@ -27,6 +27,8 @@ import static java.util.Collections.unmodifiableCollection;
/**
* An event raised after the {@link ServiceInstance instances} of one service has been changed.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ServiceInstancesChangedEvent extends ApplicationEvent {

@ -16,12 +16,12 @@
*/
package com.alibaba.cloud.dubbo.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.cloud.dubbo.http.HttpServerRequest;
import com.alibaba.cloud.dubbo.metadata.MethodMetadata;
import com.alibaba.cloud.dubbo.metadata.MethodParameterMetadata;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import com.alibaba.cloud.dubbo.service.parameter.DubboGenericServiceParameterResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import javax.annotation.PostConstruct;

@ -67,8 +67,8 @@ public interface DubboMetadataService {
* Get the json content of an exported List<URL> of {@link URL URLs} by the serviceInterface , group and version
*
* @param serviceInterface The class name of service interface
* @param group {@link Service#group() the service group} (optional)
* @param version {@link Service#version() the service version} (optional)
* @param group {@link Service#group() the service group} (optional)
* @param version {@link Service#version() the service version} (optional)
* @return non-null read-only {@link List}
* @see URL
*/

@ -26,14 +26,14 @@ import static java.lang.reflect.Proxy.newProxyInstance;
/**
* The proxy of {@link DubboMetadataService}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class DubboMetadataServiceProxy implements BeanClassLoaderAware, DisposableBean {
private final DubboGenericServiceFactory dubboGenericServiceFactory;
private ClassLoader classLoader;
private final Map<String, DubboMetadataService> dubboMetadataServiceCache = new ConcurrentHashMap<>();
private ClassLoader classLoader;
public DubboMetadataServiceProxy(DubboGenericServiceFactory dubboGenericServiceFactory) {
this.dubboGenericServiceFactory = dubboGenericServiceFactory;

@ -18,13 +18,13 @@ package com.alibaba.cloud.dubbo.service;
import org.apache.dubbo.common.URL;
import com.alibaba.cloud.dubbo.metadata.ServiceRestMetadata;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.cloud.dubbo.metadata.ServiceRestMetadata;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.util.JSONUtils;
import java.util.Collections;
import java.util.HashMap;

@ -16,67 +16,67 @@
*/
package com.alibaba.cloud.dubbo.service.parameter;
import static org.springframework.context.ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME;
import static org.springframework.util.ClassUtils.resolveClassName;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import static org.springframework.context.ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME;
import static org.springframework.util.ClassUtils.resolveClassName;
/**
* Abstract {@link DubboGenericServiceParameterResolver} implementation
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public abstract class AbstractDubboGenericServiceParameterResolver
implements DubboGenericServiceParameterResolver, BeanClassLoaderAware {
implements DubboGenericServiceParameterResolver, BeanClassLoaderAware {
private int order;
private int order;
@Autowired(required = false)
@Qualifier(CONVERSION_SERVICE_BEAN_NAME)
private ConversionService conversionService = new DefaultFormattingConversionService();
@Autowired(required = false)
@Qualifier(CONVERSION_SERVICE_BEAN_NAME)
private ConversionService conversionService = new DefaultFormattingConversionService();
private ClassLoader classLoader;
private ClassLoader classLoader;
public ConversionService getConversionService() {
return conversionService;
}
public ConversionService getConversionService() {
return conversionService;
}
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
public ClassLoader getClassLoader() {
return classLoader;
}
public ClassLoader getClassLoader() {
return classLoader;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public int getOrder() {
return order;
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
protected Class<?> resolveClass(String className) {
return resolveClassName(className, classLoader);
}
protected Class<?> resolveClass(String className) {
return resolveClassName(className, classLoader);
}
protected Object resolveValue(Object parameterValue, String parameterType) {
Class<?> targetType = resolveClass(parameterType);
return resolveValue(parameterValue, targetType);
}
protected Object resolveValue(Object parameterValue, String parameterType) {
Class<?> targetType = resolveClass(parameterType);
return resolveValue(parameterValue, targetType);
}
protected Object resolveValue(Object parameterValue, Class<?> parameterType) {
return conversionService.convert(parameterValue, parameterType);
}
protected Object resolveValue(Object parameterValue, Class<?> parameterType) {
return conversionService.convert(parameterValue, parameterType);
}
}

@ -16,10 +16,10 @@
*/
package com.alibaba.cloud.dubbo.service.parameter;
import com.alibaba.cloud.dubbo.metadata.MethodParameterMetadata;
import org.apache.dubbo.rpc.service.GenericService;
import com.alibaba.cloud.dubbo.http.HttpServerRequest;
import com.alibaba.cloud.dubbo.metadata.MethodParameterMetadata;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import org.springframework.core.Ordered;

@ -16,14 +16,14 @@
*/
package com.alibaba.cloud.dubbo.service.parameter;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import com.alibaba.cloud.dubbo.http.HttpServerRequest;
import com.alibaba.cloud.dubbo.http.converter.HttpMessageConverterHolder;
import com.alibaba.cloud.dubbo.http.util.HttpMessageConverterResolver;
import com.alibaba.cloud.dubbo.metadata.MethodParameterMetadata;
import com.alibaba.cloud.dubbo.metadata.RestMethodMetadata;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;

Loading…
Cancel
Save