使用Sentinel API

Sentinel主要有三个核心Api:

SphU:定义资源,让资源收到监控,保护资源

SphU 包含了 try-catch 风格的 API。用这种方式,当资源发生了限流之后会抛出 BlockException。这个时候可以捕捉异常,进行限流之后的逻辑处理。

  1. String resourceName = "test_sentinel_api";
  2. Entry test_sentinel_api = null;
  3. // 定义一个Sentinel保护的资源
  4. try {
  5. test_sentinel_api = SphU.entry(resourceName);
  6. ...
  7. }
  8. // 若被保护的资源被限流或者降级
  9. catch (BlockException e) {
  10. e.printStackTrace();
  11. return "限流,或者降级了";
  12. }
  13. finally {
  14. if (test_sentinel_api != null) {
  15. test_sentinel_api.exit();
  16. }
  17. }

PS:SphU.entry(xxx) 需要与 entry.exit() 方法成对出现,匹配调用,否则会导致调用链记录异常,抛出 ErrorEntryFreeException 异常。

若 entry 的时候传入了热点参数,那么 exit 的时候也一定要带上对应的参数(exit(count, args)),否则可能会有统计错误。这个时候不能使用 try-with-resources 的方式。

Tracer:异常统计

  1. // 针对来源
  2. String resourceName = "test_sentinel_api";
  3. Entry test_sentinel_api = null;
  4. // 定义一个Sentinel保护的资源
  5. try {
  6. test_sentinel_api = SphU.entry(resourceName);
  7. if (StringUtils.isBlank(a)) {
  8. throw new IllegalArgumentException("a不能为空");
  9. }
  10. return a;
  11. }
  12. // 若被保护的资源被限流或者降级
  13. catch (BlockException e) {
  14. e.printStackTrace();
  15. return "限流,或者降级了";
  16. }
  17. catch (IllegalArgumentException e2) {
  18. // 统计IllegalArgumentException
  19. Tracer.trace(e2);
  20. return "参数非法";
  21. }
  22. finally {
  23. if (test_sentinel_api != null) {
  24. test_sentinel_api.exit();
  25. }
  26. }

通过 Tracer.trace(ex) 来统计异常信息时,由于 try-with-resources 语法中 catch 调用顺序的问题,会导致无法正确统计异常数,因此统计异常信息时也不能在 try-with-resources 的 catch 块中调用 Tracer.trace(ex)。

ContextUtil:实现调用来源,实现调用

  1. // 针对来源
  2. String resourceName = "test_sentinel_api";
  3. ContextUtil.enter(resourceName, "study01");

通过SentinelResource注解的方式

Sentinel 支持通过 @SentinelResource 注解定义资源并配置 blockHandler 和 fallback 函数来进行限流之后的处理。

  1. @GetMapping("test_sentinel_resource")
  2. @SentinelResource(
  3. value = "test_sentinel_resource",
  4. blockHandler = "block",
  5. fallback = "fallback"
  6. )
  7. public String testSentinelResource(@RequestParam(required = false) String a) {
  8. if (StringUtils.isBlank(a)) {
  9. throw new IllegalArgumentException("a不能为空");
  10. }
  11. return a;
  12. }
  13. /**
  14. * 处理限流或者降级
  15. */
  16. public String block(String a, BlockException e) {
  17. log.warn("限流,或者降级了", e);
  18. return "限流,或者降级了 block";
  19. }
  20. public String fallback(String a, BlockException e) {
  21. return "限流,或者降级了 fallback";
  22. }

@SentinelResource同样支持通过配置blockHandlerClass和fallbackClass配置类来进行限流后的处理

RestTemplate整合Sentinel

在初始化restTemplate的时候添加@SentinelRestTemplate注解就可以为RestTemplate整合Sentinel,代码如下:

  1. @Bean
  2. @LoadBalanced
  3. @SentinelRestTemplate
  4. public RestTemplate restTemplate() {
  5. return new RestTemplate();
  6. }

可以通过配置resttemplate. sentinel.enabled来开启或关闭整合,代码如下:

  1. resttemplate:
  2. sentinel:
  3. # 关闭、打开SentinelRestTemplate注解
  4. enabled: true

Feign整合Sentinel

在配置文件中添加如下配置便可以为Feign整合Sentinel:

  1. feign:
  2. # feign整合sentinel
  3. sentinel:
  4. enabled: true

如果服务被限流或降级时,如果想要定制定制自己的处理逻辑,可以使用@FeignClient注解的fallback属性;如果定制处理逻辑的同时,还想拿到异常的具体细节,可以使用fallbackFactory属性,示例代码如下:

  1. @FeignClient(
  2. name = "study02",
  3. // 拿不到异常
  4. fallback = CommentFeignClientFallback.class,
  5. // 拿到异常
  6. fallbackFactory = CommentFeignClientFallbackFactory.class
  7. )
  8. public interface CommentFeignClient {
  9. @GetMapping("/find")
  10. DemoComment find();
  11. }
  12. @Component
  13. public class CommentFeignClientFallback implements CommentFeignClient {
  14. /**
  15. * 一旦被限流,就会进入这个方法
  16. * @return
  17. */
  18. @Override
  19. public DemoComment find() {
  20. DemoComment demoComment = new DemoComment();
  21. return demoComment;
  22. }
  23. }
  24. @Component
  25. @Slf4j
  26. public class CommentFeignClientFallbackFactory implements FallbackFactory<CommentFeignClient> {
  27. @Override
  28. public CommentFeignClient create(Throwable throwable) {
  29. return new CommentFeignClient() {
  30. @Override
  31. public DemoComment find() {
  32. DemoComment demoComment = new DemoComment();
  33. return demoComment;
  34. }
  35. };
  36. }
  37. }

