1
0
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:
Jim Fang 2020-01-14 17:00:57 +08:00 committed by GitHub
commit f789803cac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 205 additions and 100 deletions

View File

@ -18,7 +18,7 @@
<description>Spring Cloud Alibaba Dependencies</description> <description>Spring Cloud Alibaba Dependencies</description>
<properties> <properties>
<sentinel.version>1.6.3</sentinel.version> <sentinel.version>1.7.1</sentinel.version>
<oss.version>3.1.0</oss.version> <oss.version>3.1.0</oss.version>
<seata.version>0.9.0</seata.version> <seata.version>0.9.0</seata.version>
<nacos.client.version>1.1.4</nacos.client.version> <nacos.client.version>1.1.4</nacos.client.version>
@ -198,6 +198,11 @@
<artifactId>sentinel-api-gateway-adapter-common</artifactId> <artifactId>sentinel-api-gateway-adapter-common</artifactId>
<version>${sentinel.version}</version> <version>${sentinel.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
<version>${sentinel.version}</version>
</dependency>
<!--Alibaba Seata--> <!--Alibaba Seata-->

View File

@ -30,6 +30,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--<dependency>--> <!--<dependency>-->
<!--<groupId>com.alibaba.csp</groupId>--> <!--<groupId>com.alibaba.csp</groupId>-->

View File

@ -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");
}
}

View File

@ -9,6 +9,9 @@ management.health.diskspace.enabled=false
spring.cloud.sentinel.transport.dashboard=localhost:8080 spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true 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.http-method-specify=false
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json

View File

@ -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>

View File

@ -41,11 +41,6 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-web-servlet</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
@ -57,6 +52,11 @@
<artifactId>sentinel-spring-webflux-adapter</artifactId> <artifactId>sentinel-spring-webflux-adapter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webmvc-adapter</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>

View File

