✨✨个人主页:沫洺的主页
📚📚系列专栏: 📖 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添加依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <!--<version>2.2.1.RELEASE</version>--> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.moming</groupId> <artifactId>spring-cloud-root</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>spring-cloud-hystrix</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <!--<version>2.2.1.RELEASE</version>--> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.moming</groupId> <artifactId>user-api</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
相关配置信息
#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规则为:<FeignClientName>#<methodName>(<arg1ClassName>,<arg2ClassName>...) //AServerClient#getSleep(Integer) //public String getSleep(Integer time) ; }
全局熔断配置同上
对回退兜底类的某个方法设置熔断配置
配置key规则为:
<FeignClientName>#<methodName>(<arg1ClassName>,<arg2ClassName>...)
#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{ @Override public String getName(Integer id) { return null; } @Override public Integer getAmount(Integer id) { return null; } @Override public String getSleep(Integer time) { return "sleep兜底方法"; } }
创建测试接口
@RestController public class FeignController { @Autowired private AServerClient aServerClient; @GetMapping("/feign/test") public String test(@RequestParam Integer time){ return aServerClient.getSleep(time); } }
访问,模拟熔断