@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //最小请求数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //10秒
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60") //滚动时间10秒钟,有7次发生错误。断路器被设置为打开状态。
一、HystrixController .java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | @RestController @DefaultProperties (defaultFallback = "defaultFallback" ) public class HystrixController { //@HystrixCommand(fallbackMethod = "fallback") //2、超时设置 /*@HystrixCommand(commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //超时时间设置为3秒 })*/ //3. @HystrixCommand (commandProperties = { @HystrixProperty (name = "circuitBreaker.enabled" , value = "true" ), //设置熔断 @HystrixProperty (name = "circuitBreaker.requestVolumeThreshold" , value = "10" ), @HystrixProperty (name = "circuitBreaker.sleepWindowInMilliseconds" , value = "10000" ), @HystrixProperty (name = "circuitBreaker.errorThresholdPercentage" , value = "60" ) }) @GetMapping ( "/getProductInfoList" ) public String getProductInfoList( @RequestParam ( "number" ) Integer number){ if (number % 2 == 0 ){ return "success" ; } RestTemplate restTemplate = new RestTemplate(); return restTemplate.postForObject( "http://127.0.0.1:8091/product/listForOrder" , Arrays.asList( "157875196366160022" ),String. class ); } private String fallback(){ return "太拥挤了,请稍后再试~~" ; } private String defaultFallback(){ return "默认提示:太拥挤了,请稍后再试~~" ; } } |
Product工程中的方法
1 2 3 4 5 6 7 8 9 | @PostMapping ( "/listForOrder" ) public List<ProductInfo> listForOrder( @RequestBody List<String> productIdList){ try { Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace(); } return productService.findList(productIdList); } |
调用:
number为2时成功返回
number为1时,触发熔断
熔断:
不停的调用 http://localhost:8081/getProductInfoList?number=1 。
然后调用http://localhost:8081/getProductInfoList?number=2, 也出现拥挤提示
然后再次调用http://localhost:8081/getProductInfoList?number=2 就正常了。
二、使用配置,设置过期时间
设置超时时间为1秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | spring: application: name: order redis: host: 47.48 . 47.40 port: 6379 cloud: config: discovery: enabled: true service-id: CONFIG profile: test hystrix: command: default : execution: isolation: thread: timeoutInMilliseconds: 1000 |
接口配置
1 2 3 4 5 6 7 8 9 10 | @HystrixCommand @GetMapping ( "/getProductInfoList" ) public String getProductInfoList( @RequestParam ( "number" ) Integer number){ if (number % 2 == 0 ){ return "success" ; } RestTemplate restTemplate = new RestTemplate(); return restTemplate.postForObject( "http://127.0.0.1:8091/product/listForOrder" , Arrays.asList( "157875196366160022" ),String. class ); } |
三、为单个方法配置超时时间
1 | 给getProductInfoList设置超时时间为 3 秒<br><br> |
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!
分类:
S.Spring Cloud
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现