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

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. uniapp详细入门教程

    链接:https://www.ruletree.club/archives/2071/ 点击链接查看,内容详细,一学就会哦~! /******** * * .-~~~~~~~~~-._ _.-~~~~ ...

  2. Spring IOC官方文档学习笔记(一)之IOC容器概述

    1.IOC容器简介 (1) org.springframework.beans 与 org.springframework.context 这两个包是Spring IOC容器的基础,在org.spri ...

  3. 用友开发者中心全新升级,YonBuilder移动开发入门指南

    听说用友新上线了全新的开发者中心,有YonBuilder应用开发,集成开发.数据开发.智能与自动化.DevOps 等板块,本人作为用户老客户,对其中的移动开发比较感兴趣,本文重点讲解其中的移动开发平台 ...

  4. 一文告诉你AVM中设置字体的方法

    ​ avm 是一种简便的多端开发框架,可以开发APP.小程序.H5.今天学习了一下使用 avm 开发 APP 怎么设置字体,下面将经验分享给大家. 所需步骤: 1.  将需要使用的字体文件放到代码包r ...

  5. Potree 002 Desktop开发环境搭建

    1.工程创建 我们使用Visual Studio 2022开发,把下载好后的PotreeDesktop源码添加到Visual Studio中. 打开Visual Studio 2022,新建Asp.N ...

  6. 重新捋一捋React源码之更新渲染流程

    前言 前些天在看Dan Abramov个人博客(推荐阅读,站在React开发者的角度去解读一些API的设计初衷和最佳实践)里的一篇文章,其重点部分的思想就是即使不使用Memo(),也可以通过组合的方式 ...

  7. Java安全之JDBC Attacks学习记录

    Java安全之JDBC Attacks 写在前面 很早就看到了Make JDBC Attacks Brilliant Again议题,一直想分析学习下,但是太懒. MySQL 原理概述 "扩 ...

  8. ffmpeg拉取rtsp视频流

    公司项目需要提供实时显示网络摄像头实时视频. void RTSPFFmpeg::rtsp_open(const char *url) { AVFormatContext* format_ctx = a ...

  9. Quartz.Net 官方教程 Tutorial 2/3(Listener 和 JobStore)

    Listener 调度任务的监听,当前版本支持添加调度,触发器和任务的监听,其中触发器和任务的监听支持通过监听名称进行添加(Add*ListenerMatcher方法) 监听不能对外抛出异常,需要内部 ...

  10. 华为云MRS支持lakeformation能力,打造一站式湖仓,释放数据价值

    摘要:对云端用户而言,业务价值发现是最重要的,华为MRS支持LakeFormation后,成功降低了数据应用的成本,帮助客户落地"存"与"算"的管理,加快推进了 ...