微服务之熔断与降级篇 Sentinel
熔断与降级
- 什么是熔断?
- 指一个系统中为了防止系统过载而采取的一系列保护措施,从而防止系统崩溃
- 什么是降级?
- 当服务器压力过大时被迫选择关闭一些页面,尽可能保证核心业务的运转.
- 为什么使用熔断和降级技术
- 分布式系统由于一个功能需要调用多个模块,出现问题的几率会大大增加,熔断和降级技术在某个模块出现问题时不会使系统出现雪崩问题,增强系统的稳定性.
Sentinel
- 简介
- 它是阿里巴巴开源的一个组件,主要以流量为切入点,提供服务熔断与降级,系统自适应,全方面保护系统
- sentinel的架构图
- 特点
- 不依赖与其它的框架,运行在JDK1.7,并能与Dubbo和SpringCloud无缝结合
- 友好的图形化界面,方便管理
Sentinel的使用
- 点击下载
- 在下载好的地方按照SpringBoot的启动方式启动即可 java -Dserver.port=6677 -jar sentinel-dashboard-1.8.0.jar
- 浏览器输入: http://localhost:6677 输入账号密码(sentinel)即可访问控制台
- 项目中使用
-
添加依赖
<!-- springcloud alibaba sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- SpringBoot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
添加配置
spring: application: # 应用名称 name: project-xxxx cloud: sentinel: # 取消控制台懒加载 eager: true transport: # 控制台地址 dashboard: 127.0.0.1:6677
-
添加TestUserController.java, 模拟接口返回用户信息
@RestController public class TestUserController { @GetMapping("/user/info") public Object info() { return "{\"username\":\"admin\",\"password\":\"admin123\"}"; } }
-
启动SpringBoot项目并访问接口, 登录控制台就可看到访问信息
-
Sentinel的更多细节
- 定义资源@SentinelResource用于定义资源,并提供可选的异常处理和fallback配置项。
-
定义接口
public interface IUserService { public Object selectUserByName(String username); }
-
定义实现类
@Service public class IUserServiceImpl implements IUserService { @Autowired private RestTemplate restTemplate; @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @SentinelResource(value = "selectUserByName", blockHandler = "selectUserByNameBlockHandler", fallback = "selectUserByNameFallback") @Override public Object selectUserByName(String username) { return restTemplate.getForObject("http://localhost:9201/user/info/" + username, String.class); } // 服务流量控制处理,参数最后多一个 BlockException,其余与原函数一致。 public Object selectUserByNameBlockHandler(String username, BlockException ex) { System.out.println("selectUserByNameBlockHandler异常信息:" + ex.getMessage()); return "{\"code\":\"500\",\"msg\": \"" + username + "服务流量控制处理\"}"; } // 服务熔断降级处理,函数签名与原函数一致或加一个 Throwable 类型的参数 public Object selectUserByNameFallback(String username, Throwable throwable) { System.out.println("selectUserByNameFallback异常信息:" + throwable.getMessage()); return "{\"code\":\"500\",\"msg\": \"" + username + "服务熔断降级处理\"}"; } }
-
测试接口请求TestUserController.java
@RestController public class TestUserController { @Autowired private IUserService userService; @GetMapping("/info/{username}") public Object info(@PathVariable("username") String username) { return userService.selectUserByName(username); } }
-
- 流量规则
- 控制台定义
- 资源名: 唯一名称,默认请求路径
- 针对来源: Sentinel可以针对调用者进行限流,填写微服务名,默认default(不区分来源)
- 阈值类型/单机阈值:
- QPS(每秒请求数量):当调用该api的QPS达到阈值的时候,进行限流
- 线程数:当调用该api的线程数达到阈值的时候,进行限流
- 是否集群: 不需要集群
- 流控模式:
- 直接:api达到限流条件时,直接限流
- 关联:当关联的资源达到限流阈值时,就限流自己
- 链路:只记录指定链路上的流量(指定资源从入口资源进来的流量,如果达到峰值,就进行限流)【api级别的针对来源】
- 流控效果
- 快速失败:直接失败,抛异常
- Warm Up:根据coldFactor(冷加载因子,默认3)的值,从阈值/coldFactor,经过预热时长,才达到设置的QPS阈值
- 排队等待:匀速排队,让请求以匀速通过,阈值类型必须设置为QPS,否则无效
- 控制台定义
- 降级规则
- 控制台定义: 选择降级规则,新增降级规则,填入对应信息
- 控制台定义: 选择降级规则,新增降级规则,填入对应信息
- 动态配置规则
- Nacos配置规则
-
在nacos中配置自定义规则
[ { "resource": "selectUserByName", "count": 2, "grade": 1, "limitApp": "default", "strategy": 0, "controlBehavior": 0 } ]
-
添加依赖
<!-- springcloud alibaba nacos config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!-- sentinel datasource nacos --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
-
添加相关配置,sentinel下面的dataSource中配置nacos
spring: application: # 应用名称 name: project-xxxx cloud: nacos: config: # 配置中心地址 server-addr: 127.0.0.1:8848 # 配置文件格式 file-extension: yml # 共享配置 shared-configs: - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} sentinel: # 取消控制台懒加载 eager: true transport: # 控制台地址 dashboard: 127.0.0.1:6677 # nacos配置持久化 datasource: ds1: nacos: server-addr: 127.0.0.1:8848 dataId: sentinel-ruoyi-gateway groupId: DEFAULT_GROUP data-type: json rule-type: flow
-
- Nacos配置规则