Spring Cloud Alibaba-(4)Sentinel【流控和降级】

Spring Cloud Alibaba-(1)搭建项目环境

Spring Cloud Alibaba-(2)Nacos【服务注册与发现、配置管理】

Spring Cloud Alibaba-(3)OpenFeign【服务调用】

Spring Cloud Alibaba-(4)Sentinel【流控和降级】

1.Sentinel官网-https://sentinelguard.io/zh-cn/index.html

2.流控,即流量控制

流控的主要目的是防止系统过载。当系统请求量过大时,可以通过限制请求的速率来保证系统的稳定性和响应时间。++流控规则(6.4.1节)++可以帮助开发者合理分配系统的流量,避免因流量过大而导致系统崩溃。

3.降级,即熔断降级

降级的主要目的是当系统出现异常或负载过高时,主动降低服务的可用性,以保证核心业务不受影响。++熔断降级策略(6.5节)++通常用于处理服务超时、异常率高等问题。

4.下载 Sentinel 控制台

5.进入目录,cmd 运行(java -Dserver.port=8858 -jar sentinel-dashboard-1.8.6.jar)

6.订单微服务整合Sentinel

6.1 引入Maven依赖

复制代码
<!-- sentinel 限流降级 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

6.2 bootstrap.yml 配置 Sentinel 控制台地址

java 复制代码
server:
  port: 8000

spring:
  profiles:
    active: public
  application:
    name: order-service
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud2024
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        server-addr: http://localhost:8848
      config:
        server-addr: http://localhost:8848
        file-extension: yaml
#        namespace: 94d2d0de-e485-47b1-a375-b423ccc01301
    openfeign:
      httpclient:
        # 连接超时
        connection-timeout: 5000
    sentinel:
      transport:
        dashboard: http://localhost:8858

# Openfeign 整合 sentinel
feign:
  sentinel:
    enabled: true

6.3 直接访问 Sentinel 控制台是空的,要调用接口才会注册到Sentinel

6.4 流控规则

6.4.1 流控规则

|------|-----------------------------------|
| 阈值类型 | QPS:即每秒的访问量,超过阈值会流控 |
| 阈值类型 | 并发线程数:超过阈值会流控 |
| 流控模式 | 直接:直接流控该资源 |
| 流控模式 | 关联:流控关联资源 |
| 流控模式 | 链路:流控入口资源 |
| 流控效果 | 快速失败:超过阈值,直接拒绝 |
| 流控效果 | Warm Up(适用于激增流量):在一段时间内让流量逐渐增加到阈值 |
| 流控效果 | 排队等待(适用于脉冲流量):请求排队,保证稳定的流量速率 |

6.4.2 设置下单接口 QPS 流控规则(每秒访问量超过2次,直接流控该资源,超过2次的请求直接拒绝)

6.4.3 快速请求下单接口,测试流控

6.5 熔断降级策略

|-----------------|----------------------------------------------|
| 慢调用比例(适用于对延迟敏感) | 慢调用比例是指在一定时间内,请求超时或超过阈值所占的比例。如果超过阈值,则触发熔断降级。 |
| 异常比例(适用于对异常敏感) | 异常比例是指在一定时间内,抛出异常请求所占的比例。如果超过阈值,则触发熔断降级。 |
| 异常数(适用于对异常数敏感) | 异常数是指在一定时间内,抛出异常请求次数。如果超过阈值,则触发熔断降级。 |

7.OpenFeign 整合 Sentinel 实现服务出现异常,对服务进行降级

7.1 OpenFeign 创建降级回调类

java 复制代码
package com.dragon.openfeign;

import cn.dev33.satoken.util.SaResult;
import com.dragon.entity.Product;
import org.springframework.stereotype.Component;

@Component
public class ProductServiceFallback implements ProductServiceFeign {
    @Override
    public SaResult getById(String id) {
        return SaResult.ok().setMsg("调用产品服务的 getById() 出现异常,服务降级了");
    }

    @Override
    public SaResult saveOrUpdate(Product productDto) {
        return SaResult.ok().setMsg("调用产品服务的 saveOrUpdate() 出现异常,服务降级了");
    }

    @Override
    public SaResult test() {
        return SaResult.ok().setMsg("调用产品服务的 test() 出现异常,服务降级了");
    }
}

7.2 OpenFeign 开启降级回调 fallback = ProductServiceFallback.class

java 复制代码
package com.dragon.openfeign;

import cn.dev33.satoken.util.SaResult;
import com.dragon.entity.Product;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * value:服务提供方的服务名
 * path:服务提供方的RequestMapping
 * fallback:接口出现异常的降级回调类
 */
@FeignClient(value = "product-service",path = "/product",fallback = ProductServiceFallback.class)
public interface ProductServiceFeign {

    @Operation(summary = "根据ID获取产品")
    @PostMapping("/getById/{id}")
    SaResult getById(@PathVariable("id") String id);

    @Operation(summary = "新增或更新")
    @PostMapping("/saveOrUpdate")
    SaResult saveOrUpdate(@RequestBody Product productDto);

    @Operation(summary = "测试产品服务的 test() 接口")
    @GetMapping("/test")
    SaResult test();
}

7.3 bootstrap.yml 开启Openfeign 整合 sentinel

java 复制代码
# Openfeign 整合 sentinel
feign:
  sentinel:
    enabled: true

7.4 产品服务的 test() 中模拟出现异常

7.5 订单服务调用产品服务,进行测试

8.Sentinel 持久化

8.1 为什么 Sentinel 要实现持久化?

Sentinel 的规则默认保存在内存中,重启服务规则丢失。

8.2 Sentinel + Nacos 实现持久化

8.2.1 引入Maven依赖

复制代码
<!-- sentinel 规则保存到 nacos 配置中心 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

8.2.2 Nacos 配置管理,创建流控规则配置

8.3.3 bootstrap.yml 配置

8.4.4 调用 /order/add/{productId} 接口,Sentinel会自动读取Nacos的流控配置,实现流控

相关推荐
码外生活2 小时前
🎥 手搓一套直播高并发环境!一个后端小白从 0 到 1 的搭建笔记
后端·spring cloud
行百里er2 天前
Spring Insight 里上报 Span 为什么用 JDK HttpClient 而不是 RestTemplate
spring boot·spring cloud·监控
陈皮波比茶2 天前
SpringCloudAlibaba
java·spring cloud·微服务
欢迎来到祖安!2 天前
企业微信 API 接口能力介绍:支持消息收发、图片发送、表情互动、联系人管理等多场景应用
java·前端·数据仓库·spring cloud·企业微信
行百里er3 天前
BlockingQueue:异步批量上报
spring boot·spring cloud·监控
wno7044 天前
Spring Cloud Hystrix Dashboard仪表盘
spring·spring cloud·hystrix
BUG指挥官4 天前
Redis已接入AI
数据库·人工智能·spring boot·redis·spring cloud
wno7045 天前
Spring Cloud Hystrix服务容错
spring·spring cloud·hystrix
苏渡苇5 天前
Spring Insight 里上报 Span 为什么用 JDK HttpClient 而不是 RestTemplate
java·后端·spring·spring cloud·springboot·性能监控
YOU OU6 天前
Spring Cloud Consul
spring cloud·consul·java-consul