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

news/2024/9/23 13:15:48/

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: 8000spring:profiles:active: publicapplication:name: order-servicedatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springcloud2024username: rootpassword: 123456cloud:nacos:discovery:server-addr: http://localhost:8848config:server-addr: http://localhost:8848file-extension: yaml
#        namespace: 94d2d0de-e485-47b1-a375-b423ccc01301openfeign:httpclient:# 连接超时connection-timeout: 5000sentinel: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 {@Overridepublic SaResult getById(String id) {return SaResult.ok().setMsg("调用产品服务的 getById() 出现异常,服务降级了");}@Overridepublic SaResult saveOrUpdate(Product productDto) {return SaResult.ok().setMsg("调用产品服务的 saveOrUpdate() 出现异常,服务降级了");}@Overridepublic 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.ppmy.cn/news/1529330.html

相关文章

神经网络_使用tensorflow对fashion mnist衣服数据集分类

from tensorflow import keras import matplotlib.pyplot as plt1.数据预处理 1.1 下载数据集 fashion_mnist keras.datasets.fashion_mnist #下载 fashion mnist数据集 (train_images, train_labels),(test_images, test_labels) fashion_mnist.load_data()print("t…

高刷显示器哪个好?540Hz才有资格称高刷

高刷显示器哪个好&#xff1f;说实话&#xff0c;540Hz这些才能成为高刷显示器&#xff0c;什么200,240的&#xff0c;都不够高&#xff0c;什么是从容&#xff0c;有我不用才叫从容。下面我们一起来看看540Hz的高刷显示器都有哪些吧&#xff01; 1.高刷显示器哪个好 - 蚂蚁电…

WAN广域网技术--PPP和PPPoE

广域网基础概述 广域网&#xff08;Wide Area Network&#xff0c;WAN&#xff09;是一种覆盖广泛地区的计算机网络&#xff0c;它连接不同地理位置的计算机、服务器和设备。广域网通常用于连接不同城市、州或国家之间的网络&#xff0c;它通过互联网服务提供商&#xff08;ISP…

逻辑回归 和 支持向量机(SVM)比较

为了更好地理解为什么在二分类问题中使用 SVM&#xff0c;逻辑回归的区别&#xff0c;我们需要深入了解这两种算法的区别、优势、劣势&#xff0c;以及它们适用于不同场景的原因。 逻辑回归和 SVM 的比较 1. 模型的核心思想 • 逻辑回归&#xff1a; • 基于概率的模型&…

Android ImageView支持每个角的不同半径

Android ImageView支持每个角的不同半径 import android.annotation.SuppressLint; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; import android.content.res.Resources.NotFoundException; import an…

Flyway-SQL 脚本与 Java 迁移

Flyway SQL 脚本与 Java 迁移详解 Flyway 是一种数据库迁移工具&#xff0c;提供了 SQL 脚本和 Java 迁移两种方式来管理数据库变更。在 Flyway 中&#xff0c;数据库迁移是通过逐步执行迁移脚本或代码来完成的。Flyway 既可以通过 SQL 文件直接执行数据库操作&#xff0c;也可…

spark读取数据性能提升

1. 背景 spark默认的jdbc只会用单task读取数据&#xff0c;读取大数据量时&#xff0c;效率低。 2. 解决方案 根据分区字段&#xff0c;如日期进行划分&#xff0c;增加task数量提升效率。 /*** 返回每个task按时间段划分的过滤语句* param startDate* param endDate* param …

在项目管理中,项目进度由哪些要素决定?

在项目管理领域&#xff0c;项目进度受到多种要素的综合影响。以下是一些关键的决定要素&#xff1a; 一、项目范围 1、任务清单 明确的任务清单是项目进度的基础。详细列出项目中需要完成的各项任务&#xff0c;包括任务的先后顺序、并行任务等&#xff0c;直接关系到进度规划…