spring cloud 熔断、限流、服务降级、负载均衡总结

springcloud 微服务网关

zuul.strip-prefix=true
zuul.routes.postgres-client.path =/ps01/**
zuul.routes.postgres-client.service-id=postgres-client
zuul.routes.postgres-client02.path=/ps02/**
zuul.routes.postgres-client02.service-id=postgres-client02
zuul.semaphore.max-semaphores=100
zuul.ribbon-isolation-strategy=semaphore
@Component
public class MyRateLimiter extends ZuulFilter {//  令牌桶static RateLimiter rateLimiter= RateLimiter.create(8, 2, TimeUnit.SECONDS);@Overridepublic String filterType() {return PRE_TYPE;}@Overridepublic int filterOrder() {return SERVLET_DETECTION_FILTER_ORDER - 1;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic Object run() throws ZuulException {RequestContext context = RequestContext.getCurrentContext();boolean allowed = rateLimiter.tryAcquire();if (!allowed) {context.setSendZuulResponse(false);context.setResponseStatusCode(501);}return null;}
}
ribbon.eureka.enabled=true
ribbon.ReadTimeout = 10000
ribbon.ConnectTimeout= 10000
ribbon.MaxAutoRetries= 0
ribbon.MaxAutoRetriesNextServer=1
ribbon.OkToRetryOnAllOperations=false
spring.cloud.sentinel.transport.port=8719
spring.cloud.sentinel.transport.dashboard=192.168.1.4:8080
nacos-gateway.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes[0].id=order-servicespring.cloud.gateway.routes[0].predicates[0]=Path=/order/**
spring.cloud.gateway.routes[0].uri=lb://order-service
spring.cloud.gateway.redis-rate-limiter.include-headers=true
spring.cloud.gateway.routes[0].filters[0]=RewritePath=/order(?<segment>/?.*), $\{segment}spring.cloud.gateway.routes[0].filters[0].name=RequestRateLimiter
# 如果返回的key是空的话,则不进行限流
spring.cloud.gateway.routes[0].filters[0].args[0]=deny-empty-key=false
# 每秒产生多少个令牌
spring.cloud.gateway.routes[0].filters[0].args[1]=redis-rate-limiter.replenishRate=0# 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求
spring.cloud.gateway.routes[0].filters[0].args[2]=redis-rate-limiter.burstCapacity=0# 每次请求申请几个令牌spring.cloud.gateway.routes[0].filters[0].args[3]=redis-rate-limiter.requestedTokens=0
# 自定义限流规则
spring.cloud.gateway.routes[0].filters[0].args[0]=rate-limiter="#{@defaultGatewayRateLimiter}"
# 返回限流的key
spring.cloud.gateway.routes[0].filters[0].args[1]=key-resolver="#{@defaultGatewayKeyResolver}"
spring.cloud.gateway.routes[0].filters[0].args[2]=deny-empty-key=false
# 限流后向客户端返回的响应码429,请求太多
spring.cloud.gateway.routes[0].filters[0].args[3]=status-code=TOO_MANY_REQUESTS
# 每次请求申请几个令牌  default-gateway-rate-limiter 的值是在 defaultGatewayRateLimiter 中定义的
spring.cloud.gateway.routes[0].filters[0].args[4]=default-gateway-rate-limiter.requestedTokens=1

1.使用hystrix

@HystrixCommand(commandKey = "saveStudent",commandProperties = {@HystrixProperty(name="execution.isolation.strategy",value = "THREAD")},threadPoolKey = "saveStudent",threadPoolProperties = {@HystrixProperty(name= CORE_SIZE,value = "3"),@HystrixProperty(name= MAX_QUEUE_SIZE,value = "5"),@HystrixProperty(name= QUEUE_SIZE_REJECTION_THRESHOLD,value = "7"),@HystrixProperty(name = CIRCUIT_BREAKER_ENABLED, value = "true"),// 是否开启断路器@HystrixProperty(name = CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),// 请求次数@HystrixProperty(name = CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000"), // 时间窗口期@HystrixProperty(name = CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "60"),@HystrixProperty(name = EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "4000"),//统计滚动窗口的持续时间,以毫秒为单位。// 该时间用于断路器判断健康度时需要收集信息的持续时间,断路器在收集指标信息的时候会根据设置的时间窗长度拆分成多个“桶”来累计各个度量值,每个“桶”记录一段时间内的采集指标。)@HystrixProperty(name = METRICS_ROLLING_STATS_TIME_IN_MILLISECONDS, value = "10000")},fallbackMethod = "saveStudent")

feign.hystrix.enabled=true
hystrix.threadpool.default.coreSize=100 #并发执行的最大线程数,默认10
hystrix.threadpool.default.maxQueueSize=5  #BlockingQueue的最大队列数,当设为-1,会使用 SynchronousQueue,值为正时使用LinkedBlcokingQueue。该设置只会在初始化时有效,之后不能修改
hystrix.threadpool.default.queueSizeRejectionThreshold=1 #即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝。因为maxQueueSize不能被动态修改,这个参数将允许我们动态设置该值。if maxQueueSize == ?1,该字段将不起作用
hystrix.command.default.execution.isolation.strategy=SEMAPHORE #隔离策略,默认是Thread, 可选Thread|Semaphor
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000  #命令执行超时时间,默认1000ms
hystrix.command.default.execution.timeout.enabled=true #执行是否启用超时,默认启用true
hystrix.command.default.execution.isolation.strategy.semaphore.max-semaphores=100

service-B.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
ribbon.eureka.enabled=true
ribbon.ReadTimeout = 10000
ribbon.ConnectTimeout= 10000
ribbon.MaxAutoRetries= 0
ribbon.MaxAutoRetriesNextServer=1
ribbon.OkToRetryOnAllOperations=false

sentinel 限流熔断

sentinel 配置

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-apollo</artifactId><version>1.8.0</version></dependency>
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
spring.cloud.sentinel.transport.port=8719
spring.cloud.sentinel.transport.dashboard=localhost:8480
feign.sentinel.enabled=true
#spring.cloud.sentinel.datasource.ds.file.file=classpath:flowrule.json
#spring.cloud.sentinel.datasource.ds.file.data-type=json
#spring.cloud.sentinel.datasource.ds2.file.rule-type=flow
spring.cloud.sentinel.datasource.ds2.apollo.namespace-name=application
spring.cloud.sentinel.datasource.ds2.apollo.flow-rules-key=flowRules
spring.cloud.sentinel.datasource.ds2.apollo.default-flow-rule-value=[]
spring.cloud.sentinel.datasource.ds2.apollo.rule-type=flow
@SentinelResource(value = "sayHello",blockHandler ="handleException",blockHandlerClass = ExceptionUtil.class)
Field   说明 默认值
resource   资源名,资源名是限流规则的作用对象
count  限流阈值
grade  限流阈值类型,QPS 或线程数模式  QPS 模式
limitApp   流控针对的调用来源  default,代表不区分调用来源
strategy   判断的根据是资源自身,还是根据其它关联资源 (refResource),还是根据链路入口   根据资源本身
controlBehavior    流控效果(直接拒绝 / 排队等待 / 慢启动模式)  直接拒绝在Spring Cloud Sentinel中,降级(degrade)是指当资源被大量调用,且每个调用都执行了较长时间,为了保护系统不被打垮,Sentinel会对资源进行降级处理,即停止该资源的访问。降级规则可以通过以下参数进行配置:
Field  说明 默认值
grade:降级规则的级别,分为0(允许),1(警告),2(错误)。
count:触发降级的最小请求数。
timeWindow:时间窗口,单位为毫秒,请求次数在这个时间窗口内达到阈值则触发降级。
minRequestAmount:降级后每分钟允许的最小请求数,用于自动恢复服务。
slowRatioThreshold:慢请求比例阈值,当慢请求比例超过这个值就会进行降级。
statIntervalMs:统计时间窗口,用于计算慢请求比例,单位为毫秒。

flow 规则

[{"resource": "sayHello","controlBehavior": 0,"count": 1,"grade": 1,"limitApp": "default","strategy": 0}
]

 degrade.json

[{"resource": "sayHello","count": 100.0,"timeWindow": 10,"grade": 1,"countType": 1
}]

zuul 源码解析

hystrix

gateway 源码解析

sentinel 源码解析

遇到问题解决方式

springcloud 2020.0.3重大改变

当前组件  替代品

Hystrix  Resilience4j

Hystrix Dashboard / Turbine  Micrometer + Monitoring System

Ribbon  Spring Cloud Loadbalancer

Zuul 1    Spring Cloud Gateway

Archaius 1  Spring Boot external config + Spring Cloud Config

以下模块已从spring-cloud-netflix中删除:

spring-cloud-netflix-archaius
spring-cloud-netflix-concurrency-limits
spring-cloud-netflix-core
spring-cloud-netflix-dependencies
spring-cloud-netflix-hystrix
spring-cloud-netflix-hystrix-contract
spring-cloud-netflix-hystrix-dashboard
spring-cloud-netflix-hystrix-stream
spring-cloud-netflix-ribbon
spring-cloud-netflix-sidecar
spring-cloud-netflix-turbine
spring-cloud-netflix-turbine-stream
spring-cloud-netflix-zuul
spring-cloud-starter-netflix-archaius
spring-cloud-starter-netflix-hystrix
spring-cloud-starter-netflix-hystrix-dashboard
spring-cloud-starter-netflix-ribbon
spring-cloud-starter-netflix-turbine
spring-cloud-starter-netflix-turbine-stream
spring-cloud-starter-netflix-zuul
Support for ribbon, hystrix and zuul was removed across the release train projects.                

 获取接口start.spring.io/actuator/info

maven地址Maven Repository: org.mybatis.spring.boot » mybatis-spring-boot-starter » 2.2.2 (mvnrepository.com)

https://start.aliyun.com/

参考文献

Spring Cloud Zuul原理与注意事项_zuul配置-CSDN博客

Sentinel 流量控制限流框架详解_限流框架sentinel-CSDN博客

在生产环境中使用 Sentinel · alibaba/Sentinel Wiki · GitHub

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/773569.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第二证券|股票限售解禁是怎么回事?

限售股是指那些卖出受到限制的股票&#xff0c;其受到了不少投资者的重视。关于股票限售解禁是怎么回事&#xff0c;第二证券下面就为大家详细介绍一下。 股票限售解禁是指一些本来不能在二级商场上自在买卖的股票&#xff0c;当满足必定的条件后能够在二级商场上自在买卖。一…

【openGL4.x手册09】转换反馈

目录 一、说明二、着色器设置2.2 捕获的数据格式2.2 高级交错2.3 双精度和对齐2.4 In-shader规范 三、缓冲区绑定四、反馈过程五、反馈对象5.1 反馈暂停和恢复5.2 绑定暂停的反馈对象。 六、反馈渲染七、局限性 一、说明 转换反馈是捕获由顶点处理步骤生成的基元的过程&#xf…

如何用交换机组建LAN?

什么是LAN&#xff1f; 局域网&#xff08;LAN&#xff09;是连接有限区域&#xff08;例如办公楼、学校或家庭&#xff09;内设备的网络。它允许用户共享资源&#xff0c;包括数据、打印机和互联网访问。LAN连接设备以促进用户之间的协作和传输信息&#xff0c;例如计算机、打…

YOLOv9有效改进专栏汇总|未来更新卷积、主干、检测头注意力机制、特征融合方式等创新![2024/3/23]

​ 专栏介绍&#xff1a;YOLOv9改进系列 | 包含深度学习最新创新&#xff0c;助力高效涨点&#xff01;&#xff01;&#xff01; 专栏介绍 YOLOv9作为最新的YOLO系列模型&#xff0c;对于做目标检测的同学是必不可少的。本专栏将针对2024年最新推出的YOLOv9检测模型&#xff0…

【C语言】Infiniband驱动mlx4_reset

一、注释 这个 mlx4_reset 函数负责重置 Mellanox 设备。它保存了设备的 PCI 头信息&#xff0c;然后重置了设备&#xff0c;之后还原保存的 PCI 头信息。请注意&#xff0c;该函数是用英文注释的&#xff0c;下面提供中文注释的版本。以下是该函数的流程&#xff1a; 1. 为保…

基于51单片机的鸡蛋孵化环境监测警报系统Proteus仿真

地址&#xff1a;https://pan.baidu.com/s/1-OTZcuPHiZwdfd5KCaG7NA 提取码&#xff1a;1234 仿真图&#xff1a; 1、使用ADC0808测量一路模拟量&#xff08;可以表示温度、湿度、烟雾等等&#xff09; 2、如果测量值低于阀值&#xff0c;启动继电器&#xff1b;高于阀值&…

STM32之HAL开发——RCC外设CubeMX配置时钟

RCC外设介绍 RCC是Reset and Clock Control (复位和时钟控制)的缩写&#xff0c;它是STM32内部的一个重要外设&#xff0c;负责管理各种时钟源和时钟分频&#xff0c;以及为各个外设提供时钟使能。RCC模块可以通过寄存器操作或者库函数来配置。 RCC是复位和时钟控制模块&#…

Motorbike Physics Tool

此软件包适用于任何想要使用他们拥有的摩托车和玩家模型,并快速将其变成可驾驶摩托车而无需编写一行代码的人。 包说明 在这个工具中,它允许您使用您拥有的任何自行车模型,并将其变成可驾驶的摩托车,而无需编写任何代码。对于那些想在游戏中拥有自行车但又不想学习如何制作…

Go第三方框架--gin框架(二)

4. gin框架源码–Engine引擎和压缩前缀树的建立 讲了这么多 到标题4才开始介绍源码&#xff0c;主要原因还是想先在头脑中构建起 一个大体的框架 然后再填肉 这样不容易得脑血栓。标题四主要涉及标题2.3的步骤一 也就是 标题2.3中的 粗线框中的内容 4.1 Engine 引擎的建立 见…

工作记录 3月27日

复现计划&#xff1a; 数据集准备 跑通follow工作的前置代码 准备一个跑通的训练代码&#xff08;不管正确性&#xff09; 准备benchmark 跑一次训练 进一步阅读论文&#xff0c;更正训练代码&#xff0c;迭代 对两个方法的训练过程没有完全理解清楚&#xff0c;花了一天时间一…

43 带 fixed 列的 el-table 不兼容于 sortablejs

前言 这是一个基于 sortablejs 来实现的 el-table 的拖拽功能的基础实现 然后 这个过程中遇到的一个比较特殊的问题是, 关于 el-table-column 的 fixed 的属性, 对于 sortablejs 这边来定位目标选择列 影响的一个问题 在基础的用例中, 使用 “.el-table__body-wrapper tbo…

力扣--931.下降路径最小和

给你一个 n x n 的 方形 整数数组 matrix &#xff0c;请你找出并返回通过 matrix 的下降路径 的 最小和 。 下降路径 可以从第一行中的任何元素开始&#xff0c;并从每一行中选择一个元素。在下一行选择的元素和当前行所选元素最多相隔一列&#xff08;即位于正下方或者沿对角…

富格林:更改错误操作正规盈利出金

富格林指出&#xff0c;一些投资者在做单的时候会存在一些不好的交易习惯&#xff0c;这些习惯往往影响到交易的最终结果。想要减少亏损正规盈利出金&#xff0c;投资者需要更改这些不良习惯&#xff0c;才能从根源的接近问题有效提高出金。为帮助投资正规盈利出金&#xff0c;…

2024/3/26 C++作业

定义一个矩形类&#xff08;Rectangle&#xff09;&#xff0c;包含私有成员&#xff1a;长(length)、宽&#xff08;width&#xff09;, 定义成员函数&#xff1a; 设置长度&#xff1a;void set_l(int l) 设置宽度&#xff1a;void set_w(int w) 获取长度&#xff1a;int…

嵌入式|蓝桥杯STM32G431(HAL库开发)——CT117E学习笔记11:数字电位器MCP4017

系列文章目录 嵌入式|蓝桥杯STM32G431&#xff08;HAL库开发&#xff09;——CT117E学习笔记01&#xff1a;赛事介绍与硬件平台 嵌入式|蓝桥杯STM32G431&#xff08;HAL库开发&#xff09;——CT117E学习笔记02&#xff1a;开发环境安装 嵌入式|蓝桥杯STM32G431&#xff08;…

【Linux】详细分析/dev/loop的基本知识 | 空间满了的解决方法

目录 前言1. 基本知识2. 内存满了2.1 清空2.2 扩增 3. 彩蛋 前言 服务器一直down机&#xff0c;翻找日志文件一直找不到缘由&#xff0c;最终发现是挂载的内存满了&#xff0c;那本身这个文件就什么用呢&#xff1f; 1. 基本知识 /dev/loop是一种特殊的设备文件&#xff0c;…

005 Math类中的常用方法

Math类中包含一些用于执行基本数字运算的方法。 算术运算 方法描述abs(double a)返回 double值的绝对值。sqrt(double a) 返回 double值的正确舍入正平方根。 cbrt(double a)返回 double值的立方根。max(double a, double b)返回两个 double值中较大的 double 。min(double a…

I2C系列(三):软件模拟I2C读写24C02

一.目标 PC 端的串口调试软件通过 RS-485 与单片机通信&#xff0c;控制单片机利用软件模拟 I2C 总线对 EEPROM&#xff08;24C02&#xff09; 进行任意读写。 二.硬件简述 2.1 24C02硬件参数 24C02器件地址为0x50&#xff0c;存储容量为256字节&#xff0c;存储单元地址位数…

【SpringBoot】java.lang.Exception: No tests found matching Method

目录 问题解决 问题 在运行SpringBootMaven工程时&#xff0c;创建了一个新的Test单元测试&#xff0c;在运行时遇到的问题如下&#xff1a; java.lang.Exception: No tests found matching Method test_chatGPT(cn.bugstack.chatbot.api.test.ApiTest) from org.junit.inter…

使用 Docker Swarm(集群) 和Docker Stack(堆栈)部署容器化应用

1、Docker Swarm简介 说到集群&#xff0c;第一个想到的就是k8s&#xff0c;但docker官方也提供了集群和编排解决方案&#xff0c;它允许你将多个 Docker 主机连接在一起&#xff0c;形成一个“群集”&#xff08;Swarm&#xff09;&#xff0c;并可以在这个 Swarm 上运行和管…