• [Spring Cloud] Hystrix通过配置文件统一设置参数/与OpenFeign结合使用


    ✨✨个人主页:沫洺的主页

    📚📚系列专栏: 📖 JavaWeb专栏📖 JavaSE专栏 📖 Java基础专栏📖vue3专栏 

                               📖MyBatis专栏📖Spring专栏📖SpringMVC专栏📖SpringBoot专栏

                               📖Docker专栏📖Reids专栏📖MQ专栏📖SpringCloud专栏     

    💖💖如果文章对你有所帮助请留下三连✨✨

    衔接上篇: [Spring Cloud] Hystrix三大特性--降级,熔断,隔离_沫洺的博客-CSDN博客 

    🍁配置文件统一设置参数

    🌿全局默认配置

    1. #统计的时间窗口,默认为10s,一般不需要更改
    2. hystrix.command.default.metrics.rollingStats.timeInMilliseconds=3000
    3. #是否开启降级
    4. hystrix.command.default.fallback.enabled=true
    5. #是否开启断路器
    6. hystrix.command.default.circuitBreaker.enabled=true
    7. #失败率达到多少后跳闸,在统计窗口期中,请求数大于阈值并且失败率达到60,则触发断路,断路器开启,链路中断
    8. hystrix.command.default.circuitBreaker.errorThresholdPercentage=60
    9. #请求最小触发次数
    10. hystrix.command.default.circuitBreaker.requestVolumeThreshold=3
    11. #断路后休眠状态的时长,默认为5s,断路8s后断路器进入半开状态
    12. hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=10000
    13. #开启超时机制
    14. hystrix.command.default.execution.timeout.enabled=true
    15. #取消是否中断
    16. hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel=true
    17. #超时是否中断
    18. hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
    19. #超时阈值,单位是毫秒
    20. hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000

    通过配置文件设置参加,就不需要在@HystrixCommand注解中配置参数commandProperties

    🌾私有配置

     要想针对某个方法进行配置

    1. hystrix.command.commandKey.metrics.rollingStats.timeInMilliseconds=30000
    2. hystrix.command.commandKey.fallback.enabled=true
    3. hystrix.command.commandKey.circuitBreaker.enabled=true
    4. hystrix.command.commandKey.circuitBreaker.errorThresholdPercentage=50
    5. hystrix.command.commandKey.circuitBreaker.requestVolumeThreshold=3
    6. hystrix.command.commandKey.circuitBreaker.sleepWindowInMilliseconds=80000
    7. hystrix.command.commandKey.execution.isolation.thread.interruptOnFutureCancel=true
    8. hystrix.command.commandKey.execution.isolation.thread.interruptOnTimeout=true
    9. hystrix.command.commandKey.execution.isolation.thread.timeoutInMilliseconds=3000
    10. hystrix.command.commandKey.execution.timeout.enabled=true

    定义test2方法,指定commandKey="hello2"

    1. @RestController
    2. public class Test {
    3. @GetMapping("/test")
    4. //@HystrixCommand(fallbackMethod = "hello_fallback",commandProperties =
    5. // {
    6. // @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //开启熔断器
    7. // @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value ="30000"), //统计时间窗
    8. // @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "3"), //统计时间窗内最小请求次数阈值
    9. // @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //休眠时间窗口期
    10. // @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "30"), //时间窗内失败阈值百分比
    11. // //执行隔离线程超时
    12. // @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,value = "3000")
    13. //
    14. // })
    15. @HystrixCommand(fallbackMethod = "hello_fallback")
    16. public String hello(@RequestParam Integer time){
    17. ThreadUtil.sleep(time*1000);
    18. return "原方法";
    19. }
    20. @GetMapping("/test2")
    21. @HystrixCommand(fallbackMethod = "hello_fallback",commandKey = "hello2")
    22. public String hello2(@RequestParam Integer time){
    23. ThreadUtil.sleep(time*1000);
    24. return "原方法";
    25. }
    26. public String hello_fallback(Integer time){
    27. return "兜底方法";
    28. }
    29. }

    将配置中的commandKey替换成hello2,如下

    1. hystrix.command.hello2.metrics.rollingStats.timeInMilliseconds=30000
    2. hystrix.command.hello2.fallback.enabled=true
    3. hystrix.command.hello2.circuitBreaker.enabled=true
    4. hystrix.command.hello2.circuitBreaker.errorThresholdPercentage=30
    5. hystrix.command.hello2.circuitBreaker.requestVolumeThreshold=3
    6. hystrix.command.hello2.circuitBreaker.sleepWindowInMilliseconds=10000
    7. hystrix.command.hello2.execution.isolation.thread.interruptOnFutureCancel=true
    8. hystrix.command.hello2.execution.isolation.thread.interruptOnTimeout=true
    9. hystrix.command.hello2.execution.isolation.thread.timeoutInMilliseconds=3000
    10. hystrix.command.hello2.execution.timeout.enabled=true

    🍂Hystrix与OpenFeign结合使用

    Feign内部是支持Hystrix的

    模拟hystrix-app服务调用nacos-a服务

    子模块spring-cloud-hystrix添加依赖

    1. <dependency>
    2. <groupId>com.alibaba.cloudgroupId>
    3. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.springframework.cloudgroupId>
    7. <artifactId>spring-cloud-starter-openfeignartifactId>
    8. dependency>
    9. <dependency>
    10. <groupId>org.springframework.cloudgroupId>
    11. <artifactId>spring-cloud-starter-netflix-ribbonartifactId>
    12. dependency>

    pom.xml

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>com.mominggroupId>
    7. <artifactId>spring-cloud-rootartifactId>
    8. <version>0.0.1-SNAPSHOTversion>
    9. <relativePath>../pom.xmlrelativePath>
    10. parent>
    11. <artifactId>spring-cloud-hystrixartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.bootgroupId>
    15. <artifactId>spring-boot-starter-webartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>org.springframework.cloudgroupId>
    19. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>com.alibaba.cloudgroupId>
    23. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframework.cloudgroupId>
    27. <artifactId>spring-cloud-starter-openfeignartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.cloudgroupId>
    31. <artifactId>spring-cloud-starter-netflix-ribbonartifactId>
    32. dependency>
    33. <dependency>
    34. <groupId>cn.hutoolgroupId>
    35. <artifactId>hutool-allartifactId>
    36. dependency>
    37. <dependency>
    38. <groupId>org.projectlombokgroupId>
    39. <artifactId>lombokartifactId>
    40. <optional>trueoptional>
    41. dependency>
    42. <dependency>
    43. <groupId>com.mominggroupId>
    44. <artifactId>user-apiartifactId>
    45. <version>0.0.1-SNAPSHOTversion>
    46. <scope>compilescope>
    47. dependency>
    48. dependencies>
    49. <build>
    50. <plugins>
    51. <plugin>
    52. <groupId>org.springframework.bootgroupId>
    53. <artifactId>spring-boot-maven-pluginartifactId>
    54. <configuration>
    55. <excludes>
    56. <exclude>
    57. <groupId>org.projectlombokgroupId>
    58. <artifactId>lombokartifactId>
    59. exclude>
    60. excludes>
    61. configuration>
    62. plugin>
    63. plugins>
    64. build>
    65. project>

    相关配置信息

    1. #nacos相关配置
    2. spring.application.name=hystrix-app
    3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    4. #新版SpringBoot已不再需要@EnableDiscoveryClient,只需要在pom文件中加入服务发现实现类的maven坐标即可。
    5. #默认true,开启服务自动发现
    6. spring.cloud.discovery.enabled=true
    7. #配置feign开启hystrix所有功能
    8. feign.hystrix.enabled=true
    9. #Ribbon相关配置
    10. ##建立连接的超时时间
    11. ribbon.ConnectionTimeout=5000
    12. ##连接成功后,读取资源数据时的超时配置
    13. ribbon.ReadTimeout = 3000

    启动类添加注解

    1. @SpringBootApplication
    2. @EnableHystrix
    3. //@EnableDiscoveryClient
    4. @EnableFeignClients
    5. public class HystrixApp {
    6. public static void main(String[] args) {
    7. SpringApplication.run(HystrixApp.class, args);
    8. }
    9. }

    创建Feign客户端调用a服务,当调用失败时,降级回退到AServerClientFallBack兜底类,执行兜底方法

    @FeignClient指定参数fallback=AServerClientFallBack.class

    1. @FeignClient(name = "nacos-a",fallback = AServerClientFallBack.class)
    2. public interface AServerClient extends IUserService {
    3. //##特殊配置key规则为:#(,...)
    4. //AServerClient#getSleep(Integer)
    5. //public String getSleep(Integer time) ;
    6. }

    全局熔断配置同上

    对回退兜底类的某个方法设置熔断配置

    配置key规则为:

    #(,...)

    1. #OpenFeign自定义配置
    2. hystrix.command.AServerClient#getSleep(Integer).metrics.rollingStats.timeInMilliseconds=30000
    3. hystrix.command.AServerClient#getSleep(Integer).fallback.enabled=true
    4. hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.enabled=true
    5. hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.errorThresholdPercentage=50
    6. hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.requestVolumeThreshold=3
    7. hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.sleepWindowInMilliseconds=10000
    8. hystrix.command.AServerClient#getSleep(Integer).execution.isolation.thread.interruptOnFutureCancel=true
    9. hystrix.command.AServerClient#getSleep(Integer).execution.isolation.thread.interruptOnTimeout=true
    10. hystrix.command.AServerClient#getSleep(Integer).execution.isolation.thread.timeoutInMilliseconds=3000
    11. hystrix.command.AServerClient#getSleep(Integer).execution.timeout.enabled=true

    AServerClientFallBack

    1. @Component
    2. public class AServerClientFallBack implements AServerClient{
    3. @Override
    4. public String getName(Integer id) {
    5. return null;
    6. }
    7. @Override
    8. public Integer getAmount(Integer id) {
    9. return null;
    10. }
    11. @Override
    12. public String getSleep(Integer time) {
    13. return "sleep兜底方法";
    14. }
    15. }

    创建测试接口

    1. @RestController
    2. public class FeignController {
    3. @Autowired
    4. private AServerClient aServerClient;
    5. @GetMapping("/feign/test")
    6. public String test(@RequestParam Integer time){
    7. return aServerClient.getSleep(time);
    8. }
    9. }

    访问,模拟熔断

     

  • 相关阅读:
    EN 16034门窗及配件—CE认证
    anroid html5 拍照扫码
    SpringMVC log4j1升级log4j2
    【算法刷题日记之本手篇】星际密码与数根
    How to install mongodb 7.0 to Ubuntu 22.04
    mybatis实现CRUD及相关配置
    计算机毕业设计springboot家具销售系统tj2lo源码+系统+程序+lw文档+部署
    零基础Linux_6(开发工具_下)函数库链接+Makefile+实现进度条+Git
    字节跳动开源Linux 内核网络抓包工具 netcap
    【DL论文精读笔记】Image Segmentation Using Deep Learning: A Survey 图像分割综述
  • 原文地址:https://blog.csdn.net/HeyVIrBbox/article/details/127979646