1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

fix duplicate sentinel interceptor registration

This commit is contained in:
fangjian0423 2019-03-18 22:53:39 +08:00
parent fc724a5056
commit 3f15f7388b
2 changed files with 33 additions and 4 deletions

View File

@ -16,7 +16,9 @@
package org.springframework.cloud.alibaba.sentinel.custom;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -38,9 +40,7 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.csp.sentinel.slots.block.BlockException;
/**
* PostProcessor handle @SentinelRestTemplate Annotation, add interceptor for RestTemplate
@ -193,6 +193,9 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
// register SentinelProtectInterceptor bean
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
.getAutowireCapableBeanFactory();
if (beanFactory.containsBean(interceptorBeanName)) {
return;
}
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
.genericBeanDefinition(SentinelProtectInterceptor.class);
beanDefinitionBuilder.addConstructorArgValue(sentinelRestTemplate);

View File

@ -85,6 +85,11 @@ public class SentinelRestTemplateTests {
new AnnotationConfigApplicationContext(TestConfig9.class);
}
@Test
public void testNormalWithoutParam() {
new AnnotationConfigApplicationContext(TestConfig10.class);
}
@Configuration
public static class TestConfig1 {
@Bean
@ -220,6 +225,27 @@ public class SentinelRestTemplateTests {
}
}
@Configuration
public static class TestConfig10 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
@SentinelRestTemplate
RestTemplate restTemplate2() {
return new RestTemplate();
}
}
public static class ExceptionUtil {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {