Sentinel4服务熔断

1.Sentinel服务熔断Ribbon环境预说

sentinel整合ribbon+openFeign+fallback
Ribbon系列

  • 启动nacos和sentinel
  • 提供者9003/9004
  • 消费者84

提供者9003/9004
新建cloudalibaba-provider-payment9003/9004,两个一样的做法
POM

  1. <dependencies>
  2. <!--SpringCloud ailibaba nacos -->
  3. <dependency>
  4. <groupId>com.alibaba.cloud</groupId>
  5. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  6. </dependency>
  7. <!-- SpringBoot整合Web组件 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-actuator</artifactId>
  15. </dependency>
  16. <!--日常通用jar包配置-->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-devtools</artifactId>
  20. <scope>runtime</scope>
  21. <optional>true</optional>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. <optional>true</optional>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. </dependencies>

YML

  1. server:
  2. port: 9003
  3. spring:
  4. application:
  5. name: zds-egg-provider-payment9003
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848 #配置Nacos地址
  10. management:
  11. endpoints:
  12. web:
  13. exposure:
  14. include: '*'

主启动

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  4. @SpringBootApplication
  5. @EnableDiscoveryClient
  6. public class PaymentMain9003 {
  7. public static void main(String[] args) {
  8. SpringApplication.run(PaymentMain9003.class, args);
  9. }
  10. }

业务类

  1. @RestController
  2. public class PaymentController {
  3. @Value("${server.port}")
  4. private String serverPort;
  5. @GetMapping(value = "/paymentSQL/{id}")
  6. public SingleResult paymentSQL(@PathVariable("id") Long id){
  7. SingleResult singleResult = new SingleResult();
  8. singleResult.success("","from mysql,serverPort: "+serverPort);
  9. return singleResult;
  10. }
  11. }

测试地址 - http://localhost:9003/paymentSQL/1


消费者84
新建zds-egg-consumer-order84
pom

  1. <dependencies>
  2. <!--SpringCloud openfeign -->
  3. <!-- <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-openfeign</artifactId>
  6. </dependency>-->
  7. <!--SpringCloud ailibaba nacos -->
  8. <dependency>
  9. <groupId>com.alibaba.cloud</groupId>
  10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  11. </dependency>
  12. <!--SpringCloud ailibaba sentinel -->
  13. <dependency>
  14. <groupId>com.alibaba.cloud</groupId>
  15. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  16. </dependency>
  17. <!-- SpringBoot整合Web组件 -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-actuator</artifactId>
  25. </dependency>
  26. <!--日常通用jar包配置-->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-devtools</artifactId>
  30. <scope>runtime</scope>
  31. <optional>true</optional>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.projectlombok</groupId>
  35. <artifactId>lombok</artifactId>
  36. <optional>true</optional>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-test</artifactId>
  41. <scope>test</scope>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.springframework.cloud</groupId>
  45. <artifactId>spring-cloud-openfeign-core</artifactId>
  46. </dependency>
  47. </dependencies>

YML

  1. server:
  2. port: 84
  3. spring:
  4. application:
  5. name: nacos-order-consumer
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: localhost:8848
  10. sentinel:
  11. transport:
  12. #配置Sentinel dashboard地址
  13. dashboard: localhost:8080
  14. #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
  15. port: 8719
  16. #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
  17. service-url:
  18. nacos-user-service: http://zds-egg-provider-payment
  19. # 激活Sentinel对Feign的支持
  20. feign:
  21. sentinel:
  22. enabled: false

主启动

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. @EnableFeignClients
  4. public class OrderNacosMain84 {
  5. public static void main(String[] args) {
  6. SpringApplication.run(OrderNacosMain84.class, args);
  7. }
  8. }

ApplicationContextConfig

  1. @Configuration
  2. public class ApplicationContextConfig {
  3. @Bean
  4. @LoadBalanced
  5. public RestTemplate getRestTemplate() {
  6. return new RestTemplate();
  7. }
  8. }

CircleBreakerController

  1. @RestController
  2. @Slf4j
  3. public class CircleBreakerController {
  4. public static final String SERVICE_URL = "http://zds-egg-provider-payment";
  5. @Resource
  6. private RestTemplate restTemplate;
  7. @RequestMapping("/consumer/fallback/{id}")
  8. @SentinelResource(value = "fallback")//没有配置
  9. public SingleResult fallback(@PathVariable Long id) {
  10. SingleResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, SingleResult.class, id);
  11. if (id == 4) {
  12. throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
  13. } else if (result.getData() == null) {
  14. throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
  15. }
  16. return result;
  17. }
  18. }

测试地址 - http://localhost:84/consumer/fallback/1

2.Sentinel服务熔断无配置

3.Sentinel服务熔断只配置fallback

4.Sentinel服务熔断只配置blockHandler

5. Sentinel服务熔断fallback和blockHandler都配置

6.Sentinel服务熔断exceptionsToIgnore

7.Sentinel服务熔断OpenFeign

修改84模块

  • 84消费者调用提供者9003
  • Feign组件一般是消费侧

pom

  1. <!--SpringCloud openfeign -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-openfeign</artifactId>
  5. </dependency>

yml

  1. # 激活Sentinel对Feign的支持
  2. feign:
  3. sentinel:
  4. enabled: true

