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

format code with maven plugins

This commit is contained in:
fangjian0423
2019-09-26 17:15:41 +08:00
parent ed6942d9df
commit 2435167e1e
529 changed files with 6304 additions and 5164 deletions

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,17 +19,30 @@ package com.alibaba.cloud.sentinel;
/**
* @author fangjian
*/
public interface SentinelConstants {
public final class SentinelConstants {
String PROPERTY_PREFIX = "spring.cloud.sentinel";
/**
* Prefix of {@link SentinelProperties}.
*/
public static final String PROPERTY_PREFIX = "spring.cloud.sentinel";
String BLOCK_TYPE = "block";
String FALLBACK_TYPE = "fallback";
String URLCLEANER_TYPE = "urlCleaner";
/**
* Block type.
*/
public static final String BLOCK_TYPE = "block";
// commercialization
/**
* Fallback type.
*/
public static final String FALLBACK_TYPE = "fallback";
String FLOW_DATASOURCE_NAME = "edas-flow";
String DEGRADE_DATASOURCE_NAME = "edas-degrade";
/**
* UrlCleaner type.
*/
public static final String URLCLEANER_TYPE = "urlCleaner";
private SentinelConstants() {
throw new AssertionError("Must not instantiate constant utility class");
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,15 +20,15 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.validation.annotation.Validated;
import com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered;
import org.springframework.validation.annotation.Validated;
/**
* {@link ConfigurationProperties} for Sentinel.
*
@@ -89,9 +89,9 @@ public class SentinelProperties {
*/
private Log log = new Log();
/**
* Add HTTP method prefix for Sentinel Resource.
*/
/**
* Add HTTP method prefix for Sentinel Resource.
*/
private Boolean httpMethodSpecify = false;
public boolean isEager() {
@@ -166,15 +166,15 @@ public class SentinelProperties {
this.log = log;
}
public Boolean getHttpMethodSpecify() {
return httpMethodSpecify;
}
public Boolean getHttpMethodSpecify() {
return httpMethodSpecify;
}
public void setHttpMethodSpecify(Boolean httpMethodSpecify) {
this.httpMethodSpecify = httpMethodSpecify;
}
public void setHttpMethodSpecify(Boolean httpMethodSpecify) {
this.httpMethodSpecify = httpMethodSpecify;
}
public static class Flow {
public static class Flow {
/**
* The cold factor {@link SentinelConfig#COLD_FACTOR}.
@@ -205,6 +205,7 @@ public class SentinelProperties {
public void setBlockPage(String blockPage) {
this.blockPage = blockPage;
}
}
public static class Metric {
@@ -248,6 +249,7 @@ public class SentinelProperties {
public void setCharset(String charset) {
this.charset = charset;
}
}
public static class Transport {
@@ -306,6 +308,7 @@ public class SentinelProperties {
public void setClientIp(String clientIp) {
this.clientIp = clientIp;
}
}
public static class Filter {
@@ -316,7 +319,7 @@ public class SentinelProperties {
private int order = Ordered.HIGHEST_PRECEDENCE;
/**
* URL pattern for sentinel filter, default is /*
* URL pattern for sentinel filter, default is /*.
*/
private List<String> urlPatterns;
@@ -349,6 +352,7 @@ public class SentinelProperties {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
public static class Log {

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,8 +23,14 @@ import java.util.Optional;
import javax.annotation.PostConstruct;
import javax.servlet.Filter;
import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -35,12 +41,6 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
/**
* @author xiaojing
*/
@@ -74,7 +74,8 @@ public class SentinelWebAutoConfiguration {
}
@Bean
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled", matchIfMissing = true)
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
matchIfMissing = true)
public FilterRegistrationBean sentinelFilter() {
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
@@ -100,4 +101,4 @@ public class SentinelWebAutoConfiguration {
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,8 +22,14 @@ import java.util.Optional;
import javax.annotation.PostConstruct;
import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
import com.alibaba.csp.sentinel.adapter.spring.webflux.SentinelWebFluxFilter;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager;
import com.alibaba.csp.sentinel.adapter.spring.webflux.exception.SentinelBlockExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -37,12 +43,6 @@ import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
import com.alibaba.csp.sentinel.adapter.spring.webflux.SentinelWebFluxFilter;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager;
import com.alibaba.csp.sentinel.adapter.spring.webflux.exception.SentinelBlockExceptionHandler;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@@ -57,6 +57,7 @@ public class SentinelWebFluxAutoConfiguration {
.getLogger(SentinelWebFluxAutoConfiguration.class);
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
@Autowired
@@ -76,17 +77,19 @@ public class SentinelWebFluxAutoConfiguration {
@Bean
@Order(-2)
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled", matchIfMissing = true)
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
matchIfMissing = true)
public SentinelBlockExceptionHandler sentinelBlockExceptionHandler() {
return new SentinelBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(-1)
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled", matchIfMissing = true)
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
matchIfMissing = true)
public SentinelWebFluxFilter sentinelWebFluxFilter() {
log.info("[Sentinel Starter] register Sentinel SentinelWebFluxFilter");
return new SentinelWebFluxFilter();
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -41,4 +41,5 @@ public @interface SentinelRestTemplate {
String urlCleaner() default "";
Class<?> urlCleanerClass() default void.class;
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,15 +21,20 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.csp.sentinel.util.StringUtil;
import org.springframework.util.StringUtils;
/**
* @author fangjian
*/
final class BlockClassRegistry {
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) {

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,19 +18,6 @@ package com.alibaba.cloud.sentinel.custom;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
@@ -46,11 +33,23 @@ import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/**
* @author xiaojing
* @author jiashuai.xie
@@ -145,7 +144,8 @@ public class SentinelAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
@ConditionalOnProperty(name = "resttemplate.sentinel.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(name = "resttemplate.sentinel.enabled", havingValue = "true",
matchIfMissing = true)
public SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
return new SentinelBeanPostProcessor(applicationContext);
@@ -237,6 +237,7 @@ public class SentinelAutoConfiguration {
}
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -20,8 +20,12 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.cloud.sentinel.SentinelConstants;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -38,12 +42,9 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.sentinel.SentinelConstants;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.csp.sentinel.slots.block.BlockException;
/**
* PostProcessor handle @SentinelRestTemplate Annotation, add interceptor for RestTemplate
* PostProcessor handle @SentinelRestTemplate Annotation, add interceptor for
* RestTemplate.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @see SentinelRestTemplate
@@ -219,4 +220,4 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
interceptorBeanDefinition);
}
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.sentinel.custom;
import org.springframework.context.annotation.Configuration;
@@ -9,4 +25,5 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration
public class SentinelCircuitBreakerConfiguration {
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.sentinel.custom;
import java.lang.reflect.Field;
@@ -7,8 +23,15 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties;
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -16,16 +39,9 @@ import org.springframework.core.env.Environment;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties;
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
/**
* Sentinel {@link ReadableDataSource} Handler Handle the configurations of
* 'spring.cloud.sentinel.datasource'
* 'spring.cloud.sentinel.datasource'.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @see SentinelProperties#datasource
@@ -40,7 +56,9 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
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;

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,12 +21,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.Entry;
@@ -36,8 +30,14 @@ import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RestTemplate;
/**
* Interceptor using by SentinelRestTemplate
* Interceptor using by SentinelRestTemplate.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@@ -73,7 +73,8 @@ public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor
hostWithPathResource);
}
Entry hostEntry = null, hostWithPathEntry = null;
Entry hostEntry = null;
Entry hostWithPathEntry = null;
ClientHttpResponse response = null;
try {
hostEntry = SphU.entry(hostResource, EntryType.OUT);

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,9 +19,6 @@ package com.alibaba.cloud.sentinel.endpoint;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.config.SentinelConfig;
@@ -34,8 +31,12 @@ import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
/**
* Endpoint for Sentinel, contains ans properties and rules
* Endpoint for Sentinel, contains ans properties and rules.
*
* @author xiaojing
*/
@Endpoint(id = "sentinel")

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,6 +16,8 @@
package com.alibaba.cloud.sentinel.endpoint;
import com.alibaba.cloud.sentinel.SentinelProperties;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
@@ -25,8 +27,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import com.alibaba.cloud.sentinel.SentinelProperties;
/**
* @author hengyunabc
*/
@@ -49,4 +49,5 @@ public class SentinelEndpointAutoConfiguration {
SentinelProperties sentinelProperties) {
return new SentinelHealthIndicator(beanFactory, sentinelProperties);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,6 +19,12 @@ package com.alibaba.cloud.sentinel.endpoint;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
@@ -26,12 +32,6 @@ import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.util.StringUtils;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
/**
* A {@link HealthIndicator} for Sentinel, which checks the status of Sentinel Dashboard
* and DataSource.
@@ -145,4 +145,5 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
builder.down().withDetails(detailMap);
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -26,7 +26,7 @@ import feign.MethodMetadata;
/**
*
* Using static field {@link SentinelContractHolder#METADATA_MAP} to hold
* {@link MethodMetadata} data
* {@link MethodMetadata} data.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,13 +21,6 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import feign.Contract;
import feign.Feign;
import feign.InvocationHandlerFactory;
@@ -35,12 +28,23 @@ import feign.Target;
import feign.hystrix.FallbackFactory;
import feign.hystrix.HystrixFeign;
import org.springframework.beans.BeansException;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* {@link Feign.Builder} like {@link HystrixFeign.Builder}
* {@link Feign.Builder} like {@link HystrixFeign.Builder}.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class SentinelFeign {
public final class SentinelFeign {
private SentinelFeign() {
}
public static Builder builder() {
return new Builder();
@@ -149,6 +153,7 @@ public class SentinelFeign {
this.applicationContext = applicationContext;
feignContext = this.applicationContext.getBean(FeignContext.class);
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,6 +16,9 @@
package com.alibaba.cloud.sentinel.feign;
import com.alibaba.csp.sentinel.SphU;
import feign.Feign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -23,10 +26,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.alibaba.csp.sentinel.SphU;
import feign.Feign;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,8 +16,6 @@
package com.alibaba.cloud.sentinel.feign;
import static feign.Util.checkNotNull;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -31,24 +29,27 @@ import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import feign.Feign;
import feign.InvocationHandlerFactory.MethodHandler;
import feign.MethodMetadata;
import feign.Target;
import feign.hystrix.FallbackFactory;
import static feign.Util.checkNotNull;
/**
* {@link InvocationHandler} handle invocation that protected by Sentinel
* {@link InvocationHandler} handle invocation that protected by Sentinel.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class SentinelInvocationHandler implements InvocationHandler {
private final Target<?> target;
private final Map<Method, MethodHandler> dispatch;
private FallbackFactory fallbackFactory;
private Map<Method, Method> fallbackMethodMap;
SentinelInvocationHandler(Target<?> target, Map<Method, MethodHandler> dispatch,
@@ -70,8 +71,7 @@ public class SentinelInvocationHandler implements InvocationHandler {
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) {

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -24,16 +24,16 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.custom.SentinelProtectInterceptor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.AbstractClientHttpResponse;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.custom.SentinelProtectInterceptor;
/**
* Using by {@link SentinelRestTemplate} and {@link SentinelProtectInterceptor}
* Using by {@link SentinelRestTemplate} and {@link SentinelProtectInterceptor}.
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@@ -77,4 +77,5 @@ public class SentinelClientHttpResponse extends AbstractClientHttpResponse {
httpHeaders.putAll(headers);
return httpHeaders;
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,13 +16,10 @@
package com.alibaba.cloud.sentinel;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.junit.Before;
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@@ -37,19 +34,16 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Add this unit test to verify https://github.com/alibaba/spring-cloud-alibaba/pull/838
*
*
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ContextIdSentinelFeignTests.TestConfig.class }, properties = {
"feign.sentinel.enabled=true" })
@SpringBootTest(classes = { ContextIdSentinelFeignTests.TestConfig.class },
properties = { "feign.sentinel.enabled=true" })
public class ContextIdSentinelFeignTests {
@Autowired
@@ -60,16 +54,11 @@ public class ContextIdSentinelFeignTests {
@Test
public void testFeignClient() {
assertEquals("Sentinel Feign Client fallback success", "echo fallback",
echoService.echo("test"));
assertEquals("Sentinel Feign Client fallbackFactory success", "foo fallback",
fooService.echo("test"));
assertNotEquals("ToString method invoke was not in SentinelInvocationHandler",
echoService.toString(), fooService.toString());
assertNotEquals("HashCode method invoke was not in SentinelInvocationHandler",
echoService.hashCode(), fooService.hashCode());
assertFalse("Equals method invoke was not in SentinelInvocationHandler",
echoService.equals(fooService));
assertThat(echoService.echo("test")).isEqualTo("echo fallback");
assertThat(fooService.echo("test")).isEqualTo("foo fallback");
assertThat(fooService.toString()).isNotEqualTo(echoService.toString());
assertThat(fooService.hashCode()).isNotEqualTo(echoService.hashCode());
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
}
@Configuration
@@ -80,16 +69,24 @@ public class ContextIdSentinelFeignTests {
}
@FeignClient(contextId = "echoService", name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
@FeignClient(contextId = "echoService", name = "service-provider",
fallback = EchoServiceFallback.class,
configuration = FeignConfiguration.class)
public interface EchoService {
@GetMapping(value = "/echo/{str}")
@GetMapping("/echo/{str}")
String echo(@PathVariable("str") String str);
}
@FeignClient(contextId = "fooService", value = "foo-service", fallbackFactory = CustomFallbackFactory.class, configuration = FeignConfiguration.class)
@FeignClient(contextId = "fooService", value = "foo-service",
fallbackFactory = CustomFallbackFactory.class,
configuration = FeignConfiguration.class)
public interface FooService {
@RequestMapping(path = "echo/{str}")
String echo(@RequestParam("str") String param);
}
public static class FeignConfiguration {
@@ -121,6 +118,7 @@ public class ContextIdSentinelFeignTests {
public String echo(@RequestParam("str") String param) {
return "foo fallback";
}
}
public static class CustomFallbackFactory
@@ -132,6 +130,7 @@ public class ContextIdSentinelFeignTests {
public FooService create(Throwable throwable) {
return fooService;
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,18 +16,28 @@
package com.alibaba.cloud.sentinel;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import java.util.Arrays;
import java.util.Map;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@@ -45,30 +55,18 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
* @author jiashuai.xie
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
SentinelAutoConfigurationTests.TestConfig.class }, properties = {
"spring.cloud.sentinel.filter.order=123",
@SpringBootTest(classes = { SentinelAutoConfigurationTests.TestConfig.class },
properties = { "spring.cloud.sentinel.filter.order=123",
"spring.cloud.sentinel.filter.urlPatterns=/*,/test",
"spring.cloud.sentinel.metric.fileSingleSize=9999",
"spring.cloud.sentinel.metric.fileTotalCount=100",
@@ -79,7 +77,8 @@ import com.alibaba.csp.sentinel.transport.config.TransportConfig;
"spring.cloud.sentinel.transport.dashboard=http://localhost:8080",
"spring.cloud.sentinel.transport.port=9999",
"spring.cloud.sentinel.transport.clientIp=1.1.1.1",
"spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" }, webEnvironment = RANDOM_PORT)
"spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" },
webEnvironment = RANDOM_PORT)
public class SentinelAutoConfigurationTests {
@Autowired
@@ -131,10 +130,10 @@ public class SentinelAutoConfigurationTests {
@Test
public void contextLoads() throws Exception {
assertNotNull("FilterRegistrationBean was not created", filterRegistrationBean);
assertNotNull("SentinelProperties was not created", sentinelProperties);
assertNotNull("SentinelBeanPostProcessor was not created",
sentinelBeanPostProcessor);
assertThat(filterRegistrationBean).isNotNull();
assertThat(filterRegistrationBean).isNotNull();
assertThat(sentinelBeanPostProcessor).isNotNull();
checkSentinelLog();
checkSentinelEager();
@@ -148,139 +147,110 @@ public class SentinelAutoConfigurationTests {
private void checkEndpoint() {
SentinelEndpoint sentinelEndpoint = new SentinelEndpoint(sentinelProperties);
Map<String, Object> map = sentinelEndpoint.invoke();
assertEquals("Endpoint Sentinel log pid was wrong", true, map.get("logUsePid"));
assertEquals("Endpoint Sentinel transport console server was wrong",
"http://localhost:8080", map.get("consoleServer"));
assertEquals("Endpoint Sentinel transport port was wrong", "9999",
map.get("clientPort"));
assertEquals("Endpoint Sentinel transport heartbeatIntervalMs was wrong", 20000l,
map.get("heartbeatIntervalMs"));
assertEquals("Endpoint Sentinel transport clientIp was wrong", "1.1.1.1",
map.get("clientIp"));
assertEquals("Endpoint Sentinel metric file size was wrong", 9999l,
map.get("metricsFileSize"));
assertEquals("Endpoint Sentinel metric file count was wrong", 100,
map.get("totalMetricsFileCount"));
assertEquals("Endpoint Sentinel metric file charset was wrong", "UTF-8",
map.get("metricsFileCharset"));
assertEquals("Endpoint Sentinel block page was wrong", "/error",
map.get("blockPage"));
assertThat(map.get("logUsePid")).isEqualTo(Boolean.TRUE);
assertThat(map.get("consoleServer")).isEqualTo("http://localhost:8080");
assertThat(map.get("clientPort")).isEqualTo("9999");
assertThat(map.get("heartbeatIntervalMs")).isEqualTo(20000L);
assertThat(map.get("clientIp")).isEqualTo("1.1.1.1");
assertThat(map.get("metricsFileSize")).isEqualTo(9999L);
assertThat(map.get("totalMetricsFileCount")).isEqualTo(100);
assertThat(map.get("metricsFileCharset")).isEqualTo("UTF-8");
assertThat(map.get("blockPage")).isEqualTo("/error");
}
private void checkSentinelFilter() {
assertEquals("SentinelProperties filter order was wrong", 123,
sentinelProperties.getFilter().getOrder());
assertEquals("SentinelProperties filter url pattern size was wrong", 2,
sentinelProperties.getFilter().getUrlPatterns().size());
assertEquals("SentinelProperties filter url pattern item was wrong", "/*",
sentinelProperties.getFilter().getUrlPatterns().get(0));
assertEquals("SentinelProperties filter url pattern item was wrong", "/test",
sentinelProperties.getFilter().getUrlPatterns().get(1));
assertThat(sentinelProperties.getFilter().getOrder()).isEqualTo(123);
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(2);
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
.isEqualTo("/*");
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(1))
.isEqualTo("/test");
}
private void checkSentinelMetric() {
assertEquals("SentinelProperties metric charset was wrong", "UTF-8",
sentinelProperties.getMetric().getCharset());
assertEquals("SentinelProperties metric file single size was wrong", "9999",
sentinelProperties.getMetric().getFileSingleSize());
assertEquals("SentinelProperties metric file total count was wrong", "100",
sentinelProperties.getMetric().getFileTotalCount());
assertThat(sentinelProperties.getMetric().getCharset()).isEqualTo("UTF-8");
assertThat(sentinelProperties.getMetric().getFileSingleSize()).isEqualTo("9999");
assertThat(sentinelProperties.getMetric().getFileTotalCount()).isEqualTo("100");
}
private void checkSentinelColdFactor() {
assertEquals("SentinelProperties coldFactor was wrong", "3",
sentinelProperties.getFlow().getColdFactor());
assertThat(sentinelProperties.getFlow().getColdFactor()).isEqualTo("3");
}
private void checkSentinelTransport() {
assertEquals("SentinelProperties transport port was wrong", "9999",
sentinelProperties.getTransport().getPort());
assertEquals("SentinelProperties transport dashboard was wrong",
"http://localhost:8080",
sentinelProperties.getTransport().getDashboard());
assertEquals("SentinelProperties transport clientIp was wrong", "1.1.1.1",
sentinelProperties.getTransport().getClientIp());
assertEquals("SentinelProperties transport heartbeatIntervalMs was wrong",
"20000", sentinelProperties.getTransport().getHeartbeatIntervalMs());
assertThat(sentinelProperties.getTransport().getPort()).isEqualTo("9999");
assertThat(sentinelProperties.getTransport().getDashboard())
.isEqualTo("http://localhost:8080");
assertThat(sentinelProperties.getTransport().getClientIp()).isEqualTo("1.1.1.1");
assertThat(sentinelProperties.getTransport().getHeartbeatIntervalMs())
.isEqualTo("20000");
}
private void checkSentinelEager() {
assertEquals("SentinelProperties eager was wrong", true,
sentinelProperties.isEager());
assertThat(sentinelProperties.isEager()).isEqualTo(true);
}
private void checkSentinelLog() {
assertEquals("SentinelProperties log file pid was wrong", true,
sentinelProperties.getLog().isSwitchPid());
assertThat(sentinelProperties.getLog().isSwitchPid()).isEqualTo(true);
}
@Test
public void testFilter() {
assertEquals("Sentinel Filter order was wrong", filterRegistrationBean.getOrder(),
123);
assertEquals("Sentinel Filter url-pattern was wrong",
filterRegistrationBean.getUrlPatterns().size(), 2);
assertThat(123).isEqualTo(filterRegistrationBean.getOrder());
assertThat(2).isEqualTo(filterRegistrationBean.getUrlPatterns().size());
}
@Test
public void testSentinelSystemProperties() {
assertEquals("Sentinel log pid was wrong", true, LogBase.isLogNameUsePid());
assertEquals("Sentinel transport console server was wrong",
"http://localhost:8080", TransportConfig.getConsoleServer());
assertEquals("Sentinel transport port was wrong", "9999",
TransportConfig.getPort());
assertEquals("Sentinel transport heartbeatIntervalMs was wrong", 20000l,
TransportConfig.getHeartbeatIntervalMs().longValue());
assertEquals("Sentinel transport clientIp was wrong", "1.1.1.1",
TransportConfig.getHeartbeatClientIp());
assertEquals("Sentinel metric file size was wrong", 9999,
SentinelConfig.singleMetricFileSize());
assertEquals("Sentinel metric file count was wrong", 100,
SentinelConfig.totalMetricFileCount());
assertEquals("Sentinel metric file charset was wrong", "UTF-8",
SentinelConfig.charset());
assertEquals("Sentinel block page was wrong", "/error",
WebServletConfig.getBlockPage());
assertThat(LogBase.isLogNameUsePid()).isEqualTo(true);
assertThat(TransportConfig.getConsoleServer()).isEqualTo("http://localhost:8080");
assertThat(TransportConfig.getPort()).isEqualTo("9999");
assertThat(TransportConfig.getHeartbeatIntervalMs().longValue())
.isEqualTo(20000L);
assertThat(TransportConfig.getHeartbeatClientIp()).isEqualTo("1.1.1.1");
assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999);
assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100);
assertThat(SentinelConfig.charset()).isEqualTo("UTF-8");
assertThat(WebServletConfig.getBlockPage()).isEqualTo("/error");
}
@Test
public void testFlowRestTemplate() {
assertEquals("RestTemplate interceptors size was wrong", 2,
restTemplate.getInterceptors().size());
assertEquals("RestTemplateWithBlockClass interceptors size was wrong", 1,
restTemplateWithBlockClass.getInterceptors().size());
assertThat(restTemplate.getInterceptors().size()).isEqualTo(2);
assertThat(restTemplateWithBlockClass.getInterceptors().size()).isEqualTo(1);
ResponseEntity responseEntityBlock = restTemplateWithBlockClass
.getForEntity(flowUrl, String.class);
assertEquals("RestTemplateWithBlockClass Sentinel Block Message was wrong",
"Oops", responseEntityBlock.getBody());
assertEquals(
"RestTemplateWithBlockClass Sentinel Block Http Status Code was wrong",
HttpStatus.OK, responseEntityBlock.getStatusCode());
assertThat(responseEntityBlock.getBody()).isEqualTo("Oops");
assertThat(responseEntityBlock.getStatusCode()).isEqualTo(HttpStatus.OK);
ResponseEntity responseEntityRaw = restTemplate.getForEntity(flowUrl,
String.class);
assertEquals("RestTemplate Sentinel Block Message was wrong",
"RestTemplate request block by sentinel", responseEntityRaw.getBody());
assertEquals("RestTemplate Sentinel Block Http Status Code was wrong",
HttpStatus.OK, responseEntityRaw.getStatusCode());
assertThat(responseEntityRaw.getBody())
.isEqualTo("RestTemplate request block by sentinel");
assertThat(responseEntityRaw.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Test
public void testNormalRestTemplate() {
assertEquals("RestTemplateWithoutBlockClass interceptors size was wrong", 0,
restTemplateWithoutBlockClass.getInterceptors().size());
assertThatExceptionOfType(RestClientException.class).isThrownBy(() -> {
assertThat(restTemplateWithoutBlockClass.getInterceptors().size()).isEqualTo(0);
assertThatThrownBy(() -> {
restTemplateWithoutBlockClass.getForEntity(flowUrl, String.class);
});
}).isInstanceOf(RestClientException.class);
}
@Test
public void testFallbackRestTemplate() {
ResponseEntity responseEntity = restTemplateWithFallbackClass
.getForEntity(degradeUrl, String.class);
assertEquals("RestTemplateWithFallbackClass Sentinel Message was wrong",
"Oops fallback", responseEntity.getBody());
assertEquals("RestTemplateWithFallbackClass Sentinel Http Status Code was wrong",
HttpStatus.OK, responseEntity.getStatusCode());
assertThat(responseEntity.getBody()).isEqualTo("Oops fallback");
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Configuration
@@ -295,13 +265,15 @@ public class SentinelAutoConfigurationTests {
}
@Bean
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class, blockHandler = "handleException")
@SentinelRestTemplate(blockHandlerClass = ExceptionUtil.class,
blockHandler = "handleException")
RestTemplate restTemplateWithBlockClass() {
return new RestTemplate();
}
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class, fallback = "fallbackException")
@SentinelRestTemplate(fallbackClass = ExceptionUtil.class,
fallback = "fallbackException")
RestTemplate restTemplateWithFallbackClass() {
return new RestTemplate();
}
@@ -314,6 +286,7 @@ public class SentinelAutoConfigurationTests {
}
public static class ExceptionUtil {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
@@ -325,6 +298,7 @@ public class SentinelAutoConfigurationTests {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
return new SentinelClientHttpResponse("Oops fallback");
}
}
@Configuration
@@ -332,6 +306,7 @@ public class SentinelAutoConfigurationTests {
@ImportAutoConfiguration({ SentinelAutoConfiguration.class,
SentinelWebAutoConfiguration.class, SentinelTestConfiguration.class })
public static class TestConfig {
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,24 +16,11 @@
package com.alibaba.cloud.sentinel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
@@ -41,13 +28,25 @@ import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class }, properties = {
"spring.cloud.sentinel.filter.order=111" })
@SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class },
properties = { "spring.cloud.sentinel.filter.order=111" })
public class SentinelBeanAutowiredTests {
@Autowired
@@ -64,31 +63,27 @@ public class SentinelBeanAutowiredTests {
@Test
public void contextLoads() throws Exception {
assertNotNull("UrlCleaner was not created", urlCleaner);
assertNotNull("UrlBlockHandler was not created", urlBlockHandler);
assertNotNull("RequestOriginParser was not created", requestOriginParser);
assertNotNull("SentinelProperties was not created", sentinelProperties);
assertThat(urlCleaner).isNotNull();
assertThat(urlBlockHandler).isNotNull();
assertThat(requestOriginParser).isNotNull();
assertThat(sentinelProperties).isNotNull();
checkUrlPattern();
}
private void checkUrlPattern() {
assertEquals("SentinelProperties filter order was wrong", 111,
sentinelProperties.getFilter().getOrder());
assertEquals("SentinelProperties filter url pattern size was wrong", 1,
sentinelProperties.getFilter().getUrlPatterns().size());
assertEquals("SentinelProperties filter url pattern was wrong", "/*",
sentinelProperties.getFilter().getUrlPatterns().get(0));
assertThat(sentinelProperties.getFilter().getOrder()).isEqualTo(111);
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(1);
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
.isEqualTo("/*");
}
@Test
public void testBeanAutowired() {
assertEquals("UrlCleaner was not autowired", urlCleaner,
WebCallbackManager.getUrlCleaner());
assertEquals("UrlBlockHandler was not autowired", urlBlockHandler,
WebCallbackManager.getUrlBlockHandler());
assertEquals("RequestOriginParser was not autowired", requestOriginParser,
WebCallbackManager.getRequestOriginParser());
assertThat(WebCallbackManager.getUrlCleaner()).isEqualTo(urlCleaner);
assertThat(WebCallbackManager.getUrlBlockHandler()).isEqualTo(urlBlockHandler);
assertThat(WebCallbackManager.getRequestOriginParser())
.isEqualTo(requestOriginParser);
}
@Configuration

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,12 +16,11 @@
package com.alibaba.cloud.sentinel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@@ -29,8 +28,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@@ -62,38 +60,31 @@ public class SentinelDataSourceTests {
@Test
public void contextLoads() throws Exception {
assertNotNull("SentinelProperties was not created", sentinelProperties);
assertThat(sentinelProperties).isNotNull();
checkUrlPattern();
}
private void checkUrlPattern() {
assertEquals("SentinelProperties filter order was wrong", Integer.MIN_VALUE,
sentinelProperties.getFilter().getOrder());
assertEquals("SentinelProperties filter url pattern size was wrong", 1,
sentinelProperties.getFilter().getUrlPatterns().size());
assertEquals("SentinelProperties filter url pattern was wrong", "/*",
sentinelProperties.getFilter().getUrlPatterns().get(0));
assertThat(sentinelProperties.getFilter().getOrder())
.isEqualTo(Integer.MIN_VALUE);
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()).isEqualTo(1);
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))
.isEqualTo("/*");
}
@Test
public void testDataSource() {
assertEquals("DataSource size was wrong", 5,
sentinelProperties.getDatasource().size());
assertNull("DataSource ds1 apollo is not null",
sentinelProperties.getDatasource().get("ds1").getApollo());
assertNull("DataSource ds1 nacos is not null",
sentinelProperties.getDatasource().get("ds1").getNacos());
assertNull("DataSource ds1 zk is not null",
sentinelProperties.getDatasource().get("ds1").getZk());
assertNotNull("DataSource ds1 file is null",
sentinelProperties.getDatasource().get("ds1").getFile());
assertEquals("DataSource ds1 file dataType was wrong", "json",
sentinelProperties.getDatasource().get("ds1").getFile().getDataType());
assertEquals("DataSource ds1 file ruleType was wrong", RuleType.FLOW,
sentinelProperties.getDatasource().get("ds1").getFile().getRuleType());
assertThat(sentinelProperties.getDatasource().size()).isEqualTo(5);
assertThat(sentinelProperties.getDatasource().get("ds1").getApollo()).isNull();
assertThat(sentinelProperties.getDatasource().get("ds1").getNacos()).isNull();
assertThat(sentinelProperties.getDatasource().get("ds1").getZk()).isNull();
assertThat(sentinelProperties.getDatasource().get("ds1").getFile()).isNotNull();
assertThat(sentinelProperties.getDatasource().get("ds1").getFile().getDataType())
.isEqualTo("json");
assertThat(sentinelProperties.getDatasource().get("ds1").getFile().getRuleType())
.isEqualTo(RuleType.FLOW);
}
@Configuration

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,17 +16,16 @@
package com.alibaba.cloud.sentinel;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@@ -39,17 +38,15 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SentinelFeignTests.TestConfig.class }, properties = {
"feign.sentinel.enabled=true" })
@SpringBootTest(classes = { SentinelFeignTests.TestConfig.class },
properties = { "feign.sentinel.enabled=true" })
public class SentinelFeignTests {
@Autowired
@@ -99,29 +96,26 @@ public class SentinelFeignTests {
@Test
public void contextLoads() throws Exception {
assertNotNull("EchoService was not created", echoService);
assertNotNull("FooService was not created", fooService);
assertThat(echoService).isNotNull();
assertThat(fooService).isNotNull();
}
@Test
public void testFeignClient() {
assertEquals("Sentinel Feign Client fallback success", "echo fallback",
echoService.echo("test"));
assertEquals("Sentinel Feign Client fallbackFactory success", "foo fallback",
fooService.echo("test"));
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
barService.bar();
});
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
bazService.baz();
});
assertThat(echoService.echo("test")).isEqualTo("echo fallback");
assertThat(fooService.echo("test")).isEqualTo("foo fallback");
assertNotEquals("ToString method invoke was not in SentinelInvocationHandler",
echoService.toString(), fooService.toString());
assertNotEquals("HashCode method invoke was not in SentinelInvocationHandler",
echoService.hashCode(), fooService.hashCode());
assertFalse("Equals method invoke was not in SentinelInvocationHandler",
echoService.equals(fooService));
assertThatThrownBy(() -> {
barService.bar();
}).isInstanceOf(Exception.class);
assertThatThrownBy(() -> {
bazService.baz();
}).isInstanceOf(Exception.class);
assertThat(fooService.toString()).isNotEqualTo(echoService.toString());
assertThat(fooService.hashCode()).isNotEqualTo(echoService.hashCode());
assertThat(echoService.equals(fooService)).isEqualTo(Boolean.FALSE);
}
@Configuration
@@ -144,29 +138,38 @@ public class SentinelFeignTests {
@FeignClient(value = "test-service", fallback = EchoServiceFallback.class)
public interface EchoService {
@RequestMapping(path = "echo/{str}")
String echo(@RequestParam("str") String param);
}
@FeignClient(value = "foo-service", fallbackFactory = CustomFallbackFactory.class)
public interface FooService {
@RequestMapping(path = "echo/{str}")
String echo(@RequestParam("str") String param);
}
@FeignClient(value = "bar-service")
@FeignClient("bar-service")
public interface BarService {
@RequestMapping(path = "bar")
String bar();
}
public interface BazService {
@RequestMapping(path = "baz")
String baz();
}
@FeignClient(value = "baz-service")
@FeignClient("baz-service")
public interface BazClient extends BazService {
}
public static class EchoServiceFallback implements EchoService {
@@ -184,6 +187,7 @@ public class SentinelFeignTests {
public String echo(@RequestParam("str") String param) {
return "foo fallback";
}
}
public static class CustomFallbackFactory
@@ -195,6 +199,7 @@ public class SentinelFeignTests {
public FooService create(Throwable throwable) {
return fooService;
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,9 +16,12 @@
package com.alibaba.cloud.sentinel;
import static org.junit.Assert.assertEquals;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -28,10 +31,7 @@ import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
@@ -62,8 +62,7 @@ public class SentinelRestTemplateTests {
public void testNormal() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
TestConfig5.class);
assertEquals("RestTemplate size was wrong", 1,
context.getBeansOfType(RestTemplate.class).size());
assertThat(context.getBeansOfType(RestTemplate.class).size()).isEqualTo(1);
}
@Test(expected = BeanCreationException.class)
@@ -113,6 +112,7 @@ public class SentinelRestTemplateTests {
@Configuration
public static class TestConfig1 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -124,10 +124,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig2 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -139,10 +141,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig3 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -154,10 +158,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig4 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -169,10 +175,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig5 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -180,14 +188,22 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class, blockHandler = "handleException", fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class, fallback = "fallbackException", urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class, urlCleaner = "clean")
@SentinelRestTemplate(
blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class,
blockHandler = "handleException",
fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class,
fallback = "fallbackException",
urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class,
urlCleaner = "clean")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig6 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -195,14 +211,18 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class, blockHandler = "handleException1")
@SentinelRestTemplate(
blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class,
blockHandler = "handleException1")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig7 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -210,14 +230,18 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class, fallback = "fallbackException1")
@SentinelRestTemplate(
fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class,
fallback = "fallbackException1")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig8 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -225,14 +249,18 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class, blockHandler = "handleException2")
@SentinelRestTemplate(
blockHandlerClass = SentinelRestTemplateTests.ExceptionUtil.class,
blockHandler = "handleException2")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig9 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -240,14 +268,18 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class, fallback = "fallbackException2")
@SentinelRestTemplate(
fallbackClass = SentinelRestTemplateTests.ExceptionUtil.class,
fallback = "fallbackException2")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig10 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -265,10 +297,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate2() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig11 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -280,10 +314,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig12 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -295,10 +331,12 @@ public class SentinelRestTemplateTests {
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig13 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -306,14 +344,18 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class, urlCleaner = "clean1")
@SentinelRestTemplate(
urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class,
urlCleaner = "clean1")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class TestConfig14 {
@Bean
SentinelBeanPostProcessor sentinelBeanPostProcessor(
ApplicationContext applicationContext) {
@@ -321,13 +363,17 @@ public class SentinelRestTemplateTests {
}
@Bean
@SentinelRestTemplate(urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class, urlCleaner = "clean2")
@SentinelRestTemplate(
urlCleanerClass = SentinelRestTemplateTests.UrlCleanUtil.class,
urlCleaner = "clean2")
RestTemplate restTemplate() {
return new RestTemplate();
}
}
public static class ExceptionUtil {
public static SentinelClientHttpResponse handleException(HttpRequest request,
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
@@ -349,15 +395,18 @@ public class SentinelRestTemplateTests {
ClientHttpRequestExecution execution, BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
}
}
public static class UrlCleanUtil {
public static String clean(String url) {
return url;
}
public static void clean2(String url) {
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -21,7 +21,6 @@ import java.util.List;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -29,6 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class TestConverter implements Converter<String, List<ParamFlowRule>> {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
@@ -42,4 +42,5 @@ public class TestConverter implements Converter<String, List<ParamFlowRule>> {
}
return null;
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,21 +16,10 @@
package com.alibaba.cloud.sentinel.endpoint;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.util.ReflectionUtils;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
@@ -38,6 +27,17 @@ import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource;
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
import com.alibaba.csp.sentinel.transport.HeartbeatSender;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test cases for {@link SentinelHealthIndicator}.
@@ -171,4 +171,5 @@ public class SentinelHealthIndicatorTests {
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
.isEqualTo(new Status(Status.DOWN.getCode(), "fileDataSource2 error"));
}
}