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

Polish alibaba/spring-cloud-alibaba#1758 : [CodeBase] Sync the source code from greenwich to finichley

This commit is contained in:
mercyblitz
2020-09-21 15:58:45 +08:00
parent 201a4eaa01
commit 8cbd14b2bb
71 changed files with 341 additions and 376 deletions

View File

@@ -27,16 +27,14 @@ import com.alibaba.csp.sentinel.util.StringUtil;
*/
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<>();
private 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));
}

View File

@@ -154,8 +154,8 @@ public class SentinelAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public SentinelDataSourceHandler sentinelDataSourceHandler(
DefaultListableBeanFactory beanFactory, SentinelProperties sentinelProperties,
Environment env) {
DefaultListableBeanFactory beanFactory, SentinelProperties sentinelProperties,
Environment env) {
return new SentinelDataSourceHandler(beanFactory, sentinelProperties, env);
}

View File

@@ -56,16 +56,15 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
.getLogger(SentinelBeanPostProcessor.class);
private final ApplicationContext applicationContext;
private ConcurrentHashMap<String, SentinelRestTemplate> cache = new ConcurrentHashMap<>();
public SentinelBeanPostProcessor(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
private ConcurrentHashMap<String, SentinelRestTemplate> cache = new ConcurrentHashMap<>();
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition,
Class<?> beanType, String beanName) {
Class<?> beanType, String beanName) {
if (checkSentinelProtect(beanDefinition, beanType, beanName)) {
SentinelRestTemplate sentinelRestTemplate;
if (beanDefinition.getSource() instanceof StandardMethodMetadata) {
@@ -166,7 +165,7 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
}
private boolean checkSentinelProtect(RootBeanDefinition beanDefinition,
Class<?> beanType, String beanName) {
Class<?> beanType, String beanName) {
return beanName != null && beanType == RestTemplate.class
&& checkMethodMetadataReadingVisitor(beanDefinition);
}

View File

@@ -52,23 +52,16 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
private static final Logger log = LoggerFactory
.getLogger(SentinelDataSourceHandler.class);
private final String DATA_TYPE_FIELD = "dataType";
private final String CUSTOM_DATA_TYPE = "custom";
private final String CONVERTER_CLASS_FIELD = "converterClass";
private final DefaultListableBeanFactory beanFactory;
private final SentinelProperties sentinelProperties;
private final Environment env;
private List<String> dataTypeList = Arrays.asList("json", "xml");
private final String DATA_TYPE_FIELD = "dataType";
private final String CUSTOM_DATA_TYPE = "custom";
private final String CONVERTER_CLASS_FIELD = "converterClass";
private final DefaultListableBeanFactory beanFactory;
private final SentinelProperties sentinelProperties;
private final Environment env;
public SentinelDataSourceHandler(DefaultListableBeanFactory beanFactory,
SentinelProperties sentinelProperties, Environment env) {
SentinelProperties sentinelProperties, Environment env) {
this.beanFactory = beanFactory;
this.sentinelProperties = sentinelProperties;
this.env = env;

View File

@@ -55,7 +55,7 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
ClientHttpRequestExecution execution) throws IOException {
URI uri = request.getURI();
String hostResource = request.getMethod().toString() + ":" + uri.getScheme()
+ "://" + uri.getHost()
@@ -107,7 +107,7 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
}
private ClientHttpResponse handleBlockException(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution, BlockException ex) {
ClientHttpRequestExecution execution, BlockException ex) {
Object[] args = new Object[] { request, body, execution, ex };
// handle degrade
if (isDegradeFailure(ex)) {

View File

@@ -63,7 +63,7 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
private SentinelProperties sentinelProperties;
public SentinelHealthIndicator(DefaultListableBeanFactory beanFactory,
SentinelProperties sentinelProperties) {
SentinelProperties sentinelProperties) {
this.beanFactory = beanFactory;
this.sentinelProperties = sentinelProperties;
}
@@ -76,7 +76,7 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// detail
if (!sentinelProperties.isEnabled()) {
detailMap.put("enabled", false);
builder.up().withDetails(detailMap);
withDetails(builder.up(), detailMap);
return;
}
@@ -144,11 +144,16 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// If Dashboard and DataSource are both OK, the health status is UP
if (dashboardUp && dataSourceUp) {
builder.up().withDetails(detailMap);
withDetails(builder.up(), detailMap);
}
else {
builder.unknown().withDetails(detailMap);
withDetails(builder.down(), detailMap);
}
}
private void withDetails(Health.Builder builder, Map<String, Object> detailMap) {
for (String key : detailMap.keySet()) {
builder.withDetail(key, detailMap.get(key));
}
}
}

View File

@@ -32,13 +32,12 @@ import feign.MethodMetadata;
*/
public class SentinelContractHolder implements Contract {
private final Contract delegate;
/**
* map key is constructed by ClassFullName + configKey. configKey is constructed by
* {@link feign.Feign#configKey}
*/
public final static Map<String, MethodMetadata> METADATA_MAP = new HashMap<>();
private final Contract delegate;
public SentinelContractHolder(Contract delegate) {
this.delegate = delegate;

View File

@@ -76,7 +76,7 @@ public final class SentinelFeign {
super.invocationHandlerFactory(new InvocationHandlerFactory() {
@Override
public InvocationHandler create(Target target,
Map<Method, MethodHandler> dispatch) {
Map<Method, MethodHandler> dispatch) {
// using reflect get fallback and fallbackFactory properties from
// FeignClientFactoryBean because FeignClientFactoryBean is a package
// level class, we can not use it in our package

View File

@@ -53,7 +53,7 @@ public class SentinelInvocationHandler implements InvocationHandler {
private Map<Method, Method> fallbackMethodMap;
SentinelInvocationHandler(Target<?> target, Map<Method, MethodHandler> dispatch,
FallbackFactory fallbackFactory) {
FallbackFactory fallbackFactory) {
this.target = checkNotNull(target, "target");
this.dispatch = checkNotNull(dispatch, "dispatch");
this.fallbackFactory = fallbackFactory;
@@ -65,13 +65,23 @@ public class SentinelInvocationHandler implements InvocationHandler {
this.dispatch = checkNotNull(dispatch, "dispatch");
}
static Map<Method, Method> toFallbackMethod(Map<Method, MethodHandler> dispatch) {
Map<Method, Method> result = new LinkedHashMap<>();
for (Method method : dispatch.keySet()) {
method.setAccessible(true);
result.put(method, method);
}
return result;
}
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args)
throws Throwable {
if ("equals".equals(method.getName())) {
try {
Object otherHandler = args.length > 0 && args[0] != null
? Proxy.getInvocationHandler(args[0]) : null;
? Proxy.getInvocationHandler(args[0])
: null;
return equals(otherHandler);
}
catch (IllegalArgumentException e) {
@@ -99,7 +109,7 @@ public class SentinelInvocationHandler implements InvocationHandler {
}
else {
String resourceName = methodMetadata.template().method().toUpperCase()
+ ":" + hardCodedTarget.url() + methodMetadata.template().path();
+ ":" + hardCodedTarget.url() + methodMetadata.template().url();
Entry entry = null;
try {
ContextUtil.enter(resourceName);
@@ -166,13 +176,4 @@ public class SentinelInvocationHandler implements InvocationHandler {
return target.toString();
}
static Map<Method, Method> toFallbackMethod(Map<Method, MethodHandler> dispatch) {
Map<Method, Method> result = new LinkedHashMap<>();
for (Method method : dispatch.keySet()) {
method.setAccessible(true);
result.put(method, method);
}
return result;
}
}

View File

@@ -61,14 +61,6 @@ public class ContextIdSentinelFeignTests {
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ SentinelFeignAutoConfiguration.class })
@EnableFeignClients
public static class TestConfig {
}
@FeignClient(contextId = "echoService", name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {
@@ -85,6 +77,14 @@ public class ContextIdSentinelFeignTests {
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ SentinelFeignAutoConfiguration.class })
@EnableFeignClients
public static class TestConfig {
}
public static class FeignConfiguration {
@Bean

View File

@@ -232,7 +232,7 @@ public class SentinelAutoConfigurationTests {
@Bean
@SentinelRestTemplate
RestTemplate restTemplate() {
RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(mock(ClientHttpRequestInterceptor.class));
return restTemplate;
@@ -240,18 +240,18 @@ public class SentinelAutoConfigurationTests {
@Bean
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException")
RestTemplate restTemplateWithBlockClass() {
RestTemplate restTemplateWithBlockClass() {
return new RestTemplate();
}
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class, fallback = "fallbackException")
RestTemplate restTemplateWithFallbackClass() {
RestTemplate restTemplateWithFallbackClass() {
return new RestTemplate();
}
@Bean
RestTemplate restTemplateWithoutBlockClass() {
RestTemplate restTemplateWithoutBlockClass() {
return new RestTemplate();
}
@@ -260,13 +260,13 @@ public class SentinelAutoConfigurationTests {
public static class ExceptionUtil {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("Oops");
}
public static SentinelClientHttpResponse fallbackException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("Oops fallback");
}

View File

@@ -118,24 +118,6 @@ public class SentinelFeignTests {
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ SentinelFeignAutoConfiguration.class })
@EnableFeignClients
public static class TestConfig {
@Bean
public EchoServiceFallback echoServiceFallback() {
return new EchoServiceFallback();
}
@Bean
public CustomFallbackFactory customFallbackFactory() {
return new CustomFallbackFactory();
}
}
@FeignClient(value = "test-service", fallback = EchoServiceFallback.class)
public interface EchoService {
@@ -172,6 +154,24 @@ public class SentinelFeignTests {
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration({ SentinelFeignAutoConfiguration.class })
@EnableFeignClients
public static class TestConfig {
@Bean
public EchoServiceFallback echoServiceFallback() {
return new EchoServiceFallback();
}
@Bean
public CustomFallbackFactory customFallbackFactory() {
return new CustomFallbackFactory();
}
}
public static class EchoServiceFallback implements EchoService {
@Override

View File

@@ -114,14 +114,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig1 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(fallback = "fbk")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -131,14 +131,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig2 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class)
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -148,14 +148,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig3 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(blockHandler = "blk")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -165,14 +165,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig4 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class)
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -182,14 +182,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig5 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException", fallbackClass = ExceptionUtil.class, fallback = "fallbackException", urlCleanerClass = UrlCleanUtil.class, urlCleaner = "clean")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -199,14 +199,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig6 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException1")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -216,14 +216,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig7 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class, fallback = "fallbackException1")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -233,14 +233,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig8 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException2")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -250,14 +250,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig9 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class, fallback = "fallbackException2")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -267,20 +267,20 @@ public class SentinelRestTemplateTests {
public static class TestConfig10 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
@SentinelRestTemplate
RestTemplate restTemplate2() {
RestTemplate restTemplate2() {
return new RestTemplate();
}
@@ -290,14 +290,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig11 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(urlCleaner = "cln")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -307,14 +307,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig12 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(urlCleanerClass = UrlCleanUtil.class)
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -324,14 +324,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig13 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(urlCleanerClass = UrlCleanUtil.class, urlCleaner = "clean1")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -341,14 +341,14 @@ public class SentinelRestTemplateTests {
public static class TestConfig14 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
}
@Bean
@SentinelRestTemplate(urlCleanerClass = UrlCleanUtil.class, urlCleaner = "clean2")
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}
@@ -357,24 +357,24 @@ public class SentinelRestTemplateTests {
public static class ExceptionUtil {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("Oops");
}
public static void handleException2(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution, BlockException ex) {
ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
}
public static SentinelClientHttpResponse fallbackException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("Oops fallback");
}
public static void fallbackException2(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution, BlockException ex) {
ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
}