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

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. 错误:org.springframework.beans.factory.BeanDefinitionStoreException:

    在练习尚硅谷雷丰阳老师的SSM-CRUD整合的时候,因为使用的Thymeleaf,而不是jsp,跟着老师操作所有会出现一些错误,现在我把这些错误都整理一下,希望能帮助到有用的朋友. org.sprin ...

  2. windows上用vs2017静态编译onnxruntime-gpu CUDA cuDNN TensorRT的坎坷之路

    因为工作业务需求的关系,需编译onnxruntime引入项目中使用,主项目exe是使用的vs2017+qt5.12. onnxruntime就不用介绍是啥了撒,在优化和加速AI机器学习推理和训练这块赫 ...

  3. Spring之后置处理器

      Spring的后置处理器是Spring对外开发的重要扩展点,允许我们接入Bean的实例化流程中,以达到动态注册BeanDefinition.动态修改BeanDefinition.动态修改Bean的 ...

  4. Python + logging 控制台有日志输出,但日志文件中数据为空

    源码: def output(self, level, message): fh = logging.FileHandler(self.logpath, mode='a', encoding='utf ...

  5. Python全栈工程师之从网页搭建入门到Flask全栈项目实战(7) - 在线问答系统

    1.项目源码/业务逻辑 百度网盘链接:链接:https://pan.baidu.com/s/13VNfrSJE6vcL3HP1J5T8ew 提取码:00s0,项目业务逻辑自行阅读 2.项目搭建 点击新 ...

  6. “喜提”一个P2级故障—CMSGC太频繁,你知道这是什么鬼?

    大家好,我是陶朱公Boy. 背景 今天跟大家分享一个前几天在线上碰到的一个GC故障- "CMSGC太频繁". 不知道大家看到这条告警内容后,是什么感触?我当时是一脸懵逼的,一万个为 ...

  7. Cocos Creator 打包原生 Android 包该如何选择 NDK 版本?

    大家好,我是晓衡! 记得前段时间,在一些群里看到有小伙伴说 Cocos Creator 打包 Android 原生 APK 有问题:一种是构建失败,一种是运行起来报错. 晓衡也是有好长一段时间,没有碰 ...

  8. DML_修改数据

    DML_修改数据 修改数据: 语法: update 表名 set 列名1 = 值1,列名2 = 值2, ... [ where 条件 ] ; 注意: 1. 如果不加任何条件,则会将表中所有记录全部修改 ...

  9. java 进阶P-2.3+P-2.4

    封闭的访问属性 private 封装:把数据和对数据的操作放在一起. (所谓封装就是把数据和对这些数据的操作放在一个地方,通过这些操作把这些数据保护起来,别人不能直接接触到这些数据) 1 privat ...

  10. 复杂环境下ocr与印章识别技术理解及研发趋势

    引言 随着社会经济的发展,印章作为企事业单位.社会团体.政府部门乃至国家的一种具有法律意义的标志和证据,在现代社会生活中发挥着重要作用.随着现代商务活动的不断发展,企业在业务开展的过程中通常会涉及大量 ...