随着微服务的流行,服务和服务之间的稳定性变得越来越重要。

Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

作用:

服务雪崩

服务降级

服务熔断

服务限流

1.cmd  java -jar sentinel-dashboard-1.6.3.jar

2.打开nacos

3.访问http://localhost:8080

  <dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot整合Web组件+actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
server:
port: 8401 spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080 #配置sentinel dashbord地址
port: 8719 management:
endpoints:
web:
exposure:
include: "*"

这里的 spring.cloud.sentinel.transport.port 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。
比如 Sentinel 控制台添加了一个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。
package com.etc.cloudalibaba.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; @RestController
@Slf4j
public class FlowLimitController {
@GetMapping("/testA")
public String testA(){
return "------ testA -----";
} @GetMapping("/testB")
public String testB(){
return "------ testB -----";
}
/**
* 主要测试降级 ---- RT
* @return
*/
@GetMapping("/testC")
public String testC(){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "------ testC -----" ;
} /**
* 主要测试降级 ---- 异常占比 异常数
* @return
*/
@GetMapping("/testD")
public String testD(){
System.out.println(10/0);
return "------ testD -----" ;
}
}
package com.etc.cloudalibaba;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
public static void main(String[] args) {
SpringApplication.run( SentinelServiceMain8401.class,args);
log.info("****************SentinelServiceMain8401 启动 ************ ");
}
}

sentinel 采用懒加载方式 。

流控规则

资源名 唯一名称 默认请求路径

针对来源 Sentinel 可以针对调用者限流 填写微服务名 默认 default 不区分来源

阈值类型 /单机阈值

  • QPS (每秒请求的数量) 当调用该api的qps 达到阈值的时候进行限流

  • 线程数 : 当调用该api的线程数达到阈值的时候 进行限流

流控模式

  • 直接 api达到限流的直接限流

  • 关联 当关联的资源达到阈值时。 就限流自己,当A关联了B , B 达到阈值的之后就限流自己

  • 链路 只记录链路上的流量指定资源从入口资源进来的流量。 如果达到阈值就限流 api级别针对来源

流控效果

  • 快速失败 直接失败。 抛异常

  • Warm up 冷加载因子。 默认时3,公式阈值除3(默认3), 经过预热时长后才会达到的阈值。 100 /3 = 33 慢慢升上来 预热时长我们配置 4秒

  • 排队等待 匀速排队让请求 匀速通过。 这个阈值类型只能是qps,阈值必须是设置为qps 均匀排队 方式会严格控制请求的时间间隔。 让请求以均匀的速度通过 。 对应漏斗算法。

降级规则

RT

平均响应时间: 当1s内持续进入5个请求,对应时刻的平均响应时间均超过阈值 毫秒那么在接下来的时间窗口 之内。 对这个方法的调用都自动熔断。

/**
* 主要测试降级 ---- RT
* @return
*/
@GetMapping("/testC")
public String testC(){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "------ testC -----" ;
}

配置:类型 RT RT 200毫米 时间窗 4 s

如果QPS大于5, 并且平均响应时间大于200ms, 在接下来4s钟无法访问,之后恢复。

异常比例

当资源的每秒请求量>=5,并且每秒异常总数占通过率的比值超过阈值。 就引入降级状态在接下来的时间窗口之内对这个方法都进行自动返回。 异常比例 0.0 1.0 代表 0% 100%

异常数

当资源近1分钟的异常超过我们设定阈值之后进行熔断。 注意统计时间窗口分钟级别。 若 小60s 择结果熔断后仍可能在进入熔断。

热点key限流

异常数据可以不可自定义 Blocked by Sentinel (flow limiting)

@GetMapping("/testHotykey")
@SentinelResource(value = "testHotykey",blockHandler = "deal_testHotykey")
public String testHotykey(
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2){ return "------- testHotykey ------- "; } public String deal_testHotykey(String p1, String p2, BlockException e){
return "-------- deal_testHotykey 兜底了---------";
}

系统规则

LOAD 自适应 : 系统的load 1 作为一个启发的指标。 进行自适应系统保护。 当系统load1 超过设定的其法制。 cpu cores * 2.5

RT 当单台机器上所有入口流量的平均rt 达到阈值触发系统保护。 单位是毫米。

线程数 当单台机器上所有的入口的流量的并发线程达到我们阈值的时候触发系统保护

入口 QPS 当单台机器的素偶u的入口的流量达到qps 触发系统保护

CPU 使用率 当系统cpu使用率超过阈值就马上触发系统保护

不合适 使用危险 一个竹竿打死了一片人

规则持久化

一旦我们重新启动机器。 sentinel规则消息。 生成环境种需要配置的规则进行持久化。

