SpringCloud-Alibaba之Sentinel熔断与限流

这篇具有很好参考价值的文章主要介绍了SpringCloud-Alibaba之Sentinel熔断与限流。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

一、下载安装运行

http://localhost:8080进行访问
登录账号和密码均为sentinel
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

二、创建工程,并注册到nacos服务中心

  1. 依赖spring-cloud-starter-alibaba-nacos-discovery,spring-cloud-starter-alibaba-sentinel
    sentine-datasource-nacos (持久化)
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
  1. 配置文件
server:
	port: 8401
spring:
	application:
		name: cloudalibaba-sentinel-service
	cloud:
		nacos:
			discovery:
				server-addr: localhost:8848
		sentinel:
			transport:
				dashboard: localhost:8080 # 8080监控8401
				port: 8719 # 假如被占用会自动从8719开始依此+1扫描,直至找到未被占用的端口
management:
	endpoints:
		web:
			exposure:
				include: '*'
  1. 启动类
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401{
	
	public static void main(String[] args){
		SpringApplication.run(MainApp8401.class,args);
	}
}
  1. 业务类
@RestController
public class FlowlimitController{
	
	@GetMapping("/testA")
	public String testA(){
		return "------testA";
	}
	@GetMapping("/testB")
	public String testB(){
		return "-------testB";
	}
}

三、启动sentinel java -jar sentinel-dashboard-1.7.0.jar 启动微服务8401,查看sentinel控制台

①、首先需要获取 Sentinel 控制台,支持直接下载和源码构建两种方式。
下载:sentinel-dashboard.jar
源码构建:进入 Sentinel Github 项目页面,将代码 git clone 到本地自行编译打包

②、启动控制台,执行 java -jar sentinel-dashboard.jar完成 Sentinel 控制台的启动, 控制台默认的监听端口为 8080。

============================================================================

Sentinel 提供了两种配置限流规则的方式:代码配置 和 控制台配置。

代码规则

List<FlowRule> rules = new ArrayList<FlowRule>();
FlowRule rule = new FlowRule();

rule.setResource(str);
rule.setCount(10);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setLimitApp("default");

rules.add(rule);
FlowRuleManager.loadRules(rules);

以下为控制台规则:

Sentinel流控规则

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

QPS:每秒钟请求数量达到阈值进行限流,连续多次刷新请求即可。
线程数:调用该请求的线程数达到阈值进行限流,再代码中添加thread.sleep(3),然后多次刷新请求即可模拟出来

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

关联资源,当关联资源/testB的QPS阈值超过1,就限流/testA的Rest访问地址
利用postman进行模拟
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

Sentinel流控效果

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

严格控制请求通过的间隔时间,就是让请求匀速通过,对应的是漏桶算法。
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

降级规则

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
RT:平均响应时间,秒级。

  • 平均响应时间超出阈值且在时间窗口内通过的请求>=5,两个条件同时满足触发降级
  • 窗口期过后关闭断路器
  • RT最大4900(更大需要通过-Dcsp.sentinel.statistic.max.rt=XXXX才能生效)
    SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
@GetMapping("/testD")
public String testD(){
	try{
		TimeUnit.SECONDS.sleep(1);
	}catch(InterruptedException e){
		e.printStackTrace();
	}

	log.info("testD测试RT");
	return "testD"
}

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

异常比例:秒级

  • QPS>=5且异常比例(秒级统计)超过阈值触发降级,时间窗口结束,关闭降级
    SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
@GetMapping("/testD")
public String testD(){
	
	log.info("testD异常比例");
	int age = 10/0;
	return "testD"
}

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

异常数:分钟级

  • 异常数(分钟统计)超过阈值时,触发降级,时间窗口结束后,关闭降级
    SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
@GetMapping("/testE")
public String testE(){
	log.info("testE测试异常数");
	int age = 10/0;
	return "testE 测试异常数";
}

热点Key限流

商品ID为参数,统计一段时间内最常购买的商品ID并进行限制
用户ID为参数,统计一段时间内频繁访问的用户ID进行限制
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

@GetMapping("/testHotKey")
@sentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
public String testHotKey(@RequeestParam(value = "p1",required = false) String p1,
						 @RequeestParam(value = "p2",required = false) String p2){
	return "--------testE 测试异常数";
}

