微服务之限流降级Sentinel

910 阅读1分钟

背景

雪崩效应:在微服务分布式系统中,服务间都是相互调用的,不考虑外部因素,当服务A不可用时,调用服务A的服务B则会强制等待直至超时,一个请求对应一个线程,强制等待即线程阻塞,在请求超时时这个线程才会被释放,在高并发的应用系统中,随着时间推移,到最后线程会被耗尽,服务B也不可用。下层服务导致上层服务不可用并逐渐扩大服务范围。

常见容错方案

  • 超时
  • 限流
  • 仓壁模式
  • 断路器

整合Sentinel

  1. 依赖
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 配置
management:
  endpoints:
    web:
      exposure:
        include: '*'
  1. 启动访问http://localhost:9001/actuator/sentinel,响应结果如下则整合成功
{
    "appName":"mg-user-service",
    "consoleServer":"localhost:8080",
    "coldFactor":"3",
    "rules":{
        "systemRules":[],
        "authorityRule":[],
        "paramFlowRule":[],
        "flowRules":[],
        "degradeRules":[]
    },
    "metricsFileCharset":"UTF-8",
    "filter":{
        "order":-2147483648,
        "urlPatterns":[
            "/*"
        ],
        "enabled":true
    },
    "totalMetricsFileCount":6,
    "datasource":{},
    "clientIp":"192.168.0.102",
    "clientPort":"8719",
    "logUsePid":false,
    "metricsFileSize":52428800,
    "logDir":"/Users/gaozaoshun/logs/csp/"
}

搭建Sentinel控制台