refactor sentinel examples ()

refactor: modified Sentinel OpenFeign & RestTemplate examples
pull/3152/head
Steve Rao committed by GitHub
parent 5ab4f22e8e
commit 73a071535e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,8 +18,8 @@
<modules>
<module>sentinel-example/sentinel-core-example</module>
<module>sentinel-example/sentinel-feign-example/sentinel-feign-consumer-example</module>
<module>sentinel-example/sentinel-feign-example/sentinel-feign-provider-example</module>
<module>sentinel-example/sentinel-openfeign-example</module>
<module>sentinel-example/sentinel-resttemplate-example</module>
<module>sentinel-example/sentinel-circuitbreaker-example</module>
<module>sentinel-example/sentinel-webflux-example</module>
<module>sentinel-example/sentinel-spring-cloud-gateway-example</module>

@ -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: '*'

@ -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: '*'

@ -6,13 +6,13 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<version>${revision}</version>
<relativePath>../../../pom.xml</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-feign-provider-example</artifactId>
<name>Spring Cloud Starter Alibaba Sentinel x Feign - Provider Example</name>
<artifactId>sentinel-openfeign-example</artifactId>
<name>Spring Cloud Starter Alibaba Sentinel x Feign - Example</name>
<description>Example demonstrating how to use sentinel with feign</description>
<packaging>jar</packaging>
@ -23,13 +23,12 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>

@ -22,15 +22,16 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author lengleng
* @author raozihao
* @author <a href="mailto:zihaorao@gmail.com">Steve</a>
*/
@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);
}
}

@ -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 <a href="mailto:zihaorao@gmail.com">Steve</a> */ @Component public class EchoServiceFallbackFactory implements FallbackFactory<HttpbinClientFallback> { @Override public HttpbinClientFallback create(Throwable throwable) { return new HttpbinClientFallback(throwable); } }

@ -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
* <p>
* example feign client
* Provide the external exposure interface of the service calling client.
*
* @author raozihao
* @author <a href="mailto:zihaorao@gmail.com">Steve</a>
*/
@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();
}

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

@ -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<FlowRule> flowRules = new ArrayList<FlowRule>(); 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<DegradeRule> degradeRules = new ArrayList<DegradeRule>(); 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"); } }

@ -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 <a href="mailto:zihaorao@gmail.com">Steve</a>
*/
@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();
}
}

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

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

@ -0,0 +1,10 @@
[
{
"resource": "GET:https://httpbin.org/get",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]

@ -6,14 +6,14 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<version>${revision}</version>
<relativePath>../../../pom.xml</relativePath>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-feign-consumer-example</artifactId>
<name>Spring Cloud Starter Alibaba Sentinel x Feign - Consumer Example</name>
<description>Example demonstrating how to use sentinel with feign</description>
<artifactId>sentinel-resttemplate-example</artifactId>
<name>Spring Cloud Starter Alibaba Sentinel x RestTemplate - Example</name>
<description>Example demonstrating how to use sentinel with RestTemplate</description>
<packaging>jar</packaging>
@ -22,31 +22,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
<build>

@ -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 <a href="mailto:zihaorao@gmail.com">Steve</a>
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
public class RestTemplateApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
SpringApplication.run(RestTemplateApplication.class, args);
}
}

@ -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 <a href="mailto:zihaorao@gmail.com">Steve</a>
*/
@Configuration
public class RestTemplateConfiguration {
@LoadBalanced
@Bean
@SentinelRestTemplate
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

@ -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<FlowRule> flowRules = new ArrayList<FlowRule>(); 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<DegradeRule> degradeRules = new ArrayList<DegradeRule>(); 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"); } }

@ -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 <a href="mailto:zihaorao@gmail.com">Steve</a>
*/
@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);
}
}

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

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

@ -0,0 +1,10 @@
[
{
"resource": "GET:https://httpbin.org/get",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
Loading…
Cancel
Save