业务类
带@Feignclient注解的业务接口,fallback = PaymentFallbackService.class

  1. @FeignClient(value = "zds-egg-provider-payment",fallback = PaymentFallbackService.class)
  2. public interface PaymentService {
  3. @GetMapping(value = "/paymentSQL/{id}")
  4. public SingleResult paymentSQL(@PathVariable("id") Long id);
  5. }
  1. @Component
  2. public class PaymentFallbackService implements PaymentService{
  3. @Override
  4. public SingleResult paymentSQL(Long id) {
  5. return new SingleResult().except(44444,"服务降级返回,---PaymentFallbackService","","");
  6. }
  7. }

Controller

  1. //==================OpenFeign
  2. @Resource
  3. private PaymentService paymentService;
  4. @GetMapping(value = "/consumer/paymentSQL/{id}")
  5. public SingleResult paymentSQL(@PathVariable("id") Long id) {
  6. return paymentService.paymentSQL(id);
  7. }

启动类

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. @EnableFeignClients//<--------------
  4. public class OrderNacosMain84 {
  5. public static void main(String[] args) {
  6. SpringApplication.run(OrderNacosMain84.class, args);
  7. }
  8. }

测试 - http://localhost:84/consumer/paymentSQL/1
测试84调用9003,此时故意关闭9003微服务提供者,84消费侧自动降级,不会被耗死。
熔断框架比较

| - | Sentinel | Hystrix | resilience4j | | —- | —- | —- | —- |

| 隔离策略 | 信号量隔离(并发线程数限流) | 线程池隔商/信号量隔离 | 信号量隔离 |

| 熔断降级策略 | 基于响应时间、异常比率、异常数 | 基于异常比率 | 基于异常比率、响应时间 |

| 实时统计实现 | 滑动窗口(LeapArray) | 滑动窗口(基于RxJava) | Ring Bit Buffer |

| 动态规则配置 | 支持多种数据源 | 支持多种数据源 | 有限支持 |

| 扩展性 | 多个扩展点 | 插件的形式 | 接口的形式 |

| 基于注解的支持 | 支持 | 支持 | 支持 |

| 限流 | 基于QPS,支持基于调用关系的限流 | 有限的支持 | Rate Limiter |

| 流量整形 | 支持预热模式匀速器模式、预热排队模式 | 不支持 | 简单的Rate Limiter模式 |

| 系统自适应保护 | 支持 | 不支持 | 不支持 |

| 控制台 | 提供开箱即用的控制台,可配置规则、查看秒级监控,机器发观等 | 简单的监控查看 | 不提供控制台,可对接其它监控系统 |

8.Sentinel持久化规则

是什么
一旦我们重启应用,sentinel规则将消失,生产环境需要将配置规则进行持久化。
怎么玩
将限流配置规则持久化进Nacos保存,只要刷新8401某个rest地址,sentinel控制台的流控规则就能看到,只要Nacos里面的配置不删除,针对8401上sentinel上的流控规则持续有效。
步骤
修改zds-egg-sentinel8401
POM

  1. <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
  2. <dependency>
  3. <groupId>com.alibaba.csp</groupId>
  4. <artifactId>sentinel-datasource-nacos</artifactId>
  5. </dependency>

YML

  1. server:
  2. port: 8401
  3. spring:
  4. application:
  5. name: zds-egg-sentinel-service
  6. cloud:
  7. nacos:
  8. discovery:
  9. namespace: bcba2f23-08ae-45e7-9ef0-f659cdf60ceb
  10. group: DEFAULT_GROUP
  11. server-addr: 127.0.0.1:8848 #Nacos服务注册中心地址
  12. sentinel:
  13. transport:
  14. dashboard: 127.0.0.1:8080 #配置Sentinel dashboard地址
  15. port: 8719
  16. # Sentinel默认会将Controller方法做context整合,导致链路模式的流控失效
  17. # web-context-unify: false
  18. datasource: #<---------------------------关注点,添加Nacos数据源配置
  19. ds1: # 这里的flow名字随便取 流控规则
  20. nacos:
  21. namespace: bcba2f23-08ae-45e7-9ef0-f659cdf60ceb
  22. server-addr: 127.0.0.1:8848
  23. dataId: zds-egg-sentinel-service
  24. groupId: SENTINEL_GROUP
  25. data-type: json
  26. rule-type: flow
  27. #暴露自己的端点
  28. management:
  29. endpoints:
  30. web:
  31. exposure:
  32. include: '*'
  33. feign:
  34. sentinel:
  35. enabled: true # 激活Sentinel对Feign的支持

添加Nacos业务规则配置

配置内容解析

  1. [{
  2. "resource": "/rateLimit/byUrl",
  3. "IimitApp": "default",
  4. "grade": 1,
  5. "count": 1,
  6. "strategy": 0,
  7. "controlBehavior": 0,
  8. "clusterMode": false
  9. }]
  • resource:资源名称;
  • limitApp:来源应用;
  • grade:阈值类型,0表示线程数, 1表示QPS;
  • count:单机阈值;
  • strategy:流控模式,0表示直接,1表示关联,2表示链路;
  • controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待;
  • clusterMode:是否集群。

启动8401后刷新sentinel发现业务规则有了

快速访问测试接口 - http://localhost:8401/rateLimit/byUrl - 页面返回Blocked by Sentinel (flow limiting)
停止8401再看sentinel - 停机后发现流控规则没有了
新启动8401再看sentinel