当前位置: 首页 > news >正文

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秒之后自动熔断才恢复,符合预期

相关文章:

  • linux常用的基础命令
  • i.MX8MP平台开发分享(RDC软件配置篇)
  • Ubuntu项目部署
  • java aio nio区别
  • 【教程】从0开始搭建大语言模型:实现Attention机制
  • GEO ISP图像调试-PFC(蓝紫边校正)
  • 2024最新最全【大模型】人工智能零基础入门到精通,看完这一篇就够了!
  • DOS 命令
  • android 开机动画执行流程
  • pdf文件怎么改变大小?在线快速压缩pdf的方法
  • vue2 element组件兼容性问题
  • 苹果WWDC大会速览:AI加持全线产品,iOS融入ChatGPT
  • opencv快速安装以及各种查看版本命令
  • 分享4款免费无广告看小说app,喜欢看小说的不要错过!
  • 让指定的电脑软件开机时候自动且来(自启动)的解决方案
  • 【Under-the-hood-ReactJS-Part0】React源码解读
  • 〔开发系列〕一次关于小程序开发的深度总结
  • egg(89)--egg之redis的发布和订阅
  • ERLANG 网工修炼笔记 ---- UDP
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • HomeBrew常规使用教程
  • Java,console输出实时的转向GUI textbox
  • JavaScript新鲜事·第5期
  • Java到底能干嘛?
  • JS实现简单的MVC模式开发小游戏
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • select2 取值 遍历 设置默认值
  • vue数据传递--我有特殊的实现技巧
  • Zsh 开发指南(第十四篇 文件读写)
  • 闭包--闭包作用之保存(一)
  • 对象引论
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 构建工具 - 收藏集 - 掘金
  • 老板让我十分钟上手nx-admin
  • 理解在java “”i=i++;”所发生的事情
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 阿里云服务器如何修改远程端口?
  • ​云纳万物 · 数皆有言|2021 七牛云战略发布会启幕,邀您赴约
  • ‌JavaScript 数据类型转换
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • # 职场生活之道:善于团结
  • #70结构体案例1(导师,学生,成绩)
  • (2)从源码角度聊聊Jetpack Navigator的工作流程
  • (C语言)深入理解指针2之野指针与传值与传址与assert断言
  • (java)关于Thread的挂起和恢复
  • (备忘)Java Map 遍历
  • (翻译)terry crowley: 写给程序员
  • (三) diretfbrc详解
  • (一)RocketMQ初步认识
  • (转)我也是一只IT小小鸟
  • .gitignore文件设置了忽略但不生效
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Framework .NET Core与 .NET 的区别
  • .NET Framework Client Profile - a Subset of the .NET Framework Redistribution