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#549 : LoadBalancerInterceptor bean may be missing
This commit is contained in:
parent
be521de748
commit
ffcd829a99
@ -35,6 +35,7 @@ import org.springframework.cloud.alibaba.dubbo.service.DubboGenericServiceFactor
|
|||||||
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.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.event.EventListener;
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
@ -56,7 +57,7 @@ import java.util.Map;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(name = {"org.springframework.web.client.RestTemplate"})
|
@ConditionalOnClass(name = {"org.springframework.web.client.RestTemplate"})
|
||||||
@AutoConfigureAfter(name = {"org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration"})
|
@AutoConfigureAfter(name = {"org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration"})
|
||||||
public class DubboLoadBalancedRestTemplateAutoConfiguration implements BeanClassLoaderAware {
|
public class DubboLoadBalancedRestTemplateAutoConfiguration implements BeanClassLoaderAware, SmartInitializingSingleton {
|
||||||
|
|
||||||
private static final Class<DubboTransported> DUBBO_TRANSPORTED_CLASS = DubboTransported.class;
|
private static final Class<DubboTransported> DUBBO_TRANSPORTED_CLASS = DubboTransported.class;
|
||||||
|
|
||||||
@ -65,9 +66,12 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration implements BeanClass
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DubboServiceMetadataRepository repository;
|
private DubboServiceMetadataRepository repository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired(required = false)
|
||||||
private LoadBalancerInterceptor loadBalancerInterceptor;
|
private LoadBalancerInterceptor loadBalancerInterceptor;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private RetryLoadBalancerInterceptor retryLoadBalancerInterceptor;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ConfigurableListableBeanFactory beanFactory;
|
private ConfigurableListableBeanFactory beanFactory;
|
||||||
|
|
||||||
@ -86,6 +90,18 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration implements BeanClass
|
|||||||
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link ClientHttpRequestInterceptor} bean that may be {@link LoadBalancerInterceptor} or {@link RetryLoadBalancerInterceptor}
|
||||||
|
*/
|
||||||
|
private ClientHttpRequestInterceptor loadBalancerInterceptorBean;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterSingletonsInstantiated() {
|
||||||
|
loadBalancerInterceptorBean = retryLoadBalancerInterceptor != null ?
|
||||||
|
retryLoadBalancerInterceptor :
|
||||||
|
loadBalancerInterceptor;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapt the {@link RestTemplate} beans that are annotated {@link LoadBalanced @LoadBalanced} and
|
* Adapt the {@link RestTemplate} beans that are annotated {@link LoadBalanced @LoadBalanced} and
|
||||||
@ -140,7 +156,7 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration implements BeanClass
|
|||||||
|
|
||||||
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(restTemplate.getInterceptors());
|
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(restTemplate.getInterceptors());
|
||||||
|
|
||||||
int index = interceptors.indexOf(loadBalancerInterceptor);
|
int index = loadBalancerInterceptorBean == null ? -1 : interceptors.indexOf(loadBalancerInterceptorBean);
|
||||||
|
|
||||||
index = index < 0 ? 0 : index;
|
index = index < 0 ? 0 : index;
|
||||||
|
|
||||||
@ -157,4 +173,5 @@ public class DubboLoadBalancedRestTemplateAutoConfiguration implements BeanClass
|
|||||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,18 @@
|
|||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Cloud Open Feign -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Retry -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.retry</groupId>
|
||||||
|
<artifactId>spring-retry</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Sample API -->
|
<!-- Sample API -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
@ -28,12 +40,6 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Spring Cloud Open Feign -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user