|
|
|
@ -25,17 +25,17 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
|
|
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
|
|
|
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
|
|
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
|
|
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelProtect;
|
|
|
|
|
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.core.type.StandardMethodMetadata;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* PostProcessor handle @SentinelProtect Annotation, add interceptor for RestTemplate
|
|
|
|
|
* PostProcessor handle @SentinelRestTemplate Annotation, add interceptor for RestTemplate
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
|
|
|
|
|
* @see SentinelProtect
|
|
|
|
|
* @see SentinelRestTemplate
|
|
|
|
|
* @see SentinelProtectInterceptor
|
|
|
|
|
*/
|
|
|
|
|
public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProcessor {
|
|
|
|
@ -43,16 +43,16 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
|
|
|
|
@Autowired
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
private ConcurrentHashMap<String, SentinelProtect> cache = new ConcurrentHashMap<>();
|
|
|
|
|
private ConcurrentHashMap<String, SentinelRestTemplate> cache = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
|
|
|
|
|
Class<?> beanType, String beanName) {
|
|
|
|
|
if (checkSentinelProtect(beanDefinition, beanType)) {
|
|
|
|
|
SentinelProtect sentinelProtect = ((StandardMethodMetadata) beanDefinition
|
|
|
|
|
SentinelRestTemplate sentinelRestTemplate = ((StandardMethodMetadata) beanDefinition
|
|
|
|
|
.getSource()).getIntrospectedMethod()
|
|
|
|
|
.getAnnotation(SentinelProtect.class);
|
|
|
|
|
cache.put(beanName, sentinelProtect);
|
|
|
|
|
.getAnnotation(SentinelRestTemplate.class);
|
|
|
|
|
cache.put(beanName, sentinelRestTemplate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -61,26 +61,26 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
|
|
|
|
return beanType == RestTemplate.class
|
|
|
|
|
&& beanDefinition.getSource() instanceof StandardMethodMetadata
|
|
|
|
|
&& ((StandardMethodMetadata) beanDefinition.getSource())
|
|
|
|
|
.isAnnotated(SentinelProtect.class.getName());
|
|
|
|
|
.isAnnotated(SentinelRestTemplate.class.getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object postProcessAfterInitialization(Object bean, String beanName)
|
|
|
|
|
throws BeansException {
|
|
|
|
|
if (cache.containsKey(beanName)) {
|
|
|
|
|
// add interceptor for each RestTemplate with @SentinelProtect annotation
|
|
|
|
|
// add interceptor for each RestTemplate with @SentinelRestTemplate annotation
|
|
|
|
|
StringBuilder interceptorBeanName = new StringBuilder();
|
|
|
|
|
SentinelProtect sentinelProtect = cache.get(beanName);
|
|
|
|
|
SentinelRestTemplate sentinelRestTemplate = cache.get(beanName);
|
|
|
|
|
interceptorBeanName
|
|
|
|
|
.append(StringUtils.uncapitalize(
|
|
|
|
|
SentinelProtectInterceptor.class.getSimpleName()))
|
|
|
|
|
.append("_")
|
|
|
|
|
.append(sentinelProtect.blockHandlerClass().getSimpleName())
|
|
|
|
|
.append(sentinelProtect.blockHandler()).append("_")
|
|
|
|
|
.append(sentinelProtect.fallbackClass().getSimpleName())
|
|
|
|
|
.append(sentinelProtect.fallback());
|
|
|
|
|
.append(sentinelRestTemplate.blockHandlerClass().getSimpleName())
|
|
|
|
|
.append(sentinelRestTemplate.blockHandler()).append("_")
|
|
|
|
|
.append(sentinelRestTemplate.fallbackClass().getSimpleName())
|
|
|
|
|
.append(sentinelRestTemplate.fallback());
|
|
|
|
|
RestTemplate restTemplate = (RestTemplate) bean;
|
|
|
|
|
registerBean(interceptorBeanName.toString(), sentinelProtect);
|
|
|
|
|
registerBean(interceptorBeanName.toString(), sentinelRestTemplate);
|
|
|
|
|
SentinelProtectInterceptor sentinelProtectInterceptor = applicationContext
|
|
|
|
|
.getBean(interceptorBeanName.toString(),
|
|
|
|
|
SentinelProtectInterceptor.class);
|
|
|
|
@ -90,13 +90,13 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerBean(String interceptorBeanName,
|
|
|
|
|
SentinelProtect sentinelProtect) {
|
|
|
|
|
SentinelRestTemplate sentinelRestTemplate) {
|
|
|
|
|
// register SentinelProtectInterceptor bean
|
|
|
|
|
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext
|
|
|
|
|
.getAutowireCapableBeanFactory();
|
|
|
|
|
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
|
|
|
|
|
.genericBeanDefinition(SentinelProtectInterceptor.class);
|
|
|
|
|
beanDefinitionBuilder.addConstructorArgValue(sentinelProtect);
|
|
|
|
|
beanDefinitionBuilder.addConstructorArgValue(sentinelRestTemplate);
|
|
|
|
|
BeanDefinition interceptorBeanDefinition = beanDefinitionBuilder
|
|
|
|
|
.getRawBeanDefinition();
|
|
|
|
|
beanFactory.registerBeanDefinition(interceptorBeanName,
|
|
|
|
|