mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1171 from yuhuangbin/sentinel-enhance
[Upgrade] Upgrade Sentinel and Enhance
This commit is contained in:
commit
f789803cac
@ -18,7 +18,7 @@
|
||||
<description>Spring Cloud Alibaba Dependencies</description>
|
||||
|
||||
<properties>
|
||||
<sentinel.version>1.6.3</sentinel.version>
|
||||
<sentinel.version>1.7.1</sentinel.version>
|
||||
<oss.version>3.1.0</oss.version>
|
||||
<seata.version>0.9.0</seata.version>
|
||||
<nacos.client.version>1.1.4</nacos.client.version>
|
||||
@ -198,6 +198,11 @@
|
||||
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
|
||||
<version>${sentinel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
|
||||
<version>${sentinel.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--Alibaba Seata-->
|
||||
|
@ -30,6 +30,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.examples;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class WebMvcConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/errorPage").setViewName("errorPage");
|
||||
}
|
||||
|
||||
}
|
@ -9,6 +9,9 @@ management.health.diskspace.enabled=false
|
||||
|
||||
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
||||
spring.cloud.sentinel.eager=true
|
||||
|
||||
#spring.cloud.sentinel.block-page=/errorPage
|
||||
#spring.cloud.sentinel.filter.enabled=false
|
||||
#spring.cloud.sentinel.http-method-specify=false
|
||||
|
||||
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
|
||||
|
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
This is error page.
|
||||
</body>
|
||||
</html>
|
@ -41,11 +41,6 @@
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-web-servlet</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
@ -57,6 +52,11 @@
|
||||
<artifactId>sentinel-spring-webflux-adapter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
|
@ -26,6 +26,11 @@ public final class SentinelConstants {
|
||||
*/
|
||||
public static final String PROPERTY_PREFIX = "spring.cloud.sentinel";
|
||||
|
||||
/**
|
||||
* Block page key.
|
||||
*/
|
||||
public static final String BLOCK_PAGE_URL_CONF_KEY = "csp.sentinel.web.servlet.block.page";
|
||||
|
||||
/**
|
||||
* Block type.
|
||||
*/
|
||||
@ -41,6 +46,21 @@ public final class SentinelConstants {
|
||||
*/
|
||||
public static final String URLCLEANER_TYPE = "urlCleaner";
|
||||
|
||||
/**
|
||||
* The cold factor.
|
||||
*/
|
||||
public static final String COLD_FACTOR = "3";
|
||||
|
||||
/**
|
||||
* The charset.
|
||||
*/
|
||||
public static final String CHARSET = "UTF-8";
|
||||
|
||||
/**
|
||||
* The Sentinel api port.
|
||||
*/
|
||||
public static final String API_PORT = "8719";
|
||||
|
||||
private SentinelConstants() {
|
||||
throw new AssertionError("Must not instantiate constant utility class");
|
||||
}
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
@ -26,7 +27,9 @@ 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.boot.context.properties.DeprecatedConfigurationProperty;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
@ -52,6 +55,11 @@ public class SentinelProperties {
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* The process page when the flow control is triggered.
|
||||
*/
|
||||
private String blockPage;
|
||||
|
||||
/**
|
||||
* Configurations about datasource, like 'nacos', 'apollo', 'file', 'zookeeper'.
|
||||
*/
|
||||
@ -75,7 +83,7 @@ public class SentinelProperties {
|
||||
private Servlet servlet = new Servlet();
|
||||
|
||||
/**
|
||||
* Sentinel filter when the application is web, the configuration is effective.
|
||||
* Sentinel interceptor when the application is web, the configuration is effective.
|
||||
*/
|
||||
private Filter filter = new Filter();
|
||||
|
||||
@ -174,12 +182,23 @@ public class SentinelProperties {
|
||||
this.httpMethodSpecify = httpMethodSpecify;
|
||||
}
|
||||
|
||||
public String getBlockPage() {
|
||||
if (StringUtils.hasText(this.blockPage)) {
|
||||
return this.blockPage;
|
||||
}
|
||||
return this.servlet.getBlockPage();
|
||||
}
|
||||
|
||||
public void setBlockPage(String blockPage) {
|
||||
this.blockPage = blockPage;
|
||||
}
|
||||
|
||||
public static class Flow {
|
||||
|
||||
/**
|
||||
* The cold factor {@link SentinelConfig#COLD_FACTOR}.
|
||||
*/
|
||||
private String coldFactor = "3";
|
||||
private String coldFactor = SentinelConstants.COLD_FACTOR;
|
||||
|
||||
public String getColdFactor() {
|
||||
return coldFactor;
|
||||
@ -198,10 +217,15 @@ public class SentinelProperties {
|
||||
*/
|
||||
private String blockPage;
|
||||
|
||||
@Deprecated
|
||||
@DeprecatedConfigurationProperty(
|
||||
reason = "replaced to SentinelProperties#blockPage.",
|
||||
replacement = SentinelConstants.PROPERTY_PREFIX + ".block-page")
|
||||
public String getBlockPage() {
|
||||
return blockPage;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setBlockPage(String blockPage) {
|
||||
this.blockPage = blockPage;
|
||||
}
|
||||
@ -224,7 +248,7 @@ public class SentinelProperties {
|
||||
* Charset when sentinel write or search metric file.
|
||||
* {@link SentinelConfig#CHARSET}
|
||||
*/
|
||||
private String charset = "UTF-8";
|
||||
private String charset = SentinelConstants.CHARSET;
|
||||
|
||||
public String getFileSingleSize() {
|
||||
return fileSingleSize;
|
||||
@ -257,7 +281,7 @@ public class SentinelProperties {
|
||||
/**
|
||||
* Sentinel api port, default value is 8719 {@link TransportConfig#SERVER_PORT}.
|
||||
*/
|
||||
private String port = "8719";
|
||||
private String port = SentinelConstants.API_PORT;
|
||||
|
||||
/**
|
||||
* Sentinel dashboard address, won't try to connect dashboard when address is
|
||||
@ -314,18 +338,18 @@ public class SentinelProperties {
|
||||
public static class Filter {
|
||||
|
||||
/**
|
||||
* Sentinel filter chain order.
|
||||
* SentinelWebInterceptor order, will be register to InterceptorRegistry.
|
||||
*/
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
/**
|
||||
* URL pattern for sentinel filter, default is /*.
|
||||
* URL pattern for SentinelWebInterceptor, default is /*.
|
||||
*/
|
||||
private List<String> urlPatterns;
|
||||
private List<String> urlPatterns = Arrays.asList("/*");
|
||||
|
||||
/**
|
||||
* Enable to instance
|
||||
* {@link com.alibaba.csp.sentinel.adapter.servlet.CommonFilter}.
|
||||
* {@link com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor}.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
|
@ -16,18 +16,14 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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 com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -37,19 +33,22 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnWebApplication(type = Type.SERVLET)
|
||||
@ConditionalOnClass(CommonFilter.class)
|
||||
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
|
||||
@ConditionalOnClass(SentinelWebInterceptor.class)
|
||||
@EnableConfigurationProperties(SentinelProperties.class)
|
||||
public class SentinelWebAutoConfiguration {
|
||||
public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(SentinelWebAutoConfiguration.class);
|
||||
@ -61,44 +60,61 @@ public class SentinelWebAutoConfiguration {
|
||||
private Optional<UrlCleaner> urlCleanerOptional;
|
||||
|
||||
@Autowired
|
||||
private Optional<UrlBlockHandler> urlBlockHandlerOptional;
|
||||
private Optional<BlockExceptionHandler> blockExceptionHandlerOptional;
|
||||
|
||||
@Autowired
|
||||
private Optional<RequestOriginParser> requestOriginParserOptional;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
|
||||
urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
|
||||
requestOriginParserOptional.ifPresent(WebCallbackManager::setRequestOriginParser);
|
||||
@Autowired
|
||||
private Optional<SentinelWebInterceptor> sentinelWebInterceptorOptional;
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
if (!sentinelWebInterceptorOptional.isPresent()) {
|
||||
return;
|
||||
}
|
||||
SentinelProperties.Filter filterConfig = properties.getFilter();
|
||||
registry.addInterceptor(sentinelWebInterceptorOptional.get())
|
||||
.order(filterConfig.getOrder())
|
||||
.addPathPatterns(filterConfig.getUrlPatterns());
|
||||
log.info(
|
||||
"[Sentinel Starter] register SentinelWebInterceptor with urlPatterns: {}.",
|
||||
filterConfig.getUrlPatterns());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
|
||||
matchIfMissing = true)
|
||||
public FilterRegistrationBean sentinelFilter() {
|
||||
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
|
||||
|
||||
SentinelProperties.Filter filterConfig = properties.getFilter();
|
||||
|
||||
if (filterConfig.getUrlPatterns() == null
|
||||
|| filterConfig.getUrlPatterns().isEmpty()) {
|
||||
List<String> defaultPatterns = new ArrayList<>();
|
||||
defaultPatterns.add("/*");
|
||||
filterConfig.setUrlPatterns(defaultPatterns);
|
||||
public SentinelWebInterceptor sentinelWebInterceptor(
|
||||
SentinelWebMvcConfig sentinelWebMvcConfig) {
|
||||
return new SentinelWebInterceptor(sentinelWebMvcConfig);
|
||||
}
|
||||
|
||||
registration.addUrlPatterns(filterConfig.getUrlPatterns().toArray(new String[0]));
|
||||
Filter filter = new CommonFilter();
|
||||
registration.setFilter(filter);
|
||||
registration.setOrder(filterConfig.getOrder());
|
||||
registration.addInitParameter("HTTP_METHOD_SPECIFY",
|
||||
String.valueOf(properties.getHttpMethodSpecify()));
|
||||
log.info(
|
||||
"[Sentinel Starter] register Sentinel CommonFilter with urlPatterns: {}.",
|
||||
filterConfig.getUrlPatterns());
|
||||
return registration;
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
|
||||
matchIfMissing = true)
|
||||
public SentinelWebMvcConfig sentinelWebMvcConfig() {
|
||||
SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
|
||||
sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify());
|
||||
|
||||
if (blockExceptionHandlerOptional.isPresent()) {
|
||||
blockExceptionHandlerOptional
|
||||
.ifPresent(sentinelWebMvcConfig::setBlockExceptionHandler);
|
||||
}
|
||||
else {
|
||||
if (StringUtils.hasText(properties.getBlockPage())) {
|
||||
sentinelWebMvcConfig.setBlockExceptionHandler(((request, response,
|
||||
e) -> response.sendRedirect(properties.getBlockPage())));
|
||||
}
|
||||
else {
|
||||
sentinelWebMvcConfig
|
||||
.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
|
||||
}
|
||||
}
|
||||
|
||||
urlCleanerOptional.ifPresent(sentinelWebMvcConfig::setUrlCleaner);
|
||||
requestOriginParserOptional.ifPresent(sentinelWebMvcConfig::setOriginParser);
|
||||
return sentinelWebMvcConfig;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,6 @@ import javax.annotation.PostConstruct;
|
||||
import com.alibaba.cloud.sentinel.SentinelProperties;
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
|
||||
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
|
||||
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.init.InitExecutor;
|
||||
@ -50,6 +49,9 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static com.alibaba.cloud.sentinel.SentinelConstants.BLOCK_PAGE_URL_CONF_KEY;
|
||||
import static com.alibaba.csp.sentinel.config.SentinelConfig.setConfig;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author jiashuai.xie
|
||||
@ -124,8 +126,8 @@ public class SentinelAutoConfiguration {
|
||||
System.setProperty(SentinelConfig.COLD_FACTOR,
|
||||
properties.getFlow().getColdFactor());
|
||||
}
|
||||
if (StringUtils.hasText(properties.getServlet().getBlockPage())) {
|
||||
WebServletConfig.setBlockPage(properties.getServlet().getBlockPage());
|
||||
if (StringUtils.hasText(properties.getBlockPage())) {
|
||||
setConfig(BLOCK_PAGE_URL_CONF_KEY, properties.getBlockPage());
|
||||
}
|
||||
|
||||
// earlier initialize
|
||||
|
@ -20,7 +20,6 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.cloud.sentinel.SentinelProperties;
|
||||
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.authority.AuthorityRuleManager;
|
||||
@ -34,6 +33,8 @@ import com.alibaba.csp.sentinel.util.AppNameUtil;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
|
||||
import static com.alibaba.cloud.sentinel.SentinelConstants.BLOCK_PAGE_URL_CONF_KEY;
|
||||
|
||||
/**
|
||||
* Endpoint for Sentinel, contains ans properties and rules.
|
||||
*
|
||||
@ -56,7 +57,7 @@ public class SentinelEndpoint {
|
||||
result.put("appName", AppNameUtil.getAppName());
|
||||
result.put("logDir", LogBase.getLogBaseDir());
|
||||
result.put("logUsePid", LogBase.isLogNameUsePid());
|
||||
result.put("blockPage", WebServletConfig.getBlockPage());
|
||||
result.put("blockPage", SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY));
|
||||
result.put("metricsFileSize", SentinelConfig.singleMetricFileSize());
|
||||
result.put("metricsFileCharset", SentinelConfig.charset());
|
||||
result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount());
|
||||
|
@ -43,13 +43,13 @@
|
||||
"name": "spring.cloud.sentinel.filter.order",
|
||||
"type": "java.lang.Integer",
|
||||
"defaultValue": "Integer.MIN_VALUE",
|
||||
"description": "sentinel filter chain order, will be set to FilterRegistrationBean."
|
||||
"description": "SentinelWebInterceptor order, will be register to InterceptorRegistry."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.filter.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "Enable to instance com.alibaba.csp.sentinel.adapter.servlet.CommonFilter."
|
||||
"description": "Enable to register com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.metric.charset",
|
||||
@ -79,10 +79,15 @@
|
||||
"description": "log file should with pid."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.servlet.blockPage",
|
||||
"name": "spring.cloud.sentinel.block-page",
|
||||
"type": "java.lang.String",
|
||||
"description": "the process page when the flow control is triggered."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.servlet.block-page",
|
||||
"type": "java.lang.String",
|
||||
"description": "recommoned use spring.cloud.sentinel.block-page."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.sentinel.flow.coldFactor",
|
||||
"type": "java.lang.String",
|
||||
|
@ -24,7 +24,6 @@ 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;
|
||||
@ -43,7 +42,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpRequest;
|
||||
@ -55,6 +53,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static com.alibaba.cloud.sentinel.SentinelConstants.BLOCK_PAGE_URL_CONF_KEY;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -70,7 +69,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
"spring.cloud.sentinel.filter.urlPatterns=/*,/test",
|
||||
"spring.cloud.sentinel.metric.fileSingleSize=9999",
|
||||
"spring.cloud.sentinel.metric.fileTotalCount=100",
|
||||
"spring.cloud.sentinel.servlet.blockPage=/error",
|
||||
"spring.cloud.sentinel.blockPage=/error",
|
||||
"spring.cloud.sentinel.flow.coldFactor=3",
|
||||
"spring.cloud.sentinel.eager=true",
|
||||
"spring.cloud.sentinel.log.switchPid=true",
|
||||
@ -84,9 +83,6 @@ public class SentinelAutoConfigurationTests {
|
||||
@Autowired
|
||||
private SentinelProperties sentinelProperties;
|
||||
|
||||
@Autowired
|
||||
private FilterRegistrationBean filterRegistrationBean;
|
||||
|
||||
@Autowired
|
||||
private SentinelBeanPostProcessor sentinelBeanPostProcessor;
|
||||
|
||||
@ -130,9 +126,6 @@ public class SentinelAutoConfigurationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(filterRegistrationBean).isNotNull();
|
||||
assertThat(filterRegistrationBean).isNotNull();
|
||||
assertThat(sentinelBeanPostProcessor).isNotNull();
|
||||
|
||||
checkSentinelLog();
|
||||
@ -195,12 +188,6 @@ public class SentinelAutoConfigurationTests {
|
||||
assertThat(sentinelProperties.getLog().isSwitchPid()).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFilter() {
|
||||
assertThat(123).isEqualTo(filterRegistrationBean.getOrder());
|
||||
assertThat(2).isEqualTo(filterRegistrationBean.getUrlPatterns().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSentinelSystemProperties() {
|
||||
assertThat(LogBase.isLogNameUsePid()).isEqualTo(true);
|
||||
@ -212,7 +199,7 @@ public class SentinelAutoConfigurationTests {
|
||||
assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999);
|
||||
assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100);
|
||||
assertThat(SentinelConfig.charset()).isEqualTo("UTF-8");
|
||||
assertThat(WebServletConfig.getBlockPage()).isEqualTo("/error");
|
||||
assertThat(SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY)).isEqualTo("/error");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -16,18 +16,14 @@
|
||||
|
||||
package com.alibaba.cloud.sentinel;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
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;
|
||||
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 com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@ -53,7 +49,7 @@ public class SentinelBeanAutowiredTests {
|
||||
private UrlCleaner urlCleaner;
|
||||
|
||||
@Autowired
|
||||
private UrlBlockHandler urlBlockHandler;
|
||||
private BlockExceptionHandler blockExceptionHandler;
|
||||
|
||||
@Autowired
|
||||
private RequestOriginParser requestOriginParser;
|
||||
@ -61,10 +57,13 @@ public class SentinelBeanAutowiredTests {
|
||||
@Autowired
|
||||
private SentinelProperties sentinelProperties;
|
||||
|
||||
@Autowired
|
||||
private SentinelWebMvcConfig sentinelWebMvcConfig;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertThat(urlCleaner).isNotNull();
|
||||
assertThat(urlBlockHandler).isNotNull();
|
||||
assertThat(blockExceptionHandler).isNotNull();
|
||||
assertThat(requestOriginParser).isNotNull();
|
||||
assertThat(sentinelProperties).isNotNull();
|
||||
|
||||
@ -80,10 +79,10 @@ public class SentinelBeanAutowiredTests {
|
||||
|
||||
@Test
|
||||
public void testBeanAutowired() {
|
||||
assertThat(WebCallbackManager.getUrlCleaner()).isEqualTo(urlCleaner);
|
||||
assertThat(WebCallbackManager.getUrlBlockHandler()).isEqualTo(urlBlockHandler);
|
||||
assertThat(WebCallbackManager.getRequestOriginParser())
|
||||
.isEqualTo(requestOriginParser);
|
||||
assertThat(sentinelWebMvcConfig.getUrlCleaner()).isEqualTo(urlCleaner);
|
||||
assertThat(sentinelWebMvcConfig.getBlockExceptionHandler())
|
||||
.isEqualTo(blockExceptionHandler);
|
||||
assertThat(sentinelWebMvcConfig.getOriginParser()).isEqualTo(requestOriginParser);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ -113,15 +112,8 @@ public class SentinelBeanAutowiredTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UrlBlockHandler urlBlockHandler() {
|
||||
return new UrlBlockHandler() {
|
||||
@Override
|
||||
public void blocked(HttpServletRequest httpServletRequest,
|
||||
HttpServletResponse httpServletResponse, BlockException e)
|
||||
throws IOException {
|
||||
FilterUtil.blockRequest(httpServletRequest, httpServletResponse);
|
||||
}
|
||||
};
|
||||
public BlockExceptionHandler blockExceptionHandler() {
|
||||
return new DefaultBlockExceptionHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user