From b0273fb1c8d8c60370b912a80594c50e5da7a3f4 Mon Sep 17 00:00:00 2001 From: yuhuangbin Date: Sun, 12 Jan 2020 16:40:35 +0800 Subject: [PATCH] upgrade sentinel version and enhance --- spring-cloud-alibaba-dependencies/pom.xml | 7 +- .../sentinel-core-example/pom.xml | 4 + .../cloud/examples/WebMvcConfiguration.java | 36 ++++++++ .../src/main/resources/application.properties | 3 + .../main/resources/templates/errorPage.html | 10 ++ spring-cloud-alibaba-sentinel/pom.xml | 10 +- .../cloud/sentinel/SentinelConstants.java | 20 ++++ .../cloud/sentinel/SentinelProperties.java | 40 ++++++-- .../SentinelWebAutoConfiguration.java | 92 +++++++++++-------- .../custom/SentinelAutoConfiguration.java | 8 +- .../sentinel/endpoint/SentinelEndpoint.java | 5 +- ...itional-spring-configuration-metadata.json | 11 ++- .../SentinelAutoConfigurationTests.java | 19 +--- .../sentinel/SentinelBeanAutowiredTests.java | 40 ++++---- 14 files changed, 205 insertions(+), 100 deletions(-) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 56467251..a6df0817 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,7 +18,7 @@ Spring Cloud Alibaba Dependencies - 1.6.3 + 1.7.1 3.1.0 0.9.0 1.1.4 @@ -198,6 +198,11 @@ sentinel-api-gateway-adapter-common ${sentinel.version} + + com.alibaba.csp + sentinel-spring-webmvc-adapter + ${sentinel.version} + diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index 3b7c0ce6..2db02dc2 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -30,6 +30,10 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.boot + spring-boot-starter-thymeleaf + diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java new file mode 100644 index 00000000..b4a562fb --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/com/alibaba/cloud/examples/WebMvcConfiguration.java @@ -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"); + } + +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties index 4a60ba53..16ee4a26 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties @@ -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 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html new file mode 100644 index 00000000..04211d7e --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/templates/errorPage.html @@ -0,0 +1,10 @@ + + + + + Title + + +This is error page. + + \ No newline at end of file diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 29c65984..09cb5e8d 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -41,11 +41,6 @@ true - - com.alibaba.csp - sentinel-web-servlet - - org.springframework.boot spring-boot-starter-web @@ -57,6 +52,11 @@ sentinel-spring-webflux-adapter + + com.alibaba.csp + sentinel-spring-webmvc-adapter + + org.springframework.boot spring-boot-starter-webflux diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelConstants.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelConstants.java index ca008a04..dd6b3dc6 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelConstants.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelConstants.java @@ -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"); } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java index 8bef50ad..22d1aff2 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelProperties.java @@ -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 urlPatterns; + private List 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; diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java index 58628310..b5b28cdf 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/SentinelWebAutoConfiguration.java @@ -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 urlCleanerOptional; @Autowired - private Optional urlBlockHandlerOptional; + private Optional blockExceptionHandlerOptional; @Autowired private Optional requestOriginParserOptional; - @PostConstruct - public void init() { - urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler); - urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner); - requestOriginParserOptional.ifPresent(WebCallbackManager::setRequestOriginParser); + @Autowired + private Optional 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 registration = new FilterRegistrationBean<>(); + public SentinelWebInterceptor sentinelWebInterceptor( + SentinelWebMvcConfig sentinelWebMvcConfig) { + return new SentinelWebInterceptor(sentinelWebMvcConfig); + } - SentinelProperties.Filter filterConfig = properties.getFilter(); + @Bean + @ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled", + matchIfMissing = true) + public SentinelWebMvcConfig sentinelWebMvcConfig() { + SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig(); + sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify()); - if (filterConfig.getUrlPatterns() == null - || filterConfig.getUrlPatterns().isEmpty()) { - List defaultPatterns = new ArrayList<>(); - defaultPatterns.add("/*"); - filterConfig.setUrlPatterns(defaultPatterns); + 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()); + } } - 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; - + urlCleanerOptional.ifPresent(sentinelWebMvcConfig::setUrlCleaner); + requestOriginParserOptional.ifPresent(sentinelWebMvcConfig::setOriginParser); + return sentinelWebMvcConfig; } } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java index 7eea97ff..b71d9483 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/custom/SentinelAutoConfiguration.java @@ -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 diff --git a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/endpoint/SentinelEndpoint.java b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/endpoint/SentinelEndpoint.java index 98aace36..c1f38a85 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/endpoint/SentinelEndpoint.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/com/alibaba/cloud/sentinel/endpoint/SentinelEndpoint.java @@ -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()); diff --git a/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json index ec4c63d7..93acd7e5 100644 --- a/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelAutoConfigurationTests.java b/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelAutoConfigurationTests.java index 045ab8b7..ae67bd38 100644 --- a/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelAutoConfigurationTests.java +++ b/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelAutoConfigurationTests.java @@ -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 diff --git a/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelBeanAutowiredTests.java b/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelBeanAutowiredTests.java index cfb67114..a971b52f 100644 --- a/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelBeanAutowiredTests.java +++ b/spring-cloud-alibaba-sentinel/src/test/java/com/alibaba/cloud/sentinel/SentinelBeanAutowiredTests.java @@ -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(); } }