work hard work smart

专注于Java后端开发。 不断总结,举一反三。
随笔 - 1158, 文章 - 0, 评论 - 153, 阅读 - 186万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

服务熔断

Posted on   work hard work smart  阅读(195)  评论(0编辑  收藏  举报

@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>
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示