当前位置: 首页 > article >正文

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

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

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

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

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

Spring Cloud Alibaba-(5)Seata【分布式事务】

Spring Cloud Alibaba-(6)Spring Cloud Gateway【网关】

Spring Cloud Alibaba-(7)RocketMQ【分布式消息队列】

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 控制台地址

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 创建降级回调类

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

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

# 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的流控配置,实现流控


http://www.kler.cn/news/323117.html

相关文章:

  • 每日一题|2516. 每种字符至少取 K 个|双指针、最长子串、字典
  • WebRTC中的维纳滤波器实现详解:基于决策导向的SNR估计
  • Ubuntu一些文件及问题研究分析
  • LabVIEW提高开发效率技巧----使用状态机架构
  • 华为云技术深度解析:Flexus X实例与GitLab的云端协作实践
  • pgsql
  • uniapp view增加删除线
  • 二维数组的创建和初始化
  • 插入排序(insertion sort)
  • self-supervised, weakly supervised, and supervised respectively区别
  • Django中媒体文件的配置
  • UnityHub下载任意版本的Unity包
  • C++ STL初阶(14): map和set
  • C#:动态为Object对象添加新属性的方法
  • Linux 命令 | 每日一学,文本处理三剑客之grep命令实践
  • ssh连接GitHub自定义密钥文件名
  • 【C++前缀和】2731. 移动机器人|1922
  • PHP foo()和@foo()之间有什么区别
  • GAMES101(17~18节,物理材质模型)
  • [go] 迭代器模式
  • 新手答疑 | 零基础该怎么学习嵌入式?嵌入式Linux学习路线是什么?嵌入式开发板推荐?
  • [sql-03] 求阅读至少两章的人数
  • 数据分析工具julius ai如何使用
  • vue 流式加载mp4文件
  • 视频汇聚/视频存储/安防视频监控EasyCVR平台RTMP推流显示离线是什么原因?
  • 秋招即将来临,AIGC 产品经理 快速入门方法论
  • 【计算机网络强化】计网强化笔记
  • http代理池子大小要如何判断?
  • 信息安全工程师(25)网络安全体系框架主要组成和建设内容
  • vite 底层解析