当前位置:   article > 正文

SpringCloud 使用sentinel_springcloud sentinel 使用

springcloud sentinel 使用

一、添加依赖

  1. <dependency>
  2. <groupId>com.alibaba.cloud</groupId>
  3. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  4. </dependency>

二、配置文件配置地址

  1. spring:
  2. cloud:
  3. sentinel:
  4. transport:
  5. dashboard: localhost:8080

 三、流控模式介绍

直接:统计当前资源的请求,触发阈值时对当前资源直接限流,也是默认的模式

关联:统计与当前资源相关的另一个资源触发阈值时,对当前资源限流

        使用场景:比如用户支付时需要修改订单状态,同时用户要查询订单。查询和修改操作会争抢数据库锁,产生竞争。业务需求是有限支付和更新订单的业务,因此当修改订单业务触发阈值时,需要对查询订单业务限流。

链路:统计从指定链路访问到本资源的请求触发阈值时,对指定链路限流

例如有两条请求链路:

/test1→ /common
/test2→/common

如果只希望统计从/test2进入到/common的请求,则可以这样配置:

注意:

1、Sentinel默认会将Controller方法做context整合,导致链路模式的流控失效,需要修改application.yml,添加配置:

spring:
 
cloud:
    sentinel:
     
web-context-unify: false # 关闭context整合

2、Sentinel默认只标记Controller中的方法为资源,如果要标记其它方法(如service方法),需要利用@SentinelResource注解,示例:

@SentinelResource("goods")
public void queryGoods() {
    System.
err.println("查询商品");
}

 四、流控效果

快速失败:QPS超过阈值时,拒绝新的请求。

warm up: QPS超过阈值时,拒绝新的请求;但QPS阈值是逐渐提升的,可以避免冷启动时高并发导致服务宕机。

排队等待:请求会进入队列,按照阈值允许的时间间隔依次执行请求;如果请求预期等待时长大于超时时间,直接拒绝。

五、热点参数限流

热点参数限流是分别统计参数值相同的请求,判断是否超过QPS阈值。并且可以对部分参数设置例外配置:

注意:热点参数限流对默认的SpringMVC资源无效,必须手动添加@SentinelResource注解

 六、FeignClient整合Sentinel

Sentinel支持的雪崩解决方案:1、线程隔离(仓壁模式)2、降级熔断

(1)在application.yml中配置:feign.sentienl.enable=true

feign:
  sentinel:
   
enabled: true # 开启FeignSentinel功能

(2)给FeignClient编写FallbackFactory并注册为Bean

  1. public class UserClientFallbackFactory implements FallbackFactory<UserClient> {
  2. @Override
  3. public UserClient create(Throwable throwable) {
  4. // 创建UserClient接口实现类,实现其中的方法,编写失败降级的处理逻辑
  5. return new UserClient() {
  6. @Override
  7. public User findById(Long id) {
  8. // 记录异常信息
  9. log.error("查询用户失败", throwable);
  10. // 根据业务需求返回默认的数据,这里是空用户
  11. return new User();
  12. }
  13. };
  14. }
  15. }
  16. @Bean
  17. public UserClientFallbackFactory userClientFallback(){
  18. return new UserClientFallbackFactory();
  19. }

(3)将FallbackFactory配置到FeignClient

  1. @FeignClient(value = "userservice", fallbackFactory =UserClientFallbackFactory.class)
  2. public interface UserClient {
  3. @GetMapping("/user/{id}")
  4. User findById(@PathVariable("id") Long id);
  5. }

 七、熔断降级策略

1、慢调用比例:超过指定时长的调用为慢调用,统计单位时长内慢调用的比例,超过阈值则熔断

2、异常比例:统计单位时长内异常调用的比例,超过阈值则熔断

3、异常数:统计单位时长内异常调用的次数,超过阈值则熔断

八、授权规则

授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式。

 Sentinel是通过RequestOriginParser这个接口的parseOrigin来获取请求的来源的。

  1. public interface RequestOriginParser {
  2. /**
  3. * 从请求request对象中获取origin,获取方式自定义
  4. */
  5. String parseOrigin(HttpServletRequest request);
  6. }

所以在填写流控应用的时候,是根据自定义的RequestOriginParser来填写(比如在请求header上添加值来区分是网关过来的请求还是直接访问的服务的请求)

  1. @Component
  2. public class HeaderOriginParser implements RequestOriginParser {
  3. @Override
  4. public String parseOrigin(HttpServletRequest request) {
  5. String origin = request.getHeader("origin");
  6. if(StringUtils.isEmpty(origin)){
  7. return "blank";
  8. }
  9. return origin;
  10. }
  11. }

九、自定义返回方法

只需实现BlockExceptionHandler接口即可

  1. @Component
  2. public class SentinelBlockHandler implements BlockExceptionHandler {
  3. @Override
  4. public void handle(
  5. HttpServletRequest httpServletRequest,
  6. HttpServletResponse httpServletResponse, BlockException e) throws Exception {
  7. String msg = "未知异常";
  8. int status = 429;
  9. if (e instanceof FlowException) {
  10. msg = "请求被限流了!";
  11. } else if (e instanceof DegradeException) {
  12. msg = "请求被降级了!";
  13. } else if (e instanceof ParamFlowException) {
  14. msg = "热点参数限流!";
  15. } else if (e instanceof AuthorityException) {
  16. msg = "请求没有权限!";
  17. status = 401;
  18. }
  19. httpServletResponse.setContentType("application/json;charset=utf-8");
  20. httpServletResponse.setStatus(status);
  21. httpServletResponse.getWriter().println("{\"message\": \"" + msg + "\", \"status\": " + status + "}");
  22. }
  23. }

 十、配置规则持久化三种模式

1、原始模式:保存在内存

2、pull模式:保存在本地文件或数据库,定时去读取

3、push模式:保存在nacos,监听变更实时更新

通常生产环境采用push模式,需要修改Sentinel-dashboard的源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/878102
推荐阅读
相关标签
  

闽ICP备14008679号