返回

揭秘Sentinel:Spring Cloud Alibaba的断路器与熔断降级策略

后端

Sentinel简介

Sentinel,由阿里巴巴开源,是一款轻量级、高性能的熔断降级框架,旨在保障微服务架构中服务的稳定性和可用性,免受突发流量或故障影响。

Sentinel的主要功能

  • 断路器: 隔离故障服务,避免故障蔓延。
  • 熔断: 熔断故障频发的服务,直至故障修复。
  • 降级: 当服务故障时,将请求重定向或返回默认值,降低系统影响。
  • 限流: 控制并发量,防止服务过载。
  • 授权: 验证请求的合法性,防止未授权访问。
  • 系统负载异常: 监控系统负载,触发熔断或降级策略,保护系统免于过载。

Sentinel在Spring Cloud Alibaba中的应用

Sentinel与Spring Cloud Alibaba深度集成,通过简单配置即可启用其功能。

Sentinel的集成

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-alibaba-sentinel</artifactId>
  <version>2.2.6.RELEASE</version>
</dependency>

在application.yml中配置:

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080
      filter:
        enabled: true

Sentinel的使用

使用@SentinelResource注解标记需要保护的方法:

@RestController
public class SentinelController {

  @SentinelResource(value = "hello")
  @GetMapping("/hello")
  public String hello() {
    return "Hello, Sentinel!";
  }

}

Sentinel的使用示例

断路器示例

当服务故障时,断路器自动隔离该服务,返回备用响应。

@RestController
public class SentinelController {

  @SentinelResource(value = "hello", fallback = "helloFallback")
  @GetMapping("/hello")
  public String hello() {
    // 模拟服务故障
    throw new RuntimeException("Hello, Sentinel!");
  }

  @GetMapping("/helloFallback")
  public String helloFallback() {
    return "Hello, Sentinel! Fallback!";
  }

}

熔断示例

当服务故障频繁时,熔断器熔断该服务,阻止新请求访问。

@RestController
public class SentinelController {

  @SentinelResource(value = "hello", fallback = "helloFallback", exceptionsToIgnore = {NullPointerException.class})
  @GetMapping("/hello")
  public String hello() {
    // 模拟服务故障
    if (Math.random() < 0.5) {
      throw new RuntimeException("Hello, Sentinel!");
    }

    return "Hello, Sentinel!";
  }

  @GetMapping("/helloFallback")
  public String helloFallback() {
    return "Hello, Sentinel! Fallback!";
  }

}

降级示例

当服务故障时,降级策略重定向请求或返回默认值,降低系统影响。

@RestController
public class SentinelController {

  @SentinelResource(value = "hello", fallback = "helloFallback", defaultFallback = "helloDefaultFallback")
  @GetMapping("/hello")
  public String hello() {
    // 模拟服务故障
    if (Math.random() < 0.5) {
      throw new RuntimeException("Hello, Sentinel!");
    }

    return "Hello, Sentinel!";
  }

  @GetMapping("/helloFallback")
  public String helloFallback() {
    return "Hello, Sentinel! Fallback!";
  }

  @GetMapping("/helloDefaultFallback")
  public String helloDefaultFallback() {
    return "Hello, Sentinel! Default Fallback!";
  }

}

限流示例

当服务并发量达到上限时,限流策略拒绝新请求。

@RestController
public class SentinelController {

  @SentinelResource(value = "hello", blockHandler = "helloBlockHandler")
  @GetMapping("/hello")
  public String hello() {
    // 模拟服务过载
    if (Math.random() < 0.5) {
      Thread.sleep(100);
    }

    return "Hello, Sentinel!";
  }

  @GetMapping("/helloBlockHandler")
  public String helloBlockHandler(BlockException ex) {
    return "Hello, Sentinel! BlockHandler!";
  }

}

授权示例

当请求不满足授权规则时,授权策略拒绝该请求。

@RestController
public class SentinelController {

  @SentinelResource(value = "hello", author