java:测试hystrix的一些关键参数
# 参考资料
https://www.cnblogs.com/zhenbianshu/p/9630167.html
# 示例程序如下:
【pom.xml】
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.12.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>2.3.12.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.2.10.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId><version>2.2.9.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.2.9.RELEASE</version>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>2.2.10.RELEASE</version>
</dependency>
<dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.12</version>
</dependency>
【application.properties】
server.port=8080
spring.application.name=myFeignClientmanagement.server.port=7001
management.endpoints.web.exposure.include=*eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
eureka.client.register-with-eureka=true
eureka.client.serviceUrl.instance.prefer-ip-address=true#feign.client.config.myFeignClient.connectTimeout=10000
#feign.client.config.myFeignClient.readTimeout=20000 # 甚至可以针对一个FeignClient进行配置
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=10000
feign.hystrix.enabled=truehystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=3000
hystrix.command.default.circuitBreaker.requestVolumeThreshold=1
hystrix.command.default.circuitBreaker.errorThresholdPercentage=1
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000logging.level.root=info
logging.level.com.chz.myFeignClient=DEBUG
参数解释:
- 【feign.client.config.default.readTimeout=10000】:因为hystrix的熔断时间设为2000,所以feign的timeout时间设大一点,以免受这个readTimeout的影响
- 【hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000】:feign调用外部接口超过2秒就会发生熔断
- 【hystrix.command.default.metrics.rollingStats.timeInMilliseconds=3000】:hystrix的滑动窗口设为3秒,即发生首次熔断之后3秒内再次调用时自动熔断
- 【hystrix.command.default.circuitBreaker.requestVolumeThreshold=1】:滑动窗口内只要有一个请求就会触发【circuitBreaker.errorThresholdPercentage】参数的运算。生产环境千万不要这样设,只是为了测试。
- 【hystrix.command.default.circuitBreaker.errorThresholdPercentage=1】:滑动窗口内的请求有1%是失败的就会触发自动熔断。生产环境千万不要这样设,只是为了测试。
- 【hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000】:自动熔断时间为5秒钟,即熔断发生5秒后自动熔断才会有机会恢复
【FeignConfiguration.java】
package com.chz.myFeignClient.config;@Configuration
public class FeignConfiguration
{@BeanLogger.Level feignLoggerLevel() {return Logger.Level.FULL;}
}
【Test2Controller.java】
package com.chz.myFeignClient.controller;@Slf4j
@RestController
@RequestMapping("/test2")
public class Test2Controller {@Autowiredprivate Test1FeignClient test1FeignClient;@GetMapping("/test")public String test() {log.info("chz >>> Test2Controller.test()");String result = test1FeignClient.hello();log.info("chz >>> Test2Controller.test(): {}", result);return result;}
}
【Test1FeignClient.java】
package com.chz.myFeignClient.feign;@FeignClient(contextId = "com.chz.myFeignClient.feign.Test1FeignClient",name = "myEurekaClient1",path = "/test",fallbackFactory = Test1FeignClientFallback.class
)
public interface Test1FeignClient
{@GetMapping(value = "/hello")String hello();
}@Slf4j
@Component
class Test1FeignClientFallback implements FallbackFactory<Test1FeignClient>
{@Overridepublic Test1FeignClient create(Throwable cause) {return new Test1FeignClient() {@Overridepublic String hello() {log.error("chz >>> Test1FeignClientFallback.hello() -> fallback", cause);return "fallback hello";}};}
}
说明:远程调用的接口【http://myEurekaClient1/test/hello】有50%左右的机会会休眠3000秒,关键代码如下:
@GetMapping("/hello")
public String hello() {log.info("chz >>> TestController.hello()");try {if( Math.abs(ThreadLocalRandom.current().nextInt() % 2)==0 ) {log.info("chz >>> TestController.hello(), sleep");Thread.sleep(3000L);}} catch (InterruptedException e) {log.info("interrupt", e);}log.info("chz >>> TestController.hello(), result");return "Hello: " + serverPort;
}
【FeignRequestInterceptor.java】
package com.chz.myFeignClient.interceptor;@Slf4j
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor
{@Overridepublic void apply(RequestTemplate template){// 这里给每一个FeignClient的调用增加一个headerlog.info("FeignRequestInterceptor::apply: tid:{}", Thread.currentThread().getId());template.header("chz-new-header", "just-for-test");}
}
【MyFeignClientTest.java】
package com.chz.myFeignClient;@EnableHystrix
@EnableFeignClients
@SpringBootApplication
public class MyFeignClientTest
{public static void main(String[] args) {SpringApplication.run(MyFeignClientTest.class, args);}
}
运行【MyFeignClientTest】,然后不断访问【http://localhost:8080/test2/test】,查看日志
从截图可以看出超过了5秒之后自动熔断才恢复,符合预期