From 73a071535e372b02b6654d09ac9def85ced53cf4 Mon Sep 17 00:00:00 2001 From: Steve Rao Date: Wed, 15 Feb 2023 18:35:55 +0800 Subject: [PATCH] refactor sentinel examples (#3144) refactor: modified Sentinel OpenFeign & RestTemplate examples --- spring-cloud-alibaba-examples/pom.xml | 4 +- .../{sentinel-feign-example => }/readme-zh.md | 0 .../{sentinel-feign-example => }/readme.md | 0 .../src/main/resources/application.yml | 23 --------- .../src/main/resources/application.yml | 16 ------- .../pom.xml | 15 +++--- .../cloud/examples/OpenFeignApplication.java} | 7 +-- .../EchoServiceFallbackFactory.java | 1 + .../configuration/HttpbinClient.java} | 33 +++++++------ .../configuration/HttpbinClientFallback.java | 47 +++++++++++++++++++ .../SentinelRulesConfiguration.java | 1 + .../examples/controller/TestController.java | 24 +++++++--- .../src/main/resources/application.yml | 24 ++++++++++ .../src/main/resources/degraderule.json | 17 +++++++ .../src/main/resources/flowrule.json | 10 ++++ .../pom.xml | 29 ++---------- .../examples/RestTemplateApplication.java} | 9 ++-- .../RestTemplateConfiguration.java | 40 ++++++++++++++++ .../SentinelRulesConfiguration.java | 1 + .../examples/controller/TestController.java} | 27 ++++++++--- .../src/main/resources/application.yml | 19 ++++++++ .../src/main/resources/degraderule.json | 17 +++++++ .../src/main/resources/flowrule.json | 10 ++++ 23 files changed, 264 insertions(+), 110 deletions(-) rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example => }/readme-zh.md (100%) rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example => }/readme.md (100%) delete mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml delete mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-provider-example => sentinel-openfeign-example}/pom.xml (78%) rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java => sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/OpenFeignApplication.java} (85%) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/EchoServiceFallbackFactory.java rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java => sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClient.java} (58%) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClientFallback.java create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-consumer-example => sentinel-openfeign-example}/src/main/java/com/alibaba/cloud/examples/controller/TestController.java (69%) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/application.yml create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/degraderule.json create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/flowrule.json rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-consumer-example => sentinel-resttemplate-example}/pom.xml (62%) rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java => sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/RestTemplateApplication.java} (80%) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java rename spring-cloud-alibaba-examples/sentinel-example/{sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java => sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java} (56%) create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/application.yml create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/degraderule.json create mode 100644 spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/flowrule.json diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 2a85c54ea..adf5c69c6 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -18,8 +18,8 @@ sentinel-example/sentinel-core-example - sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example - sentinel-example/sentinel-feign-example/sentinel-feign-provider-example + sentinel-example/sentinel-openfeign-example + sentinel-example/sentinel-resttemplate-example sentinel-example/sentinel-circuitbreaker-example sentinel-example/sentinel-webflux-example sentinel-example/sentinel-spring-cloud-gateway-example diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/readme-zh.md similarity index 100% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/readme-zh.md rename to spring-cloud-alibaba-examples/sentinel-example/readme-zh.md diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/readme.md similarity index 100% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/readme.md rename to spring-cloud-alibaba-examples/sentinel-example/readme.md diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml deleted file mode 100644 index c981dca1e..000000000 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/resources/application.yml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 18087 - -spring: - application: - name: service-consumer - cloud: - nacos: - discovery: - server-addr: 127.0.0.1:8848 - sentinel: - transport: - dashboard: 127.0.0.1:8081 - -feign: - sentinel: - enabled: true - -management: - endpoints: - web: - exposure: - include: '*' diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml deleted file mode 100644 index f6098ec77..000000000 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/resources/application.yml +++ /dev/null @@ -1,16 +0,0 @@ -server: - port: 18088 - -spring: - application: - name: service-provider - cloud: - nacos: - discovery: - server-addr: 127.0.0.1:8848 - -management: - endpoints: - web: - exposure: - include: '*' diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/pom.xml similarity index 78% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/pom.xml index 8f31d86a7..ecc2d4f56 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/pom.xml @@ -6,13 +6,13 @@ com.alibaba.cloud spring-cloud-alibaba-examples ${revision} - ../../../pom.xml + ../../pom.xml 4.0.0 - sentinel-feign-provider-example - Spring Cloud Starter Alibaba Sentinel x Feign - Provider Example + sentinel-openfeign-example + Spring Cloud Starter Alibaba Sentinel x Feign - Example Example demonstrating how to use sentinel with feign jar @@ -23,13 +23,12 @@ spring-boot-starter-web - com.alibaba.cloud - spring-cloud-starter-alibaba-nacos-discovery + org.springframework.cloud + spring-cloud-starter-openfeign - - org.springframework.boot - spring-boot-starter-actuator + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/OpenFeignApplication.java similarity index 85% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/OpenFeignApplication.java index ca10bb0c7..ed699c63c 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/ConsumerApplication.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/OpenFeignApplication.java @@ -22,15 +22,16 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** - * @author lengleng + * @author raozihao + * @author Steve */ @EnableFeignClients @SpringBootApplication @EnableDiscoveryClient -public class ConsumerApplication { +public class OpenFeignApplication { public static void main(String[] args) { - SpringApplication.run(ConsumerApplication.class, args); + SpringApplication.run(OpenFeignApplication.class, args); } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/EchoServiceFallbackFactory.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/EchoServiceFallbackFactory.java new file mode 100644 index 000000000..71b4114d1 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/EchoServiceFallbackFactory.java @@ -0,0 +1 @@ +/* * Copyright 2013-2023 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.configuration; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; /** * @author raozihao * @author Steve */ @Component public class EchoServiceFallbackFactory implements FallbackFactory { @Override public HttpbinClientFallback create(Throwable throwable) { return new HttpbinClientFallback(throwable); } } \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClient.java similarity index 58% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClient.java index c19ebc7f8..de7ba4938 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/service/EchoService.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClient.java @@ -14,26 +14,29 @@ * limitations under the License. */ -package com.alibaba.cloud.examples.service; +package com.alibaba.cloud.examples.configuration; + import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; /** - * @author lengleng - *

- * example feign client + * Provide the external exposure interface of the service calling client. + * + * @author raozihao + * @author Steve */ -@FeignClient(name = "service-provider") -public interface EchoService { - - /** - * 调用服务提供方的输出接口. - * @param str 用户输入 - * @return echo result - */ - @GetMapping("/echo/{str}") - String echo(@PathVariable("str") String str); +@FeignClient(name = "openfeign-example", url = "https://httpbin.org", contextId = "openfeign-example", fallbackFactory = EchoServiceFallbackFactory.class) +public interface HttpbinClient { + + @GetMapping("/delay/3") + String delay(); + + @GetMapping("/status/500") + String status500(); + + + @GetMapping("/get") + String get(); } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClientFallback.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClientFallback.java new file mode 100644 index 000000000..83a86c9fa --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/HttpbinClientFallback.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2023 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.configuration; + + +/** + * When the service is blown, the fallback operation is performed. + * + * @author raozihao + */ +public class HttpbinClientFallback implements HttpbinClient { + + @Override + public String delay() { + return "delay degrade by sentinel"; + } + + @Override + public String status500() { + return "500 degrade by sentinel"; + } + + @Override + public String get() { + return "get degrade by sentinel"; + } + + private Throwable throwable; + + HttpbinClientFallback(Throwable throwable) { + this.throwable = throwable; + } +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java new file mode 100644 index 000000000..b915eea18 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java @@ -0,0 +1 @@ +package com.alibaba.cloud.examples.configuration; import java.util.ArrayList; import java.util.List; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import jakarta.annotation.PostConstruct; import org.springframework.stereotype.Component; /** * @description * @author ChengPu raozihao * @date 2023/2/11 */ @Component public class SentinelRulesConfiguration { /** * You can configure sentinel rules by referring https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8#%E6%9F%A5%E8%AF%A2%E6%9B%B4%E6%94%B9%E8%A7%84%E5%88%99 */ @PostConstruct public void init() { System.out.println("Load Sentinel Rules start!"); List flowRules = new ArrayList(); FlowRule flowRule = new FlowRule(); flowRule.setResource("GET:https://httpbin.org/get"); flowRule.setCount(1); flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT); flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT); flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); flowRule.setLimitApp("default"); flowRules.add(flowRule); FlowRuleManager.loadRules(flowRules); List degradeRules = new ArrayList(); DegradeRule degradeRule1 = new DegradeRule(); degradeRule1.setResource("GET:https://httpbin.org/status/500"); degradeRule1.setCount(1); degradeRule1.setMinRequestAmount(1); degradeRule1.setTimeWindow(30); degradeRule1.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT); degradeRule1.setLimitApp("default"); degradeRules.add(degradeRule1); DegradeRule degradeRule2 = new DegradeRule(); degradeRule2.setResource("GET:https://httpbin.org/delay/3"); degradeRule2.setCount(1); degradeRule2.setGrade(RuleConstant.DEGRADE_GRADE_RT); degradeRule2.setSlowRatioThreshold(0.1); degradeRule2.setMinRequestAmount(1); degradeRule2.setTimeWindow(30); degradeRule2.setLimitApp("default"); degradeRules.add(degradeRule2); DegradeRuleManager.loadRules(degradeRules); System.out.println("Load Sentinel Rules end!"); } } \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java similarity index 69% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java index dc3eb828d..fc9d19427 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java @@ -16,25 +16,35 @@ package com.alibaba.cloud.examples.controller; -import com.alibaba.cloud.examples.service.EchoService; +import com.alibaba.cloud.examples.configuration.HttpbinClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; /** - * @author lengleng + * @author raozihao + * @author Steve */ @RestController public class TestController { @Autowired - private EchoService echoService; + private HttpbinClient httpbinClient; - @GetMapping("/echo-feign/{str}") - public String feign(@PathVariable String str) { - return echoService.echo(str); + @GetMapping("/rt") + public String delay() { + return httpbinClient.delay(); + } + + @GetMapping("/exp") + public String exp() { + return httpbinClient.status500(); + } + + @GetMapping("/get") + public String get() { + return httpbinClient.get(); } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/application.yml new file mode 100644 index 000000000..601088869 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/application.yml @@ -0,0 +1,24 @@ +server: + port: 18087 + +spring: + application: + name: openfeign-example + cloud: + sentinel: + transport: + dashboard: 127.0.0.1:8081 +# Don't support run in jar by using configuration file method now, refer to https://github.com/alibaba/spring-cloud-alibaba/issues/3033 +# datasource: +# ds1.file: +# file: "classpath: degraderule.json" +# ruleType: "degrade" +# dataType: "json" +# ds2.file: +# file: "classpath: flowrule.json" +# ruleType: "flow" +# dataType: "json" + +feign: + sentinel: + enabled: true diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/degraderule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/degraderule.json new file mode 100644 index 000000000..5383758c0 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/degraderule.json @@ -0,0 +1,17 @@ +[ + { + "resource": "GET:https://httpbin.org/status/500", + "count": 1, + "grade": 2, + "minRequestAmount": 1, + "timeWindow": 30 + }, + { + "resource": "GET:https://httpbin.org/delay/3", + "count": 1, + "grade": 0, + "slowRatioThreshold": 0.1, + "minRequestAmount": 1, + "timeWindow": 30 + } +] diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/flowrule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/flowrule.json new file mode 100644 index 000000000..98ce4f3b0 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-openfeign-example/src/main/resources/flowrule.json @@ -0,0 +1,10 @@ +[ + { + "resource": "GET:https://httpbin.org/get", + "controlBehavior": 0, + "count": 1, + "grade": 1, + "limitApp": "default", + "strategy": 0 + } +] diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/pom.xml similarity index 62% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/pom.xml index be3b835ad..97655c8dd 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/pom.xml @@ -6,14 +6,14 @@ com.alibaba.cloud spring-cloud-alibaba-examples ${revision} - ../../../pom.xml + ../../pom.xml 4.0.0 - sentinel-feign-consumer-example - Spring Cloud Starter Alibaba Sentinel x Feign - Consumer Example - Example demonstrating how to use sentinel with feign + sentinel-resttemplate-example + Spring Cloud Starter Alibaba Sentinel x RestTemplate - Example + Example demonstrating how to use sentinel with RestTemplate jar @@ -22,31 +22,10 @@ org.springframework.boot spring-boot-starter-web - - com.alibaba.cloud - spring-cloud-starter-alibaba-nacos-discovery - - - - org.springframework.boot - spring-boot-starter-actuator - - - - org.springframework.cloud - spring-cloud-starter-openfeign - - com.alibaba.cloud spring-cloud-starter-alibaba-sentinel - - - org.springframework.cloud - spring-cloud-starter-loadbalancer - - diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/RestTemplateApplication.java similarity index 80% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/RestTemplateApplication.java index 5f7767dc1..44c508767 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/ProviderApplication.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/RestTemplateApplication.java @@ -18,17 +18,16 @@ package com.alibaba.cloud.examples; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** - * @author lengleng + * @author raozihao + * @author Steve */ -@EnableDiscoveryClient @SpringBootApplication -public class ProviderApplication { +public class RestTemplateApplication { public static void main(String[] args) { - SpringApplication.run(ProviderApplication.class, args); + SpringApplication.run(RestTemplateApplication.class, args); } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java new file mode 100644 index 000000000..8e8b03c75 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/RestTemplateConfiguration.java @@ -0,0 +1,40 @@ +/* + * Copyright 2013-2023 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.configuration; + + +import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * @author raozihao + * @author Steve + */ +@Configuration +public class RestTemplateConfiguration { + + @LoadBalanced + @Bean + @SentinelRestTemplate + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java new file mode 100644 index 000000000..f55246698 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/configuration/SentinelRulesConfiguration.java @@ -0,0 +1 @@ +package com.alibaba.cloud.examples.configuration; import java.util.ArrayList; import java.util.List; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; import jakarta.annotation.PostConstruct; import org.springframework.stereotype.Component; /** * @description * @author ChengPu raozihao * @date 2023/2/11 */ @Component public class SentinelRulesConfiguration { /** * You can configure sentinel rules by referring https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8#%E6%9F%A5%E8%AF%A2%E6%9B%B4%E6%94%B9%E8%A7%84%E5%88%99 */ @PostConstruct public void init() { System.out.println("Load Sentinel Rules start!"); List flowRules = new ArrayList(); FlowRule flowRule = new FlowRule(); flowRule.setResource("GET:https://httpbin.org/get"); flowRule.setCount(1); flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT); flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT); flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); flowRule.setLimitApp("default"); flowRules.add(flowRule); FlowRuleManager.loadRules(flowRules); List degradeRules = new ArrayList(); DegradeRule degradeRule1 = new DegradeRule(); degradeRule1.setResource("GET:https://httpbin.org/status/500"); degradeRule1.setCount(1); degradeRule1.setMinRequestAmount(1); degradeRule1.setTimeWindow(30); degradeRule1.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT); degradeRule1.setLimitApp("default"); degradeRules.add(degradeRule1); DegradeRule degradeRule2 = new DegradeRule(); degradeRule2.setResource("GET:https://httpbin.org/delay/3"); degradeRule2.setCount(1); degradeRule2.setGrade(RuleConstant.DEGRADE_GRADE_RT); degradeRule2.setSlowRatioThreshold(0.1); degradeRule2.setMinRequestAmount(1); degradeRule2.setTimeWindow(30); degradeRule2.setLimitApp("default"); degradeRules.add(degradeRule2); DegradeRuleManager.loadRules(degradeRules); System.out.println("Load Sentinel Rules end!"); } } \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java similarity index 56% rename from spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java rename to spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java index 5a469b811..d52dc1716 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-feign-example/sentinel-feign-provider-example/src/main/java/com/alibaba/cloud/examples/controller/EchoController.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/java/com/alibaba/cloud/examples/controller/TestController.java @@ -16,19 +16,34 @@ package com.alibaba.cloud.examples.controller; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; /** - * @author lengleng + * @author raozihao + * @author Steve */ @RestController -public class EchoController { +public class TestController { - @GetMapping("/echo/{str}") - public String echo(@PathVariable String str) { - return "provider-" + str; + @Autowired + RestTemplate restTemplate; + + @GetMapping("/exp") + public String exp() { + return restTemplate.getForObject("https://httpbin.org/status/500", String.class); + } + + @GetMapping("/rt") + public String rt() { + return restTemplate.getForObject("https://httpbin.org/delay/3", String.class); + } + + @GetMapping("/get") + public String get() { + return restTemplate.getForObject("https://httpbin.org/get", String.class); } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/application.yml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/application.yml new file mode 100644 index 000000000..8fea0f9b0 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/application.yml @@ -0,0 +1,19 @@ +server: + port: 18088 + +spring: + application: + name: sentinel-resttemplate-example +# Don't support run in jar by using configuration file method now, refer to https://github.com/alibaba/spring-cloud-alibaba/issues/3033 +# cloud: +# sentinel: +# datasource: +# ds1.file: +# file: "classpath: degraderule.json" +# ruleType: "degrade" +# dataType: "json" +# ds2.file: +# file: "classpath: flowrule.json" +# ruleType: "flow" +# dataType: "json" + diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/degraderule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/degraderule.json new file mode 100644 index 000000000..5383758c0 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/degraderule.json @@ -0,0 +1,17 @@ +[ + { + "resource": "GET:https://httpbin.org/status/500", + "count": 1, + "grade": 2, + "minRequestAmount": 1, + "timeWindow": 30 + }, + { + "resource": "GET:https://httpbin.org/delay/3", + "count": 1, + "grade": 0, + "slowRatioThreshold": 0.1, + "minRequestAmount": 1, + "timeWindow": 30 + } +] diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/flowrule.json b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/flowrule.json new file mode 100644 index 000000000..98ce4f3b0 --- /dev/null +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-resttemplate-example/src/main/resources/flowrule.json @@ -0,0 +1,10 @@ +[ + { + "resource": "GET:https://httpbin.org/get", + "controlBehavior": 0, + "count": 1, + "grade": 1, + "limitApp": "default", + "strategy": 0 + } +]