mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Check and store UrlCleaner
and UrlCleanerClass
configurations of
every `SentinelResttemplate` annotation
This commit is contained in:
parent
be7dace3d1
commit
370512cafd
@ -25,6 +25,7 @@ public interface SentinelConstants {
|
||||
|
||||
String BLOCK_TYPE = "block";
|
||||
String FALLBACK_TYPE = "fallback";
|
||||
String URLCLEANER_TYPE = "urlCleaner";
|
||||
|
||||
// commercialization
|
||||
|
||||
|
@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author fangjian
|
||||
@ -29,6 +30,7 @@ final class BlockClassRegistry {
|
||||
|
||||
private static final Map<String, Method> FALLBACK_MAP = new ConcurrentHashMap<>();
|
||||
private static final Map<String, Method> BLOCK_HANDLER_MAP = new ConcurrentHashMap<>();
|
||||
private static final Map<String, Method> URL_CLEANER_MAP = new ConcurrentHashMap<>();
|
||||
|
||||
static Method lookupFallback(Class<?> clazz, String name) {
|
||||
return FALLBACK_MAP.get(getKey(clazz, name));
|
||||
@ -38,6 +40,10 @@ final class BlockClassRegistry {
|
||||
return BLOCK_HANDLER_MAP.get(getKey(clazz, name));
|
||||
}
|
||||
|
||||
static Method lookupUrlCleaner(Class<?> clazz, String name) {
|
||||
return URL_CLEANER_MAP.get(getKey(clazz, name));
|
||||
}
|
||||
|
||||
static void updateFallbackFor(Class<?> clazz, String name, Method method) {
|
||||
if (clazz == null || StringUtil.isBlank(name)) {
|
||||
throw new IllegalArgumentException("Bad argument");
|
||||
@ -52,6 +58,13 @@ final class BlockClassRegistry {
|
||||
BLOCK_HANDLER_MAP.put(getKey(clazz, name), method);
|
||||
}
|
||||
|
||||
static void updateUrlCleanerFor(Class<?> clazz, String name, Method method) {
|
||||
if (clazz == null || StringUtil.isBlank(name)) {
|
||||
throw new IllegalArgumentException("Bad argument");
|
||||
}
|
||||
URL_CLEANER_MAP.put(getKey(clazz, name), method);
|
||||
}
|
||||
|
||||
private static String getKey(Class<?> clazz, String name) {
|
||||
return String.format("%s:%s", clazz.getCanonicalName(), name);
|
||||
}
|
||||
|
@ -90,6 +90,9 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
||||
checkBlock4RestTemplate(sentinelRestTemplate.fallbackClass(),
|
||||
sentinelRestTemplate.fallback(), beanName,
|
||||
SentinelConstants.FALLBACK_TYPE);
|
||||
checkBlock4RestTemplate(sentinelRestTemplate.urlCleanerClass(),
|
||||
sentinelRestTemplate.urlCleaner(), beanName,
|
||||
SentinelConstants.URLCLEANER_TYPE);
|
||||
}
|
||||
|
||||
private void checkBlock4RestTemplate(Class<?> blockClass, String blockMethod,
|
||||
@ -111,8 +114,14 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
||||
throw new IllegalArgumentException(type + " method attribute exists but "
|
||||
+ type + " class attribute is not exists in bean[" + beanName + "]");
|
||||
}
|
||||
Class[] args = new Class[] { HttpRequest.class, byte[].class,
|
||||
ClientHttpRequestExecution.class, BlockException.class };
|
||||
Class[] args;
|
||||
if (type.equals(SentinelConstants.URLCLEANER_TYPE)) {
|
||||
args = new Class[] { String.class };
|
||||
}
|
||||
else {
|
||||
args = new Class[] { HttpRequest.class, byte[].class,
|
||||
ClientHttpRequestExecution.class, BlockException.class };
|
||||
}
|
||||
String argsStr = Arrays.toString(
|
||||
Arrays.stream(args).map(clazz -> clazz.getSimpleName()).toArray());
|
||||
Method foundMethod = ClassUtils.getStaticMethod(blockClass, blockMethod, args);
|
||||
@ -127,10 +136,18 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
||||
+ ", please check your class name, method name and arguments");
|
||||
}
|
||||
|
||||
if (!ClientHttpResponse.class.isAssignableFrom(foundMethod.getReturnType())) {
|
||||
log.error(
|
||||
"{} method return value in bean[{}] is not ClientHttpResponse: {}#{}{}",
|
||||
type, beanName, blockClass.getName(), blockMethod, argsStr);
|
||||
Class<?> standardReturnType;
|
||||
if (type.equals(SentinelConstants.URLCLEANER_TYPE)) {
|
||||
standardReturnType = String.class;
|
||||
}
|
||||
else {
|
||||
standardReturnType = ClientHttpResponse.class;
|
||||
}
|
||||
|
||||
if (!standardReturnType.isAssignableFrom(foundMethod.getReturnType())) {
|
||||
log.error("{} method return value in bean[{}] is not {}: {}#{}{}", type,
|
||||
beanName, standardReturnType.getName(), blockClass.getName(),
|
||||
blockMethod, argsStr);
|
||||
throw new IllegalArgumentException(type + " method return value in bean["
|
||||
+ beanName + "] is not ClientHttpResponse: " + blockClass.getName()
|
||||
+ "#" + blockMethod + argsStr);
|
||||
@ -139,9 +156,12 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
||||
BlockClassRegistry.updateBlockHandlerFor(blockClass, blockMethod,
|
||||
foundMethod);
|
||||
}
|
||||
else {
|
||||
else if (type.equals(SentinelConstants.FALLBACK_TYPE)) {
|
||||
BlockClassRegistry.updateFallbackFor(blockClass, blockMethod, foundMethod);
|
||||
}
|
||||
else {
|
||||
BlockClassRegistry.updateUrlCleanerFor(blockClass, blockMethod, foundMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkSentinelProtect(RootBeanDefinition beanDefinition,
|
||||
@ -177,7 +197,9 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
||||
.append(sentinelRestTemplate.blockHandlerClass().getSimpleName())
|
||||
.append(sentinelRestTemplate.blockHandler()).append("_")
|
||||
.append(sentinelRestTemplate.fallbackClass().getSimpleName())
|
||||
.append(sentinelRestTemplate.fallback());
|
||||
.append(sentinelRestTemplate.fallback()).append("_")
|
||||
.append(sentinelRestTemplate.urlCleanerClass().getSimpleName())
|
||||
.append(sentinelRestTemplate.urlCleaner());
|
||||
RestTemplate restTemplate = (RestTemplate) bean;
|
||||
String interceptorBeanName = interceptorBeanNamePrefix + "@"
|
||||
+ bean.toString();
|
||||
|
Loading…
x
Reference in New Issue
Block a user