将限流的规则保存在nacos上

引入pom

<!--     sentinel-datasource-nacos 后续持久化用   -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

改yml

编写json

[
{
"resource": "/rateLimit/byUrl",
"limitApp": "default",
"grade": 1,
"count": 1,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]

resource 资源名

limitApp 来源应用

grade 阈值类型 0 线程数 1 qps

count 阈值数量

strategy 流控模式 0 直接 1 关联 2 链路

controlBehavior 流控效果 0 快速失败 1 预热 2 排队等待

clusterMode 是否是集群

Sptring cloud Alibaba Sentinel 实现熔断与限流的更多相关文章

  1. Spring Cloud Alibaba:Sentinel实现熔断与限流

    一.什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩效应”,为微服务系统提供了稳 ...

  2. Spring Cloud Alibaba | Sentinel: 服务限流基础篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定 ...

  3. Spring Cloud Alibaba | Sentinel: 服务限流高级篇

    目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...

  4. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...

  5. 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例

    先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...

  6. Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探

    目录 Spring Cloud Alibaba | Sentinel: 分布式系统的流量防卫兵初探 1. Sentinel 是什么? 2. Sentinel 的特征: 3. Sentinel 的开源生 ...

  7. Spring Cloud Alibaba Sentinel对Feign的支持

    Spring Cloud Alibaba Sentinel 除了对 RestTemplate 做了支持,同样对于 Feign 也做了支持,如果我们要从 Hystrix 切换到 Sentinel 是非常 ...

  8. Spring Cloud Alibaba Sentinel对RestTemplate的支持

    Spring Cloud Alibaba Sentinel 支持对 RestTemplate 的服务调用使用 Sentinel 进行保护,在构造 RestTemplate bean的时候需要加上 @S ...

  9. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  10. 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例

    sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...

随机推荐

  1. java计算器༼༎ຶᴗ༎ຶ༽༼༎ຶᴗ༎ຶ༽༼༎ຶᴗ༎ຶ༽༼༎ຶᴗ༎ຶ༽,又是掉发的一天

    题目: 给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值. 注意:不允许使用任何将字符串作为数学表达式计算的内置函数,比如 eval() . 示例 1: 输入:s = " ...

  2. 沁恒微(WCH)CH395/392配置使用,代码指南 网路接口芯片 CH395 CH392

    CH395/CH392相关资料可以从官网下载具体连接可以看博客:WCH以太网相关芯片资料总结 里面是WCH官网相关信息的链接. 也可以去Gitee上下载:Gitee链接. STM32控制CH395的例 ...

  3. Auto-Job任务调度框架

    Auto-Job 任务调度框架 一.背景 生活中,业务上我们会碰到很多有关作业调度的场景,如每周五十二点发放优惠券.或者每天凌晨进行缓存预热.亦或每月定期从第三方系统抽数等等,Spring和java目 ...

  4. Cesium for Unreal加载倾斜摄影

    本文介绍UE4中通过Cesium插架加载本地倾斜摄影模型.Cesium for Unreal插件运行在UE环境何总运行Cesium,这样方便做一个GIS应用. 安装Cesium for Unreal插 ...

  5. python之路55 cookie与session 操作 把模块变成字符串进行导入

    django中间件三个了解的方法 1.process_view 路由匹配成功之后执行视图函数/类之前自动触发(顺序同process_request) 2.process_exception 视图函数/ ...

  6. python之路33 MySQL 1

    存取数据的演变 1.文本文件 文件路径不固定:C:\aaa.txt D:\bbb.txt E:\ccc.txt 数据格式不统一:jason|123 jason$123 jason 123 2.软件开发 ...

  7. [Leetcode]设计循环队列

    题目   代码 class MyCircularQueue { public: /** Initialize your data structure here. Set the size of the ...

  8. Flutter新版本2.X系列--01创建项目

    1.新建项目,打开Android studio,点击红圈部分 2.选择第一个 3.设置你的项目名称,flutter sdk位置,以及项目存储路径 4.设置包名,这个要唯一 5.好啦 ~ 作为一名前端开 ...

  9. 扒一扒Bean注入到Spring的那些姿势,你会几种?

    大家好,我是三友~~ 这篇文章我准备来扒一扒Bean注入到Spring的那些姿势. 其实关于Bean注入Spring容器的方式网上也有很多相关文章,但是很多文章可能会存在以下常见的问题 注入方式总结的 ...

  10. 网络编程前戏和OSI七层协议

    目录 一.软件开发架构 1.什么是软件开发架构 2.软件开发架构 架构方式一:c/s架构 架构方式二:b/s架构 架构优劣势 二.架构总结 三.网络编程前戏 1.什么是网络编程 2.学习网络编程的目的 ...