public String deal_testHotKey(String p1,String p2,BlockException exception){
	return "--------deal_testHotKey 测试异常数";
}

对第0个参数p1,进行阈值限定。
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

参数例外项,对参数指定的值进行阈值设定,如下所示:当参数为5,限流阈值改为200
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

系统规则

SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

====================================================================

HTTP埋点
Sentinel starter 默认为所有的 HTTP 服务提供了限流埋点,如果只想对 HTTP 服务进行限流,那么只需要引入依赖,无需修改代码。

自定义埋点
如果需要对某个特定的方法进行限流或降级,可以通过 @SentinelResource 注解来完成限流的埋点

自定义限流处理逻辑

默认处理逻辑

public class CustomUrlBlockHandler implements UrlBlockHandler{
	
	@Override
	public void blocked(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws IOException {
		
	}
}

WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler());

使用注解@SentinelResource处理逻辑
blockHandler 属性(针对所有类型的 BlockException,需自行判断)或 fallback 属性(针对熔断降级异常)注意对应方法的签名和位置有限制。Sentinel注解支持文档

@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,
					blockHandler = "handlerException2") //指定处理限流的类以及方法
public CommonResult customerBlockHandler(){
	return new CommonResult(200,"按客户自定义",new Payment(2020L,"serial002"));
}
/**
	另外创建一个handler包
	
	创建CustomerBlockHandler类自定义限流处理逻辑
	可以定义多个方法
*/
public class CustomerBlockHandler{

	public static CommonResult handlerException(BlockException exception){
		return new CommonResult(444,"按客户自定义自定义处理",new Payment(2020L,"serial002"));
	}

	public static CommonResult handlerException2(BlockException exception){
		return new CommonResult(444,"按客户自定义自定义处理",new Payment(2020L,"serial002"));
	}
}

@SentinelResource注解的属性

Controller中

@SentinelResource(value = "fallback",fallback = "handlerFallback") //只负责业务异常


@SentinelResource(value = "fallback",blockHandler = "blockHandler") //赋值sentinel控制台的设置出现异常

若blockHandler和fallback都进行了配置,则被限流降级而抛出BlockException时只会进入blockHandler处理逻辑

远程调用接口@FeignClinet进行Sentinel组合

Sentinel规则持久化

将限流配置规则持久化到Nacos保存,只要刷新rest地址,sentinel控制台就能显示出流控规则。

一、工程中引入依赖

sentinel-datasource-nacos

二、配置文件中
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring
三、nacos中添加配置列表,添加json配置规则
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

四、sentinel控制台就可以查到流控规则
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

Endpoint信息查看

Spring Boot 应用支持通过 Endpoint 来暴露相关信息,Sentinel Starter 也支持这一点。
Maven 中添加 spring-boot-starter-actuator依赖,并在配置中允许 Endpoints 的访问

  • Spring Boot 1.x 中添加配置 management.security.enabled=false
  • Spring Boot 2.x 中添加配置 management.endpoints.web.exposure.include=*

Spring Boot 1.x 可以通过访问 http://127.0.0.1:18083/sentinel
Spring Boot 2.x 可以通过访问 http://127.0.0.1:18083/actuator/sentinel 来访问
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring

查看实时监控

支持实时监控查看,您可以通过 Sentinel 控制台查看各链路的请求的通过数和被限流数等信息。
其中 p_qps 为通过(pass) 流控的 QPS,b_qps 为被限流 (block) 的 QPS
SpringCloud-Alibaba之Sentinel熔断与限流,spring cloud,sentinel,spring文章来源地址https://www.toymoban.com/news/detail-604118.html

