diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml
index 80e48b0b..ccb740da 100644
--- a/spring-cloud-alibaba-dependencies/pom.xml
+++ b/spring-cloud-alibaba-dependencies/pom.xml
@@ -17,7 +17,7 @@
Spring Cloud Alibaba Dependencies
- 1.5.2
+ 1.6.0
3.1.0
0.5.0
1.0.0
@@ -187,6 +187,12 @@
sentinel-cluster-client-default
${sentinel.version}
+
+ com.alibaba.csp
+ sentinel-spring-webflux-adapter
+ ${sentinel.version}
+
+
diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml
index a5b3e948..7e810f19 100644
--- a/spring-cloud-alibaba-examples/pom.xml
+++ b/spring-cloud-alibaba-examples/pom.xml
@@ -21,6 +21,7 @@
sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example
sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example
sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api
+ sentinel-example/sentinel-webflux-example
nacos-example/nacos-discovery-example
nacos-example/nacos-config-example
nacos-example/nacos-gateway-example
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json
index 8aacfc33..63afc91a 100644
--- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/flowrule.json
@@ -1,6 +1,6 @@
[
{
- "resource": "resource",
+ "resource": "/hello",
"controlBehavior": 0,
"count": 1,
"grade": 1,
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml
new file mode 100644
index 00000000..c3b4f17c
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml
@@ -0,0 +1,72 @@
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-alibaba-examples
+ 0.9.1.BUILD-SNAPSHOT
+ ../../pom.xml
+
+ 4.0.0
+
+
+ sentinel-webflux-example
+ jar
+ Example demonstrating how to use sentinel with webflux
+
+
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-alibaba-sentinel
+
+
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+
+
+
+ org.springframework.boot
+ spring-boot-starter-actuator
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+ org.apache.maven.plugins
+ maven-deploy-plugin
+ ${maven-deploy-plugin.version}
+
+ true
+
+
+
+
+
+
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/MyConfiguration.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/MyConfiguration.java
new file mode 100644
index 00000000..9d95923a
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/MyConfiguration.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 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 org.springframework.cloud.alibaba.cloud.examples;
+
+import static org.springframework.web.reactive.function.BodyInserters.fromObject;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.web.reactive.function.server.ServerResponse;
+import org.springframework.web.server.ServerWebExchange;
+
+import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
+
+import reactor.core.publisher.Mono;
+
+/**
+ * @author Jim
+ */
+@Configuration
+public class MyConfiguration {
+
+ @Bean
+ public BlockRequestHandler blockRequestHandler() {
+ return new BlockRequestHandler() {
+ @Override
+ public Mono handleRequest(ServerWebExchange exchange,
+ Throwable t) {
+ return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
+ .contentType(MediaType.APPLICATION_JSON_UTF8)
+ .body(fromObject("block"));
+ }
+ };
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxApplication.java
new file mode 100644
index 00000000..87a42503
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxApplication.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 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 org.springframework.cloud.alibaba.cloud.examples;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author Jim
+ */
+@SpringBootApplication
+public class SentinelWebFluxApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(SentinelWebFluxApplication.class, args);
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxController.java
new file mode 100644
index 00000000..2c5f9f1d
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxController.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 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 org.springframework.cloud.alibaba.cloud.examples;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
+
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+/**
+ * @author Jim
+ */
+@RestController
+public class SentinelWebFluxController {
+
+ @GetMapping("/mono")
+ public Mono mono() {
+ return Mono.just("simple string")
+ // transform the publisher here.
+ .transform(new SentinelReactorTransformer<>("mono"));
+ }
+
+ @GetMapping("/flux")
+ public Flux flux() {
+ return Flux.fromArray(new String[] { "a", "b", "c" })
+ // transform the publisher here.
+ .transform(new SentinelReactorTransformer<>("flux"));
+ }
+
+}
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/application.properties
new file mode 100644
index 00000000..e9750c02
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/application.properties
@@ -0,0 +1,10 @@
+spring.application.name=sentinel-example
+server.port=18084
+management.endpoints.web.exposure.include=*
+spring.cloud.sentinel.transport.dashboard=localhost:8080
+spring.cloud.sentinel.eager=true
+
+
+spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
+spring.cloud.sentinel.datasource.ds1.file.data-type=json
+spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
\ No newline at end of file
diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/flowrule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/flowrule.json
new file mode 100644
index 00000000..10e022b8
--- /dev/null
+++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/flowrule.json
@@ -0,0 +1,18 @@
+[
+ {
+ "resource": "/mono",
+ "controlBehavior": 0,
+ "count": 0,
+ "grade": 1,
+ "limitApp": "default",
+ "strategy": 0
+ },
+ {
+ "resource": "/flux",
+ "controlBehavior": 0,
+ "count": 0,
+ "grade": 1,
+ "limitApp": "default",
+ "strategy": 0
+ }
+]
diff --git a/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelZuulAutoConfiguration.java b/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelZuulAutoConfiguration.java
index 35577cae..311b4609 100644
--- a/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelZuulAutoConfiguration.java
+++ b/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/SentinelZuulAutoConfiguration.java
@@ -16,26 +16,20 @@
package org.springframework.cloud.alibaba.sentinel.zuul;
-import static org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration.PREFIX;
+import static org.springframework.cloud.commons.util.InetUtilsProperties.PREFIX;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.alibaba.sentinel.zuul.handler.FallBackProviderHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.core.env.Environment;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultRequestOriginParser;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultUrlCleaner;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.RequestOriginParser;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.UrlCleaner;
-import com.alibaba.csp.sentinel.adapter.zuul.filters.SentinelErrorFilter;
-import com.alibaba.csp.sentinel.adapter.zuul.filters.SentinelPostFilter;
-import com.alibaba.csp.sentinel.adapter.zuul.filters.SentinelPreFilter;
-import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties;
-import com.alibaba.csp.sentinel.util.StringUtil;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.DefaultRequestOriginParser;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
import com.netflix.zuul.ZuulFilter;
@@ -48,40 +42,6 @@ import com.netflix.zuul.ZuulFilter;
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class SentinelZuulAutoConfiguration {
- @Autowired
- private Environment environment;
-
- public static final String PREFIX = "spring.cloud.sentinel.zuul";
-
- @Bean
- public SentinelZuulProperties sentinelZuulProperties() {
- SentinelZuulProperties properties = new SentinelZuulProperties();
- String enabledStr = environment.getProperty(PREFIX + "." + "enabled");
- String preOrderStr = environment.getProperty(PREFIX + "." + "order.pre");
- String postOrderStr = environment.getProperty(PREFIX + "." + "order.post");
- String errorOrderStr = environment.getProperty(PREFIX + "." + "order.error");
- if (StringUtil.isNotEmpty(enabledStr)) {
- Boolean enabled = Boolean.valueOf(enabledStr);
- properties.setEnabled(enabled);
- }
- if (StringUtil.isNotEmpty(preOrderStr)) {
- properties.getOrder().setPre(Integer.parseInt(preOrderStr));
- }
- if (StringUtil.isNotEmpty(postOrderStr)) {
- properties.getOrder().setPost(Integer.parseInt(postOrderStr));
- }
- if (StringUtil.isNotEmpty(errorOrderStr)) {
- properties.getOrder().setError(Integer.parseInt(errorOrderStr));
- }
- return properties;
- }
-
- @Bean
- @ConditionalOnMissingBean(UrlCleaner.class)
- public UrlCleaner urlCleaner() {
- return new DefaultUrlCleaner();
- }
-
@Bean
@ConditionalOnMissingBean(RequestOriginParser.class)
public RequestOriginParser requestOriginParser() {
@@ -89,20 +49,19 @@ public class SentinelZuulAutoConfiguration {
}
@Bean
- public ZuulFilter preFilter(SentinelZuulProperties sentinelZuulProperties,
- UrlCleaner urlCleaner, RequestOriginParser requestOriginParser) {
- return new SentinelPreFilter(sentinelZuulProperties, urlCleaner,
- requestOriginParser);
+ public ZuulFilter sentinelZuulPreFilter() {
+ // We can also provider the filter order in the constructor.
+ return new SentinelZuulPreFilter();
}
@Bean
- public ZuulFilter postFilter(SentinelZuulProperties sentinelZuulProperties) {
- return new SentinelPostFilter(sentinelZuulProperties);
+ public ZuulFilter sentinelZuulPostFilter() {
+ return new SentinelZuulPostFilter();
}
@Bean
- public ZuulFilter errorFilter(SentinelZuulProperties sentinelZuulProperties) {
- return new SentinelErrorFilter(sentinelZuulProperties);
+ public ZuulFilter sentinelZuulErrorFilter() {
+ return new SentinelZuulErrorFilter();
}
@Bean
diff --git a/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/handler/FallBackProviderHandler.java b/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/handler/FallBackProviderHandler.java
index 564fc446..0628d5d5 100644
--- a/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/handler/FallBackProviderHandler.java
+++ b/spring-cloud-alibaba-sentinel-zuul/src/main/java/org/springframework/cloud/alibaba/sentinel/zuul/handler/FallBackProviderHandler.java
@@ -8,9 +8,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultBlockFallbackProvider;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.ZuulBlockFallbackManager;
-import com.alibaba.csp.sentinel.adapter.zuul.fallback.ZuulBlockFallbackProvider;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.DefaultBlockFallbackProvider;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;
+import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider;
/**
* @author tiger
diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml
index 1ac1a212..a88c7f05 100644
--- a/spring-cloud-alibaba-sentinel/pom.xml
+++ b/spring-cloud-alibaba-sentinel/pom.xml
@@ -15,11 +15,6 @@
-
- com.alibaba.csp
- sentinel-web-servlet
-
-
com.alibaba.csp
sentinel-transport-simple-http
@@ -42,6 +37,28 @@
true
+
+ com.alibaba.csp
+ sentinel-web-servlet
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+ true
+
+
+
+ com.alibaba.csp
+ sentinel-spring-webflux-adapter
+
+
+
+ org.springframework.boot
+ spring-boot-starter-webflux
+ true
+
+
org.springframework.cloud
spring-cloud-starter-openfeign
@@ -129,12 +146,6 @@
provided
true
-
- org.springframework.boot
- spring-boot-starter-web
- provided
- true
-
org.springframework.boot
diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java
index 5a73f2cb..37d77137 100644
--- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java
+++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java
@@ -18,26 +18,35 @@ package org.springframework.cloud.alibaba.sentinel;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
+import javax.annotation.PostConstruct;
import javax.servlet.Filter;
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;
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 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
*/
@Configuration
-@ConditionalOnWebApplication
+@ConditionalOnWebApplication(type = Type.SERVLET)
+@ConditionalOnClass(CommonFilter.class)
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
@EnableConfigurationProperties(SentinelProperties.class)
public class SentinelWebAutoConfiguration {
@@ -48,6 +57,22 @@ public class SentinelWebAutoConfiguration {
@Autowired
private SentinelProperties properties;
+ @Autowired
+ private Optional urlCleanerOptional;
+
+ @Autowired
+ private Optional urlBlockHandlerOptional;
+
+ @Autowired
+ private Optional requestOriginParserOptional;
+
+ @PostConstruct
+ public void init() {
+ urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
+ urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
+ requestOriginParserOptional.ifPresent(WebCallbackManager::setRequestOriginParser);
+ }
+
@Bean
@ConditionalOnProperty(name = "spring.cloud.sentinel.filter.enabled", matchIfMissing = true)
public FilterRegistrationBean sentinelFilter() {
@@ -66,7 +91,8 @@ public class SentinelWebAutoConfiguration {
Filter filter = new CommonFilter();
registration.setFilter(filter);
registration.setOrder(filterConfig.getOrder());
- log.info("[Sentinel Starter] register Sentinel with urlPatterns: {}.",
+ log.info(
+ "[Sentinel Starter] register Sentinel CommonFilter with urlPatterns: {}.",
filterConfig.getUrlPatterns());
return registration;
diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebFluxAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebFluxAutoConfiguration.java
new file mode 100644
index 00000000..f36f326e
--- /dev/null
+++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebFluxAutoConfiguration.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 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
+ *
+ * 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 org.springframework.cloud.alibaba.sentinel;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import javax.annotation.PostConstruct;
+
+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;
+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.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+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 Jim
+ */
+@Configuration
+@ConditionalOnWebApplication(type = Type.REACTIVE)
+@ConditionalOnClass(SentinelReactorTransformer.class)
+@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
+@EnableConfigurationProperties(SentinelProperties.class)
+public class SentinelWebFluxAutoConfiguration {
+
+ private static final Logger log = LoggerFactory
+ .getLogger(SentinelWebFluxAutoConfiguration.class);
+
+ private final List viewResolvers;
+ private final ServerCodecConfigurer serverCodecConfigurer;
+
+ @Autowired
+ private Optional blockRequestHandler;
+
+ public SentinelWebFluxAutoConfiguration(
+ ObjectProvider> viewResolvers,
+ ServerCodecConfigurer serverCodecConfigurer) {
+ this.viewResolvers = viewResolvers.getIfAvailable(Collections::emptyList);
+ this.serverCodecConfigurer = serverCodecConfigurer;
+ }
+
+ @PostConstruct
+ public void init() {
+ blockRequestHandler.ifPresent(WebFluxCallbackManager::setBlockHandler);
+ }
+
+ @Bean
+ @Order(-2)
+ @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)
+ public SentinelWebFluxFilter sentinelWebFluxFilter() {
+ log.info("[Sentinel Starter] register Sentinel SentinelWebFluxFilter");
+ return new SentinelWebFluxFilter();
+ }
+
+}
\ No newline at end of file
diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java
index d2423e00..05984c24 100644
--- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java
+++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java
@@ -16,8 +16,6 @@
package org.springframework.cloud.alibaba.sentinel.custom;
-import java.util.Optional;
-
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,10 +33,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
-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.config.WebServletConfig;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.config.SentinelConfig;
@@ -52,6 +46,7 @@ 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;
@@ -71,15 +66,6 @@ public class SentinelAutoConfiguration {
@Autowired
private SentinelProperties properties;
- @Autowired
- private Optional urlCleanerOptional;
-
- @Autowired
- private Optional urlBlockHandlerOptional;
-
- @Autowired
- private Optional requestOriginParserOptional;
-
@PostConstruct
private void init() {
if (StringUtils.isEmpty(System.getProperty(LogBase.LOG_DIR))
@@ -142,10 +128,6 @@ public class SentinelAutoConfiguration {
WebServletConfig.setBlockPage(properties.getServlet().getBlockPage());
}
- urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
- urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
- requestOriginParserOptional.ifPresent(WebCallbackManager::setRequestOriginParser);
-
// earlier initialize
if (properties.isEager()) {
InitExecutor.doInit();
@@ -179,6 +161,11 @@ public class SentinelAutoConfiguration {
private ObjectMapper objectMapper = new ObjectMapper();
+ public SentinelConverterConfiguration() {
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
+ false);
+ }
+
@Bean("sentinel-json-flow-converter")
public JsonConverter jsonFlowConverter() {
return new JsonConverter(objectMapper, FlowRule.class);
@@ -211,6 +198,10 @@ public class SentinelAutoConfiguration {
private XmlMapper xmlMapper = new XmlMapper();
+ public SentinelXmlConfiguration() {
+ xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ }
+
@Bean("sentinel-xml-flow-converter")
public XmlConverter xmlFlowConverter() {
return new XmlConverter(xmlMapper, FlowRule.class);
diff --git a/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/spring.factories
index d964e3b0..20f314cc 100644
--- a/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/spring.factories
+++ b/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/spring.factories
@@ -1,5 +1,6 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alibaba.sentinel.SentinelWebAutoConfiguration,\
+org.springframework.cloud.alibaba.sentinel.SentinelWebFluxAutoConfiguration,\
org.springframework.cloud.alibaba.sentinel.endpoint.SentinelEndpointAutoConfiguration,\
org.springframework.cloud.alibaba.sentinel.custom.SentinelAutoConfiguration,\
org.springframework.cloud.alibaba.sentinel.feign.SentinelFeignAutoConfiguration