Spring Cloud Alibaba学习笔记(6) - Sentinel使用总结的更多相关文章

  1. Spring Cloud Alibaba学习笔记(1) - 整合Spring Cloud Alibaba

    Spring Cloud Alibaba从孵化器版本毕业:https://github.com/alibaba/spring-cloud-alibaba,记录一下自己学习Spring Cloud Al ...

  2. Spring Cloud Alibaba学习笔记(15) - 整合Spring Cloud Gateway

    Spring Cloud Gateway 概述 Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于Netty.Reactor以及WEbFlux构建,它 ...

  3. Spring Cloud Alibaba学习笔记

    引自B站楠哥:https://space.bilibili.com/434617924 一.创建父工程 创建父工程hello-spring-cloud-alibaba Spring Cloud Ali ...

  4. Spring Cloud Alibaba学习笔记(3) - Ribbon

    1.手写一个客户端负载均衡器 在了解什么是Ribbon之前,首先通过代码的方式手写一个负载均衡器 RestTemplate restTemplate = new RestTemplate(); // ...

  5. Spring Cloud Alibaba学习笔记(22) - Nacos配置管理

    目前业界流行的统一配置管理中心组件有Spring Cloud Config.Spring Cloud Alibaba的Nacos及携程开源的Apollo,本文将介绍Nacos作为统一配置管理中心的使用 ...

  6. Spring Cloud Alibaba学习笔记(2) - Nacos服务发现

    1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...

  7. Spring Cloud Alibaba学习笔记(7) - Sentinel规则持久化及生产环境使用

    Sentinel 控制台 需要具备下面几个特性: 规则管理及推送,集中管理和推送规则.sentinel-core 提供 API 和扩展接口来接收信息.开发者需要根据自己的环境,选取一个可靠的推送规则方 ...

  8. Spring Cloud Alibaba学习笔记(5) - 整合Sentinel及Sentinel规则

    整合Sentinel 应用整合Sentinel 在dependencies中添加依赖,即可整合Sentinel <dependency> <groupId>com.alibab ...

  9. Spring Cloud Alibaba学习笔记(18) - Spring Cloud Gateway 内置的过滤器工厂

    参考:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.1.0.RELEASE/single/spring-clou ...

  10. Spring Cloud Alibaba学习笔记(23) - 调用链监控工具Spring Cloud Sleuth + Zipkin

    随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求陷入性能瓶颈或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何 ...

随机推荐

  1. 都2019年了,Java为什么还在坚持多线程不选择协程?

    都2019年了,Java为什么还在坚持多线程不选择协程? - 知乎 https://www.zhihu.com/question/332042250/answer/734051666

  2. DialogFragment: DialogFragment的一些理解

    Android 自3.0版本引入了DialogFragment这个类,并推荐开发者使用这个类替代之前经常使用的Dialog类,那么DialogFragment相对于之前的Dialog究竟有什么优势呢? ...

  3. log4j实现日志自动清理功能

    log4j不支持自动清理功能,但是log4j2版本支持,log4j2是log4j的升级版,比logback先进. log4j升级为log4j2(不需要改动代码)https://blog.csdn.ne ...

  4. linux下phpmailer发送邮件出现SMTP ERROR: Failed to connect to server: (0)错误

    转自:https://www.cnblogs.com/raincowl/p/8875647.html //Create a new PHPMailer instance $mail = new PHP ...

  5. Access 字段拼接(UPDATE 数据追加)

    今天遇到一个需求,在Access数据库中,有个net_id 字段,它的值是由 “jjgrape” 这个字符串和 id 字段组成的,也就是说,要把 ‘jjgrape’ 和 id 字段拼接起来: 那怎么拼 ...

  6. k8s记录-ca证书制作(二)

    1)下载cfssl #!/bin/bash wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 wget https://pkg.cfssl.org/R ...

  7. 转 ORA-13541: system moving window baseline size (691200) greater than retention (432000)

    修改awr生成报告时间间隔和保存时间时报错,由默认的每小时生成,保存8天修改为每半个小时生成一次,保存5天: SQL> exec dbms_workload_repository.modify_ ...

  8. Linux下手动查杀木马

    (1).模拟木马程序病原体并让其自动运行 黑客让脚本自动执行的3种方法:1.计划任务:2.开机启动:3.系统命令被人替换,定一个触发事件. 1)生成木马程序病原体 [root@youxi1 ~]# v ...

  9. mybatis typeHandler类型转换器

    typeHandler类型转换器 在JDBC中,需要在PreparedStatement对象中设置那些已经预编译过的SQL语句的参数.执行SQL后,会通过ResultSet对象获取得到数据库的数据,而 ...

  10. LeetCode_392. Is Subsequence

    392. Is Subsequence Easy Given a string s and a string t, check if s is subsequence of t. You may as ...