到了这里,关于SpringCloud-Alibaba之Sentinel熔断与限流的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • springcloud alibaba sentinel熔断降级

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从 流量控制、熔断降级、系统负载保护 等多个维度保护服务的稳定性。 sentinel相当于hystrix的升级版,加入了web界面,能够实时在线的改变流量策略。 Sentinel 分为两个部分: 核心库(J

    2024年01月23日
    浏览(38)
  • SpringCloud.04.熔断器Hystrix( Spring Cloud Alibaba 熔断(Sentinel))

    目录 熔断器概述 使用Sentinel工具 什么是Sentinel 微服务集成Sentinel 配置provider文件,在里面加入有关控制台的配置 实现一个接口的限流 基本概念 重要功能 Sentinel规则 流控规则 简单配置 配置流控模式 配置流控效果 降级规则 @SentinelResource的使用 Feign整合Sentinel 由于Hystrix已经停

    2024年01月19日
    浏览(33)
  • Java之SpringCloud Alibaba【四】【微服务 Sentinel服务熔断】

    Java之SpringCloud Alibaba【一】【Nacos一篇文章精通系列】 跳转 Java之SpringCloud Alibaba【二】【微服务调用组件Feign】 跳转 Java之SpringCloud Alibaba【三】【微服务Nacos-config配置中心】 跳转 Java之SpringCloud Alibaba【四】【微服务 Sentinel服务熔断】 跳转 Java之SpringCloud Alibaba【五】【微服务

    2024年02月12日
    浏览(28)
  • SpringCloud学习6(Spring Cloud Alibaba)断路器Sentinel熔断降级

    SpringCloud、SpringCloudAlibaba、SpringBoot版本选择。为了避免各种千奇百怪的bug,我们还是采用官方推荐的毕业版本。 修改tomcat配置最大线程数 引入测试依赖 编写测试代码 这里同时我们在浏览器去请求该地址,响应会变得很慢 测试结论:此时会发现由于thread接口囤积大量请求,

    2023年04月08日
    浏览(41)
  • SpringCloud Alibaba Sentinel 与 SpringCloud Gateway 的限流有什么差别?(三种限流算法原理分析)

    目录 一、Sentinel 与 Gateway 的限流有什么差别? 1.1、前置知识 - 四种常见的限流算法 1.1.1、Tips 1.1.2、计数器算法 1)固定窗口计数器算法 2)滑动窗口计数器算法 1.1.3、令牌桶算法 1.1.4、漏桶算法 1.2、解决问题 1.1.1、Tips 限流, 就是指对服务器请求量做限制,避免因为突发的

    2024年01月25日
    浏览(35)
  • 【SpringCloud Alibaba】(六)使用 Sentinel 实现服务限流与容错

    今天,我们就使用 Sentinel 实现接口的限流,并使用 Feign 整合 Sentinel 实现服务容错的功能,让我们体验下微服务使用了服务容错功能的效果。 因为内容仅仅围绕着 SpringCloud Alibaba技术栈展开,所以,这里我们使用的服务容错组件是阿里开源的 Sentinel。 当然,能够实现服务容错

    2024年02月14日
    浏览(34)
  • Sentinel限流--流控模式与限流效果

    簇点链路就是项目内的调用链路(controller - servcie - mapper ),链路中被监控的每个接口就是一个资源。 默认情况下sentinel会监控SpringMVC的每一个端点(Endpoint),Endpoint可以理解为controller中的每一个方法,每一个端点(Endpoint)就是调用链路中的一个资源。 流控、熔断等都是

    2024年02月17日
    浏览(25)
  • 微服务韧性工程:利用Sentinel实施有效服务容错与限流降级

            目录 一、雪崩效应 二、Sentinel 服务容错         2.1 Sentinel容错思路         2.2 内部异常兼容         2.3 外部流量控制 三、Sentinel 项目搭建 四、Sentinel 工作原理         服务容错是微服务设计中一项重要原则和技术手段,主要目标是在服务出现故障、网络波

    2024年03月15日
    浏览(72)
  • 【微服务SpringCloud-Alibaba】:Nacos 配置中心

    在 SpringCloud 中,我们使用了 Config 组件管理所有配置文件,使用了 Bus 消息总线更新配置,两者需要配合使用才能动态的管理配置文件。而 Nacos 可以替代这两个组件,实现动态的管理所有配置文件。 2.1、添加配置文件 想要管理配置,先要有配置文件。在使用 Config 组件管理配

    2023年04月27日
    浏览(41)
  • 微服务简介,Springcloud-alibaba中的Nacos简介

    目录 一:微服务架构 1.0:单体架构 1.1:微服务架构 1.2:微服务架构的优势 1.3:微服务架构的缺点(挑战) 1.4:SpringCloud与微服务关系 1.5:SpringBoot和SpringCloud关系 二:服务注册与发现 2.1:服务注册与发现 2.2:注册中心对比 nacos功能与架构 三:nacos简介 nacos功能:  nacos安装

    2024年02月10日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包