@ -26,6 +26,11 @@ public final class SentinelConstants {
*/ */
public static final String PROPERTY_PREFIX = "spring.cloud.sentinel"; 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. * Block type.
*/ */
@ -41,6 +46,21 @@ public final class SentinelConstants {
*/ */
public static final String URLCLEANER_TYPE = "urlCleaner"; 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() { private SentinelConstants() {
throw new AssertionError("Must not instantiate constant utility class"); throw new AssertionError("Must not instantiate constant utility class");
} }

View File

@ -16,6 +16,7 @@
package com.alibaba.cloud.sentinel; package com.alibaba.cloud.sentinel;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -26,7 +27,9 @@ import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
/** /**
@ -52,6 +55,11 @@ public class SentinelProperties {
*/ */
private boolean enabled = true; private boolean enabled = true;
/**
* The process page when the flow control is triggered.
*/
private String blockPage;
/** /**
* Configurations about datasource, like 'nacos', 'apollo', 'file', 'zookeeper'. * Configurations about datasource, like 'nacos', 'apollo', 'file', 'zookeeper'.
*/ */
@ -75,7 +83,7 @@ public class SentinelProperties {
private Servlet servlet = new Servlet(); 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(); private Filter filter = new Filter();
@ -174,12 +182,23 @@ public class SentinelProperties {
this.httpMethodSpecify = httpMethodSpecify; 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 { public static class Flow {
/** /**
* The cold factor {@link SentinelConfig#COLD_FACTOR}. * The cold factor {@link SentinelConfig#COLD_FACTOR}.
*/ */
private String coldFactor = "3"; private String coldFactor = SentinelConstants.COLD_FACTOR;
public String getColdFactor() { public String getColdFactor() {
return coldFactor; return coldFactor;
@ -198,10 +217,15 @@ public class SentinelProperties {
*/ */
private String blockPage; private String blockPage;
@Deprecated
@DeprecatedConfigurationProperty(
reason = "replaced to SentinelProperties#blockPage.",
replacement = SentinelConstants.PROPERTY_PREFIX + ".block-page")
public String getBlockPage() { public String getBlockPage() {
return blockPage; return blockPage;
} }
@Deprecated
public void setBlockPage(String blockPage) { public void setBlockPage(String blockPage) {
this.blockPage = blockPage; this.blockPage = blockPage;
} }
@ -224,7 +248,7 @@ public class SentinelProperties {
* Charset when sentinel write or search metric file. * Charset when sentinel write or search metric file.
* {@link SentinelConfig#CHARSET} * {@link SentinelConfig#CHARSET}
*/ */
private String charset = "UTF-8"; private String charset = SentinelConstants.CHARSET;
public String getFileSingleSize() { public String getFileSingleSize() {
return fileSingleSize; return fileSingleSize;
@ -257,7 +281,7 @@ public class SentinelProperties {
/** /**
* Sentinel api port, default value is 8719 {@link TransportConfig#SERVER_PORT}. * 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 * Sentinel dashboard address, won't try to connect dashboard when address is
@ -314,18 +338,18 @@ public class SentinelProperties {
public static class Filter { public static class Filter {
/** /**
* Sentinel filter chain order. * SentinelWebInterceptor order, will be register to InterceptorRegistry.
*/ */
private int order = Ordered.HIGHEST_PRECEDENCE; 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 * Enable to instance
* {@link com.alibaba.csp.sentinel.adapter.servlet.CommonFilter}. * {@link com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor}.
*/ */
private boolean enabled = true; private boolean enabled = true;

View File

@ -16,18 +16,14 @@
package com.alibaba.cloud.sentinel; package com.alibaba.cloud.sentinel;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.annotation.PostConstruct; import com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor;
import javax.servlet.Filter; 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.servlet.CommonFilter; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
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.Logger;
import org.slf4j.LoggerFactory; 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;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.Bean;
import org.springframework.context.annotation.Configuration; 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 xiaojing
* @author yuhuangbin
*/ */
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(CommonFilter.class)
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
@ConditionalOnClass(SentinelWebInterceptor.class)
@EnableConfigurationProperties(SentinelProperties.class) @EnableConfigurationProperties(SentinelProperties.class)
public class SentinelWebAutoConfiguration { public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
private static final Logger log = LoggerFactory private static final Logger log = LoggerFactory
.getLogger(SentinelWebAutoConfiguration.class); .getLogger(SentinelWebAutoConfiguration.class);
@ -61,44 +60,61 @@ public class SentinelWebAutoConfiguration {
private Optional<UrlCleaner> urlCleanerOptional; private Optional<UrlCleaner> urlCleanerOptional;
@Autowired @Autowired
private Optional<UrlBlockHandler> urlBlockHandlerOptional; private Optional<BlockExceptionHandler> blockExceptionHandlerOptional;
@Autowired @Autowired
private Optional<RequestOriginParser> requestOriginParserOptional; private Optional<RequestOriginParser> requestOriginParserOptional;
@PostConstruct @Autowired
public void init() { private Optional<SentinelWebInterceptor> sentinelWebInterceptorOptional;
urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner); @Override
requestOriginParserOptional.ifPresent(WebCallbackManager::setRequestOriginParser); 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 @Bean
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled", @ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
matchIfMissing = true) matchIfMissing = true)
public FilterRegistrationBean sentinelFilter() { public SentinelWebInterceptor sentinelWebInterceptor(
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>(); SentinelWebMvcConfig sentinelWebMvcConfig) {
return new SentinelWebInterceptor(sentinelWebMvcConfig);
SentinelProperties.Filter filterConfig = properties.getFilter();
if (filterConfig.getUrlPatterns() == null
|| filterConfig.getUrlPatterns().isEmpty()) {
List<String> defaultPatterns = new ArrayList<>();
defaultPatterns.add("/*");
filterConfig.setUrlPatterns(defaultPatterns);
} }
registration.addUrlPatterns(filterConfig.getUrlPatterns().toArray(new String[0])); @Bean
Filter filter = new CommonFilter(); @ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled",
registration.setFilter(filter); matchIfMissing = true)
registration.setOrder(filterConfig.getOrder()); public SentinelWebMvcConfig sentinelWebMvcConfig() {
registration.addInitParameter("HTTP_METHOD_SPECIFY", SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
String.valueOf(properties.getHttpMethodSpecify())); sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify());
log.info(
"[Sentinel Starter] register Sentinel CommonFilter with urlPatterns: {}.",
filterConfig.getUrlPatterns());
return registration;
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;
} }
} }

View File

@ -21,7 +21,6 @@ import javax.annotation.PostConstruct;
import com.alibaba.cloud.sentinel.SentinelProperties; import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter; import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter; 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.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.init.InitExecutor; 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.core.env.Environment;
import org.springframework.util.StringUtils; 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 xiaojing
* @author jiashuai.xie * @author jiashuai.xie
@ -124,8 +126,8 @@ public class SentinelAutoConfiguration {
System.setProperty(SentinelConfig.COLD_FACTOR, System.setProperty(SentinelConfig.COLD_FACTOR,
properties.getFlow().getColdFactor()); properties.getFlow().getColdFactor());
} }
if (StringUtils.hasText(properties.getServlet().getBlockPage())) { if (StringUtils.hasText(properties.getBlockPage())) {
WebServletConfig.setBlockPage(properties.getServlet().getBlockPage()); setConfig(BLOCK_PAGE_URL_CONF_KEY, properties.getBlockPage());
} }
// earlier initialize // earlier initialize

View File

@ -20,7 +20,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.alibaba.cloud.sentinel.SentinelProperties; 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.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase; import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; 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.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; 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. * Endpoint for Sentinel, contains ans properties and rules.
* *
@ -56,7 +57,7 @@ public class SentinelEndpoint {
result.put("appName", AppNameUtil.getAppName()); result.put("appName", AppNameUtil.getAppName());
result.put("logDir", LogBase.getLogBaseDir()); result.put("logDir", LogBase.getLogBaseDir());
result.put("logUsePid", LogBase.isLogNameUsePid()); 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("metricsFileSize", SentinelConfig.singleMetricFileSize());
result.put("metricsFileCharset", SentinelConfig.charset()); result.put("metricsFileCharset", SentinelConfig.charset());
result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount()); result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount());

View File

@ -43,13 +43,13 @@
"name": "spring.cloud.sentinel.filter.order", "name": "spring.cloud.sentinel.filter.order",
"type": "java.lang.Integer", "type": "java.lang.Integer",
"defaultValue": "Integer.MIN_VALUE", "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", "name": "spring.cloud.sentinel.filter.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"defaultValue": true, "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", "name": "spring.cloud.sentinel.metric.charset",
@ -79,10 +79,15 @@
"description": "log file should with pid." "description": "log file should with pid."
}, },
{ {
"name": "spring.cloud.sentinel.servlet.blockPage", "name": "spring.cloud.sentinel.block-page",
"type": "java.lang.String", "type": "java.lang.String",
"description": "the process page when the flow control is triggered." "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", "name": "spring.cloud.sentinel.flow.coldFactor",
"type": "java.lang.String", "type": "java.lang.String",

View File

@ -24,7 +24,6 @@ import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor; import com.alibaba.cloud.sentinel.custom.SentinelBeanPostProcessor;
import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint; import com.alibaba.cloud.sentinel.endpoint.SentinelEndpoint;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse; 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.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase; import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.slots.block.BlockException; 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.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest; 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.RestClientException;
import org.springframework.web.client.RestTemplate; 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.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock; 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.filter.urlPatterns=/*,/test",
"spring.cloud.sentinel.metric.fileSingleSize=9999", "spring.cloud.sentinel.metric.fileSingleSize=9999",
"spring.cloud.sentinel.metric.fileTotalCount=100", "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.flow.coldFactor=3",
"spring.cloud.sentinel.eager=true", "spring.cloud.sentinel.eager=true",
"spring.cloud.sentinel.log.switchPid=true", "spring.cloud.sentinel.log.switchPid=true",
@ -84,9 +83,6 @@ public class SentinelAutoConfigurationTests {
@Autowired @Autowired
private SentinelProperties sentinelProperties; private SentinelProperties sentinelProperties;
@Autowired
private FilterRegistrationBean filterRegistrationBean;
@Autowired @Autowired
private SentinelBeanPostProcessor sentinelBeanPostProcessor; private SentinelBeanPostProcessor sentinelBeanPostProcessor;
@ -130,9 +126,6 @@ public class SentinelAutoConfigurationTests {
@Test @Test
public void contextLoads() throws Exception { public void contextLoads() throws Exception {
assertThat(filterRegistrationBean).isNotNull();
assertThat(filterRegistrationBean).isNotNull();
assertThat(sentinelBeanPostProcessor).isNotNull(); assertThat(sentinelBeanPostProcessor).isNotNull();
checkSentinelLog(); checkSentinelLog();
@ -195,12 +188,6 @@ public class SentinelAutoConfigurationTests {
assertThat(sentinelProperties.getLog().isSwitchPid()).isEqualTo(true); assertThat(sentinelProperties.getLog().isSwitchPid()).isEqualTo(true);
} }
@Test
public void testFilter() {
assertThat(123).isEqualTo(filterRegistrationBean.getOrder());
assertThat(2).isEqualTo(filterRegistrationBean.getUrlPatterns().size());
}
@Test @Test
public void testSentinelSystemProperties() { public void testSentinelSystemProperties() {
assertThat(LogBase.isLogNameUsePid()).isEqualTo(true); assertThat(LogBase.isLogNameUsePid()).isEqualTo(true);
@ -212,7 +199,7 @@ public class SentinelAutoConfigurationTests {
assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999); assertThat(SentinelConfig.singleMetricFileSize()).isEqualTo(9999);
assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100); assertThat(SentinelConfig.totalMetricFileCount()).isEqualTo(100);
assertThat(SentinelConfig.charset()).isEqualTo("UTF-8"); assertThat(SentinelConfig.charset()).isEqualTo("UTF-8");
assertThat(WebServletConfig.getBlockPage()).isEqualTo("/error"); assertThat(SentinelConfig.getConfig(BLOCK_PAGE_URL_CONF_KEY)).isEqualTo("/error");
} }
@Test @Test

View File

@ -16,18 +16,14 @@
package com.alibaba.cloud.sentinel; package com.alibaba.cloud.sentinel;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration; import com.alibaba.cloud.sentinel.custom.SentinelAutoConfiguration;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager; import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.util.FilterUtil; import com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -53,7 +49,7 @@ public class SentinelBeanAutowiredTests {
private UrlCleaner urlCleaner; private UrlCleaner urlCleaner;
@Autowired @Autowired
private UrlBlockHandler urlBlockHandler; private BlockExceptionHandler blockExceptionHandler;
@Autowired @Autowired
private RequestOriginParser requestOriginParser; private RequestOriginParser requestOriginParser;
@ -61,10 +57,13 @@ public class SentinelBeanAutowiredTests {
@Autowired @Autowired
private SentinelProperties sentinelProperties; private SentinelProperties sentinelProperties;
@Autowired
private SentinelWebMvcConfig sentinelWebMvcConfig;
@Test @Test
public void contextLoads() throws Exception { public void contextLoads() throws Exception {
assertThat(urlCleaner).isNotNull(); assertThat(urlCleaner).isNotNull();
assertThat(urlBlockHandler).isNotNull(); assertThat(blockExceptionHandler).isNotNull();
assertThat(requestOriginParser).isNotNull(); assertThat(requestOriginParser).isNotNull();
assertThat(sentinelProperties).isNotNull(); assertThat(sentinelProperties).isNotNull();
@ -80,10 +79,10 @@ public class SentinelBeanAutowiredTests {
@Test @Test
public void testBeanAutowired() { public void testBeanAutowired() {
assertThat(WebCallbackManager.getUrlCleaner()).isEqualTo(urlCleaner); assertThat(sentinelWebMvcConfig.getUrlCleaner()).isEqualTo(urlCleaner);
assertThat(WebCallbackManager.getUrlBlockHandler()).isEqualTo(urlBlockHandler); assertThat(sentinelWebMvcConfig.getBlockExceptionHandler())
assertThat(WebCallbackManager.getRequestOriginParser()) .isEqualTo(blockExceptionHandler);
.isEqualTo(requestOriginParser); assertThat(sentinelWebMvcConfig.getOriginParser()).isEqualTo(requestOriginParser);
} }
@Configuration @Configuration
@ -113,15 +112,8 @@ public class SentinelBeanAutowiredTests {
} }
@Bean @Bean
public UrlBlockHandler urlBlockHandler() { public BlockExceptionHandler blockExceptionHandler() {
return new UrlBlockHandler() { return new DefaultBlockExceptionHandler();
@Override
public void blocked(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, BlockException e)
throws IOException {
FilterUtil.blockRequest(httpServletRequest, httpServletResponse);
}
};
} }
} }