、整合pom文件
PS:nacos启动命令:startup.cmd -m standalone
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
二、在application.yml中添加属性配置
# 配置actuator和Sentinel联动
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
三、测试运行结果,启动IDEA
访问sentinel网页链接代码:http://localhost:8070/actuator/sentinel
PS:访问端口号根据自己IDEA项目的端口号进行修改调整
四、导入sentinel-dashboard-1.6.2.jar的jar包并启动(启动命令:java -jar sentinel-dashboard-1.6.2.jar)
在网页中打开链接:http://localhost:8080/#/dashboard/home
,给application.yml添加sentinel添加配置属性(添加配置属性时需要和nacos平级)
sentinel:
transport:
#指定sentin控制台的地址
dashboard: localhost:8080
4.1、我们就可以通过访问网页来监控到并对其进行限流
流控规则
4.1、控流效果(限流)
限流的目的是通过限制并发访问数或者限制一个窗口内允许处理的请求数量来保护系统,一旦达到限制流量,则对当前请求处理采取对应的的拒绝策略。如跳转错误页面、进行排队、服务降级等。
快速失败:直接抛出异常
Warm Up:根据codeFactor(默认3)的值,从阈值/codeFactor,经过预热时长,才到达设置的QPS阈值
(关于WarmUp详情参考:https://github.com/alibaba/Sentinel/wiki/%E9%99%90%E6%B5%81---%E5%86%B7%E5%90%AF%E5%8A%A8)
排队等待(单位毫秒):适用于应对突发情况(突然流量剧增)的场景,匀速排队,让请求以均匀的速度通过,阈值类型必须设成QPS,否则无效
————————————————
版权声明:本文为CSDN博主「大坏蛋^_^」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qzc70919700/article/details/122579231
五、降级规则(熔断)
服务熔断是指当前服务提供者无法正常为服务调用者提供服务时,比如请求超时、服务异常等,为了防止整个系统出现雪崩效应,暂时将出现故障的接口隔离出来,断绝与外部接口的联系,当触发熔断之后,后续一段时间内该服务调用者的请求都会直接失败,直到目标服务恢复正常。
参考官方文档:https://github.com/alibaba/Sentinel/wiki
5.1、降级-RT(单位:毫秒)
比如1秒内持续进入5个请求,对应时刻的平均响应时间均超过阈值,那么接下来在一个固定的时间窗口内,对这个方法的访问都会自动熔断。
平均响应时间(RT单位毫秒,时间窗口单位秒)
注意:RT默认最大4900ms,可以通过-Dcsp.sentinel.statistic.max.rt=xxx修改
5.2、降级-异常比例(单位:毫秒)
当某个方法每秒调用所获得的异常总数的比例超过设定的阈值时,该资源会自动进入降级状态,也就是在接下来的一个固定的时间窗口内,对这个方法的调用都会自动返回。
5.3、降级-异常数(单位:分钟)
和异常比例类似,当某个方法在指定时间窗口内获得的异常数量超过阈值时,会触发熔断。
注意:时间窗口 < 60秒可能会出现问题
六、我们可以使用Feign整合Sentinel
非常简单,只需要在application.yml中配置属性即可(该属性idea不会有提示,但是功能可用,直接手写上去就行)ps:注意顶格粘贴
feign:
sentinel:
enabled: true
七、微服务对限流后的处理fallbackfactory
7.1、在feignclient下新建一个fallbackfactory包,然后在新建一个UserCenterFeignClientFallbackFactory 类
package com.example.cloud.contercenter.feignclient.fallbackfactory;
import com.example.cloud.contercenter.entity.User;
import com.example.cloud.contercenter.feignclient.UserCenterFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class UserCenterFeignClientFallbackFactory implements FallbackFactory<UserCenterFeignClient> {
@Override
public UserCenterFeignClient create(Throwable throwable) {
return new UserCenterFeignClient(){
@Override
public User findUserById(String id) {
log.warn("*****************已经被限流");
User u=new User();
u.setUsername("对不起,你已经被限流了");
return u;
}
};
}
}
7.2、在Feign的客户端接口上添加引用
@FeignClient(name = "user-center", fallbackFactory = UserCenterFeignClientFallbackFactory.class)
package com.example.cloud.contercenter.feignclient;
import com.example.cloud.contercenter.entity.User;
import com.example.cloud.contercenter.feignclient.fallbackfactory.UserCenterFeignClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "user-center",fallbackFactory = UserCenterFeignClientFallbackFactory.class)
public interface UserCenterFeignClient {
@GetMapping("/user/{id}")
public User findUserById(@PathVariable String id);
}