mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
close #482
This commit is contained in:
parent
48dc322407
commit
9f554b08ff
@ -168,9 +168,9 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
|||||||
throws BeansException {
|
throws BeansException {
|
||||||
if (cache.containsKey(beanName)) {
|
if (cache.containsKey(beanName)) {
|
||||||
// add interceptor for each RestTemplate with @SentinelRestTemplate annotation
|
// add interceptor for each RestTemplate with @SentinelRestTemplate annotation
|
||||||
StringBuilder interceptorBeanName = new StringBuilder();
|
StringBuilder interceptorBeanNamePrefix = new StringBuilder();
|
||||||
SentinelRestTemplate sentinelRestTemplate = cache.get(beanName);
|
SentinelRestTemplate sentinelRestTemplate = cache.get(beanName);
|
||||||
interceptorBeanName
|
interceptorBeanNamePrefix
|
||||||
.append(StringUtils.uncapitalize(
|
.append(StringUtils.uncapitalize(
|
||||||
SentinelProtectInterceptor.class.getSimpleName()))
|
SentinelProtectInterceptor.class.getSimpleName()))
|
||||||
.append("_")
|
.append("_")
|
||||||
@ -179,26 +179,25 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
|||||||
.append(sentinelRestTemplate.fallbackClass().getSimpleName())
|
.append(sentinelRestTemplate.fallbackClass().getSimpleName())
|
||||||
.append(sentinelRestTemplate.fallback());
|
.append(sentinelRestTemplate.fallback());
|
||||||
RestTemplate restTemplate = (RestTemplate) bean;
|
RestTemplate restTemplate = (RestTemplate) bean;
|
||||||
registerBean(interceptorBeanName.toString(), sentinelRestTemplate);
|
String interceptorBeanName = interceptorBeanNamePrefix + "@"
|
||||||
|
+ bean.toString();
|
||||||
|
registerBean(interceptorBeanName, sentinelRestTemplate, (RestTemplate) bean);
|
||||||
SentinelProtectInterceptor sentinelProtectInterceptor = applicationContext
|
SentinelProtectInterceptor sentinelProtectInterceptor = applicationContext
|
||||||
.getBean(interceptorBeanName.toString(),
|
.getBean(interceptorBeanName, SentinelProtectInterceptor.class);
|
||||||
SentinelProtectInterceptor.class);
|
|
||||||
restTemplate.getInterceptors().add(0, sentinelProtectInterceptor);
|
restTemplate.getInterceptors().add(0, sentinelProtectInterceptor);
|
||||||
}
|
}
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerBean(String interceptorBeanName,
|
private void registerBean(String interceptorBeanName,
|
||||||
SentinelRestTemplate sentinelRestTemplate) {
|
SentinelRestTemplate sentinelRestTemplate, RestTemplate restTemplate) {
|
||||||
// register SentinelProtectInterceptor bean
|
// register SentinelProtectInterceptor bean
|
||||||
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
|
||||||
.getAutowireCapableBeanFactory();
|
.getAutowireCapableBeanFactory();
|
||||||
if (beanFactory.containsBean(interceptorBeanName)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
||||||
.genericBeanDefinition(SentinelProtectInterceptor.class);
|
.genericBeanDefinition(SentinelProtectInterceptor.class);
|
||||||
beanDefinitionBuilder.addConstructorArgValue(sentinelRestTemplate);
|
beanDefinitionBuilder.addConstructorArgValue(sentinelRestTemplate);
|
||||||
|
beanDefinitionBuilder.addConstructorArgValue(restTemplate);
|
||||||
BeanDefinition interceptorBeanDefinition = beanDefinitionBuilder
|
BeanDefinition interceptorBeanDefinition = beanDefinitionBuilder
|
||||||
.getRawBeanDefinition();
|
.getRawBeanDefinition();
|
||||||
beanFactory.registerBeanDefinition(interceptorBeanName,
|
beanFactory.registerBeanDefinition(interceptorBeanName,
|
||||||
|
@ -27,11 +27,12 @@ import org.springframework.http.HttpRequest;
|
|||||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.Entry;
|
import com.alibaba.csp.sentinel.Entry;
|
||||||
|
import com.alibaba.csp.sentinel.EntryType;
|
||||||
import com.alibaba.csp.sentinel.SphU;
|
import com.alibaba.csp.sentinel.SphU;
|
||||||
import com.alibaba.csp.sentinel.Tracer;
|
import com.alibaba.csp.sentinel.Tracer;
|
||||||
import com.alibaba.csp.sentinel.context.ContextUtil;
|
|
||||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
|
||||||
|
|
||||||
@ -44,8 +45,12 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
|
|||||||
|
|
||||||
private final SentinelRestTemplate sentinelRestTemplate;
|
private final SentinelRestTemplate sentinelRestTemplate;
|
||||||
|
|
||||||
public SentinelProtectInterceptor(SentinelRestTemplate sentinelRestTemplate) {
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
public SentinelProtectInterceptor(SentinelRestTemplate sentinelRestTemplate,
|
||||||
|
RestTemplate restTemplate) {
|
||||||
this.sentinelRestTemplate = sentinelRestTemplate;
|
this.sentinelRestTemplate = sentinelRestTemplate;
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -61,32 +66,33 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
|
|||||||
entryWithPath = false;
|
entryWithPath = false;
|
||||||
}
|
}
|
||||||
Entry hostEntry = null, hostWithPathEntry = null;
|
Entry hostEntry = null, hostWithPathEntry = null;
|
||||||
ClientHttpResponse response;
|
ClientHttpResponse response = null;
|
||||||
try {
|
try {
|
||||||
ContextUtil.enter(hostWithPathResource);
|
hostEntry = SphU.entry(hostResource, EntryType.OUT);
|
||||||
if (entryWithPath) {
|
if (entryWithPath) {
|
||||||
hostWithPathEntry = SphU.entry(hostWithPathResource);
|
hostWithPathEntry = SphU.entry(hostWithPathResource, EntryType.OUT);
|
||||||
}
|
}
|
||||||
hostEntry = SphU.entry(hostResource);
|
|
||||||
response = execution.execute(request, body);
|
response = execution.execute(request, body);
|
||||||
|
if (this.restTemplate.getErrorHandler().hasError(response)) {
|
||||||
|
Tracer.trace(
|
||||||
|
new IllegalStateException("RestTemplate ErrorHandler has error"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Throwable e) {
|
catch (Throwable e) {
|
||||||
if (!BlockException.isBlockException(e)) {
|
if (!BlockException.isBlockException(e)) {
|
||||||
Tracer.trace(e);
|
Tracer.trace(e);
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return handleBlockException(request, body, execution, (BlockException) e);
|
return handleBlockException(request, body, execution, (BlockException) e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (hostEntry != null) {
|
|
||||||
hostEntry.exit();
|
|
||||||
}
|
|
||||||
if (hostWithPathEntry != null) {
|
if (hostWithPathEntry != null) {
|
||||||
hostWithPathEntry.exit();
|
hostWithPathEntry.exit();
|
||||||
}
|
}
|
||||||
ContextUtil.exit();
|
if (hostEntry != null) {
|
||||||
|
hostEntry.exit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user