• 服务断路器_Resilience4j超时降级


    创建模块cloud-consumer-resilience4j-order80

    image-20220223172803252

    POM引入依赖

    1. <dependencies>
    2. <!-- 引入Eureka 客户端依赖 -->
    3. <dependency>
    4. <groupId>org.springframework.cloud</groupId>
    5. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    6. </dependency>
    7. <!-- 引入服务调用依赖 OpenFigen -->
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-openfeign</artifactId>
    11. </dependency>
    12. <dependency>
    13. <groupId>org.springframework.boot</groupId>
    14. <artifactId>spring-boot-starter-web</artifactId>
    15. </dependency>
    16. <dependency>
    17. <groupId>org.projectlombok</groupId>
    18. <artifactId>lombok</artifactId>
    19. <version>1.18.22</version>
    20. </dependency>
    21. <!-- actuator监控信息完善 -->
    22. <dependency>
    23. <groupId>org.springframework.boot</groupId>
    24. <artifactId>spring-boot-starter-actuator</artifactId>
    25. </dependency>
    26. <dependency>
    27. <groupId>io.github.resilience4j</groupId>
    28. <artifactId>resilience4j-spring-cloud2</artifactId>
    29. </dependency>
    30. <dependency>
    31. <groupId>org.springframework.cloud</groupId>
    32. <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
    33. </dependency>
    34. </dependencies>

    修改YML文件

    1. server:
    2. port: 80
    3. eureka:
    4. instance:
    5. #实例名字
    6. instance-id: cloud-order-resilience4j-consumer80
    7. client:
    8. # Eureka Server地址
    9. service-url:
    10. # 单机Eureka server 地址
    11. # defaultZone: http://localhost:7001/eureka
    12. defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/
    13. spring:
    14. application:
    15. # 设置应用名字
    16. name: cloud-order-consumer
    17. logging:
    18. level:
    19. com.hgy.service: debug
    20. # 超时机制
    21. resilience4j:
    22. timelimiter:
    23. instances:
    24. delay:
    25. # 设置超时时间 5
    26. timeoutDuration: 2

    编写服务提供者提供超时方法

    1. /**
    2. * 超时服务
    3. * @return
    4. */
    5. @GetMapping("timeout")
    6. public String timeout(){
    7. try {
    8. TimeUnit.SECONDS.sleep(5);
    9. } catch (InterruptedException e) {
    10. e.printStackTrace();
    11. }
    12. return "success";
    13. }

    编写服务提供者Controller

    1. @GetMapping("/timeout")
    2. @TimeLimiter(name = "delay",fallbackMethod = "timeoutfallback")
    3. public CompletableFuture timeout() {
    4. log.info("********* 进入方法 ******");
    5. //异步操作
    6. CompletableFuture completableFuture = CompletableFuture
    7. .supplyAsync((Supplier) () -> (paymentFeignService.timeout()));
    8. log.info("********* 离开方法 ******");
    9. return completableFuture;
    10. }

    编写服务降级方法

    1. /**
    2. * 超时服务降级方法
    3. * @param e
    4. * @return
    5. */
    6. public CompletableFuture timeoutfallback(Exception e){
    7. e.printStackTrace();
    8. return CompletableFuture.completedFuture(ResponseEntity.ok("超时啦"));
    9. }

    测试

    image-20220214172235232

  • 相关阅读:
    DPDK LPM库(学习笔记)
    跨平台Markdown编辑软件Typora mac中文版功能介绍
    Linux新的IO模型io_uring
    leetcode 698. 划分为k个相等的子集-状态压缩+记忆搜索的一步步实现
    [免费专栏] Android安全之Android工程模式
    PostgreSQL常用数据类型
    AttributeError: module ‘scipy.signal‘ has no attribute ‘correlation_lags‘
    github上的nemo_go项目
    尚硅谷尚品项目汇笔记(一)
    Vue3 -- 入门
  • 原文地址:https://blog.csdn.net/weixin_68967484/article/details/131710612