[Spring Cloud] Hystrix通过配置文件统一设置参数/与OpenFeign结合使用
创始人
2024-02-08 10:32:32

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

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

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

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

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

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

🍁配置文件统一设置参数

🌿全局默认配置

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

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

🌾私有配置

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

hystrix.command.commandKey.metrics.rollingStats.timeInMilliseconds=30000
hystrix.command.commandKey.fallback.enabled=true
hystrix.command.commandKey.circuitBreaker.enabled=true
hystrix.command.commandKey.circuitBreaker.errorThresholdPercentage=50
hystrix.command.commandKey.circuitBreaker.requestVolumeThreshold=3
hystrix.command.commandKey.circuitBreaker.sleepWindowInMilliseconds=80000
hystrix.command.commandKey.execution.isolation.thread.interruptOnFutureCancel=true
hystrix.command.commandKey.execution.isolation.thread.interruptOnTimeout=true
hystrix.command.commandKey.execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.command.commandKey.execution.timeout.enabled=true

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

@RestController
public class Test {@GetMapping("/test")//@HystrixCommand(fallbackMethod = "hello_fallback",commandProperties =//        {//                @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //开启熔断器//                @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value ="30000"), //统计时间窗//                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "3"),      //统计时间窗内最小请求次数阈值//                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //休眠时间窗口期//                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "30"), //时间窗内失败阈值百分比//                //执行隔离线程超时//                @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,value = "3000")////        })@HystrixCommand(fallbackMethod = "hello_fallback")public String hello(@RequestParam Integer time){ThreadUtil.sleep(time*1000);return "原方法";}@GetMapping("/test2")@HystrixCommand(fallbackMethod = "hello_fallback",commandKey = "hello2")public String hello2(@RequestParam Integer time){ThreadUtil.sleep(time*1000);return "原方法";}public String hello_fallback(Integer time){return "兜底方法";}
}

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

hystrix.command.hello2.metrics.rollingStats.timeInMilliseconds=30000
hystrix.command.hello2.fallback.enabled=true
hystrix.command.hello2.circuitBreaker.enabled=true
hystrix.command.hello2.circuitBreaker.errorThresholdPercentage=30
hystrix.command.hello2.circuitBreaker.requestVolumeThreshold=3
hystrix.command.hello2.circuitBreaker.sleepWindowInMilliseconds=10000
hystrix.command.hello2.execution.isolation.thread.interruptOnFutureCancel=true
hystrix.command.hello2.execution.isolation.thread.interruptOnTimeout=true
hystrix.command.hello2.execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.command.hello2.execution.timeout.enabled=true

🍂Hystrix与OpenFeign结合使用

Feign内部是支持Hystrix的

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

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

        com.alibaba.cloudspring-cloud-starter-alibaba-nacos-discoveryorg.springframework.cloudspring-cloud-starter-openfeignorg.springframework.cloudspring-cloud-starter-netflix-ribbon

pom.xml


4.0.0com.momingspring-cloud-root0.0.1-SNAPSHOT../pom.xmlspring-cloud-hystrixorg.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-netflix-hystrixcom.alibaba.cloudspring-cloud-starter-alibaba-nacos-discoveryorg.springframework.cloudspring-cloud-starter-openfeignorg.springframework.cloudspring-cloud-starter-netflix-ribboncn.hutoolhutool-allorg.projectlomboklomboktruecom.mominguser-api0.0.1-SNAPSHOTcompileorg.springframework.bootspring-boot-maven-pluginorg.projectlomboklombok

相关配置信息

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

启动类添加注解

@SpringBootApplication
@EnableHystrix
//@EnableDiscoveryClient
@EnableFeignClients
public class HystrixApp {public static void main(String[] args) {SpringApplication.run(HystrixApp.class, args);}
}

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

@FeignClient指定参数fallback=AServerClientFallBack.class

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

全局熔断配置同上

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

配置key规则为:

#(,...)

#OpenFeign自定义配置
hystrix.command.AServerClient#getSleep(Integer).metrics.rollingStats.timeInMilliseconds=30000
hystrix.command.AServerClient#getSleep(Integer).fallback.enabled=true
hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.enabled=true
hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.errorThresholdPercentage=50
hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.requestVolumeThreshold=3
hystrix.command.AServerClient#getSleep(Integer).circuitBreaker.sleepWindowInMilliseconds=10000
hystrix.command.AServerClient#getSleep(Integer).execution.isolation.thread.interruptOnFutureCancel=true
hystrix.command.AServerClient#getSleep(Integer).execution.isolation.thread.interruptOnTimeout=true
hystrix.command.AServerClient#getSleep(Integer).execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.command.AServerClient#getSleep(Integer).execution.timeout.enabled=true

AServerClientFallBack

@Component
public class AServerClientFallBack implements AServerClient{@Overridepublic String getName(Integer id) {return null;}@Overridepublic Integer getAmount(Integer id) {return null;}@Overridepublic String getSleep(Integer time) {return "sleep兜底方法";}
}

创建测试接口

@RestController
public class FeignController {@Autowiredprivate AServerClient aServerClient;@GetMapping("/feign/test")public String test(@RequestParam Integer time){return aServerClient.getSleep(time);}
}

访问,模拟熔断

 

相关内容

热门资讯

埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...