From 9757c6ac1c01948129e56b45271a51f95136762f Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Sun, 28 Apr 2019 14:41:49 +0800 Subject: [PATCH 1/5] fix #569. ObjectMapper ignore unknown properties --- .../sentinel/custom/SentinelAutoConfiguration.java | 10 ++++++++++ 1 file changed, 10 insertions(+) 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..df8ca1a5 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 @@ -52,6 +52,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; @@ -179,6 +180,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 +217,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); From 9de1e2dbbfabaf7c4f8d3b5a6159e140f060333b Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Mon, 29 Apr 2019 14:08:20 +0800 Subject: [PATCH 2/5] Polish #614 --- spring-cloud-alibaba-dependencies/pom.xml | 8 +- spring-cloud-alibaba-sentinel/pom.xml | 35 ++++--- .../SentinelWebAutoConfiguration.java | 30 +++++- .../SentinelWebFluxAutoConfiguration.java | 92 +++++++++++++++++++ .../custom/SentinelAutoConfiguration.java | 19 ---- .../main/resources/META-INF/spring.factories | 1 + 6 files changed, 152 insertions(+), 33 deletions(-) create mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebFluxAutoConfiguration.java diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 0de018f0..b498443a 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-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 1ac1a212..37c1ffdf 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,30 @@ true + + com.alibaba.csp + sentinel-web-servlet + true + + + + org.springframework.boot + spring-boot-starter-web + true + + + + com.alibaba.csp + sentinel-spring-webflux-adapter + true + + + + org.springframework.boot + spring-boot-starter-webflux + true + + org.springframework.cloud spring-cloud-starter-openfeign @@ -129,12 +148,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 df8ca1a5..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; @@ -72,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)) @@ -143,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(); 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 From 566753aba715d7f52071d4c26ffff0f13a0a572e Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Mon, 29 Apr 2019 14:10:21 +0800 Subject: [PATCH 3/5] add sentinel webflux example and modify sentinel web example --- spring-cloud-alibaba-examples/pom.xml | 1 + .../sentinel-core-example/pom.xml | 5 ++ .../src/main/resources/flowrule.json | 2 +- .../sentinel-webflux-example/pom.xml | 77 +++++++++++++++++++ .../cloud/examples/MyConfiguration.java | 51 ++++++++++++ .../examples/SentinelWebFluxApplication.java | 32 ++++++++ .../examples/SentinelWebFluxController.java | 47 +++++++++++ .../src/main/resources/application.properties | 10 +++ .../src/main/resources/flowrule.json | 18 +++++ 9 files changed, 242 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/MyConfiguration.java create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxApplication.java create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/SentinelWebFluxController.java create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/application.properties create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/src/main/resources/flowrule.json 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/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index 76afa6d9..264d8447 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 @@ -31,6 +31,11 @@ spring-boot-starter-actuator + + com.alibaba.csp + sentinel-web-servlet + + 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..9db5f0cd --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml @@ -0,0 +1,77 @@ + + + + + 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 + + + + com.alibaba.csp + sentinel-spring-webflux-adapter + + + + + + + + + + + + + + + + + + + + + + + + + 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 + } +] From 93a53eb10eea09843dc20672f48b4e25856d4302 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Mon, 29 Apr 2019 14:23:02 +0800 Subject: [PATCH 4/5] fix build error --- .../zuul/SentinelZuulAutoConfiguration.java | 67 ++++--------------- .../zuul/handler/FallBackProviderHandler.java | 6 +- 2 files changed, 16 insertions(+), 57 deletions(-) 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 From 2047418d8b4db5009dd23f3adb2e5ac8650a5bb7 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Mon, 29 Apr 2019 15:54:55 +0800 Subject: [PATCH 5/5] modify sentinel dependency --- .../sentinel-example/sentinel-core-example/pom.xml | 5 ----- .../sentinel-example/sentinel-webflux-example/pom.xml | 5 ----- spring-cloud-alibaba-sentinel/pom.xml | 2 -- 3 files changed, 12 deletions(-) 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 264d8447..76afa6d9 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 @@ -31,11 +31,6 @@ spring-boot-starter-actuator - - com.alibaba.csp - sentinel-web-servlet - - 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 index 9db5f0cd..c3b4f17c 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-webflux-example/pom.xml @@ -33,11 +33,6 @@ spring-boot-starter-actuator - - com.alibaba.csp - sentinel-spring-webflux-adapter - - diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 37c1ffdf..a88c7f05 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -40,7 +40,6 @@ com.alibaba.csp sentinel-web-servlet - true @@ -52,7 +51,6 @@ com.alibaba.csp sentinel-spring-webflux-adapter - true