mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #905 from echooymxq/master
Advance adapt Dubbo restTemplates
This commit is contained in:
commit
08cce947fd
@ -25,16 +25,19 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
|||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
|
||||||
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
|
import org.springframework.cloud.client.loadbalancer.RestTemplateCustomizer;
|
||||||
import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor;
|
import org.springframework.cloud.client.loadbalancer.RetryLoadBalancerInterceptor;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.event.ContextRefreshedEvent;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.type.MethodMetadata;
|
import org.springframework.core.type.MethodMetadata;
|
||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
@ -56,8 +59,8 @@ import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory;
|
|||||||
@ConditionalOnClass(name = { "org.springframework.web.client.RestTemplate" })
|
@ConditionalOnClass(name = { "org.springframework.web.client.RestTemplate" })
|
||||||
@AutoConfigureAfter(name = {
|
@AutoConfigureAfter(name = {
|
||||||
"org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration" })
|
"org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration" })
|
||||||
public class DubboLoadBalancedRestTemplateAutoConfiguration
|
public class DubboLoadBalancedRestTemplateAutoConfiguration implements
|
||||||
implements BeanClassLoaderAware, SmartInitializingSingleton {
|
BeanClassLoaderAware, ApplicationContextAware, SmartInitializingSingleton {
|
||||||
|
|
||||||
private static final Class<DubboTransported> DUBBO_TRANSPORTED_CLASS = DubboTransported.class;
|
private static final Class<DubboTransported> DUBBO_TRANSPORTED_CLASS = DubboTransported.class;
|
||||||
|
|
||||||
@ -89,6 +92,9 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration
|
|||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private Map<String, RestTemplate> restTemplates = Collections.emptyMap();
|
private Map<String, RestTemplate> restTemplates = Collections.emptyMap();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,8 +117,10 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration
|
|||||||
* {@link SmartInitializingSingleton} beans or
|
* {@link SmartInitializingSingleton} beans or
|
||||||
* {@link RestTemplateCustomizer#customize(RestTemplate) customization})
|
* {@link RestTemplateCustomizer#customize(RestTemplate) customization})
|
||||||
*/
|
*/
|
||||||
@EventListener(ApplicationStartedEvent.class)
|
@EventListener(ContextRefreshedEvent.class)
|
||||||
public void adaptRestTemplates() {
|
public void adaptRestTemplates(ContextRefreshedEvent event) {
|
||||||
|
|
||||||
|
if (event.getApplicationContext() == this.applicationContext) {
|
||||||
|
|
||||||
DubboTransportedAttributesResolver attributesResolver = new DubboTransportedAttributesResolver(
|
DubboTransportedAttributesResolver attributesResolver = new DubboTransportedAttributesResolver(
|
||||||
environment);
|
environment);
|
||||||
@ -126,6 +134,7 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the annotation attributes {@link RestTemplate} bean being annotated
|
* Gets the annotation attributes {@link RestTemplate} bean being annotated
|
||||||
@ -144,10 +153,10 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration
|
|||||||
AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
|
AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
|
||||||
MethodMetadata factoryMethodMetadata = annotatedBeanDefinition
|
MethodMetadata factoryMethodMetadata = annotatedBeanDefinition
|
||||||
.getFactoryMethodMetadata();
|
.getFactoryMethodMetadata();
|
||||||
attributes = factoryMethodMetadata != null
|
attributes = factoryMethodMetadata != null ? Optional
|
||||||
? Optional.ofNullable(factoryMethodMetadata
|
.ofNullable(factoryMethodMetadata
|
||||||
.getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME)).orElse(attributes)
|
.getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME))
|
||||||
: Collections.emptyMap();
|
.orElse(attributes) : Collections.emptyMap();
|
||||||
}
|
}
|
||||||
return attributesResolver.resolve(attributes);
|
return attributesResolver.resolve(attributes);
|
||||||
}
|
}
|
||||||
@ -188,4 +197,9 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration
|
|||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user