0
点赞
收藏
分享

微信扫一扫

Hystrix应用场景

三次方 2021-09-28 阅读 143

最近项目用4.0的开发框架,熔断这块将是重点,在这里简单介绍下熔断。

认识Hystrix

为什么需要Hystrix?

出现不可用,但是其他依赖仍然可用.

Hystrix特性

  • 快照时间窗:断路器确定是否打开需要统计一些请求和错误数据,而统计的时间范围就是快照时间窗,默认为最近的10秒。
  • 请求总数下限:在快照时间窗内,必须满足请求总数下限才有资格根据熔断。默认为20,意味着在10秒内,如果该hystrix命令的调用此时不足20次,即时所有的请求都超时或其他原因失败,断路器都不会打开。
  • 错误百分比下限:当请求总数在快照时间窗内超过了下限,比如发生了30次调用,如果在这30次调用中,有16次发生了超时异常,也就是超过50%的错误百分比,在默认设定50%下限情况下,这时候就会将断路器打开。
对比项 线程池隔离 信号量隔离
线程 与调用线程非相同线程 与调用线程相同(jetty线程)
异步 排队、调度、上下文开销等 无线程切换,开销低
并发支持 支持 支持(最大线程池大小) 支持(最大信号量上限)

如何使用Hystrix?

监控环境搭建
<parent>
    <groupId>com.belle.blf</groupId>
    <artifactId>blf-tools</artifactId>
    <version>4.0.1-SNAPSHOT</version>
</parent>

<artifactId>blf-hystrix-center</artifactId>
<name>blf::tools::hystrix::center</name>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
    </dependency>
</dependencies>
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }
}
改造要监控的服务
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
模拟调用单据编码熔断
@ApiOperation(value="测试获取系统单据编号", notes="")
    @GetMapping("/code_test.json")
    @HystrixCommand(fallbackMethod = "fallback")
    public String getTestSheeIdCode(){
        int randomInt= random.nextInt(10) ;
        if(randomInt< 8){   //模拟调用失败情况
            throw new RuntimeException("call dependency service fail.");
        }
        return systemCodeFeignApi.getSystemCode("1001");
    }

    public String fallback(){
        return "some exception occur call fallback method.";
    }

[图片上传失败...(image-5768cd-1527554854790)]

Hystrix-feign

@FeignClient(url = "http://localhost:10031",name = Applications.FEIGN_BLF_ITG_API,fallback = SystemCodeHystrixClient.class)
public interface SystemCodeFeignApi {
    
    /**
     * 根据字段获取单据编号
     * @param sysCodeId
     * @param
     * @return
     */
    @RequestMapping(value = "/blf-itg-api/itg/api/sysCodeRule/getSheetIdCode.json", method = RequestMethod.GET)
    public String getSystemCode(@RequestParam(value = "sysCodeId") String sysCodeId);

}
@Component("systemCodeHystrixClient")
public class SystemCodeHystrixClient implements SystemCodeFeignApi {
    private static final Logger LOGGER = LoggerFactory.getLogger(SystemCodeHystrixClient.class);


    @Override
    public String getSystemCode(String sysCodeId) {
        return "-1";
    }
@ApiOperation(value="测试获取系统单据编号", notes="")
@GetMapping("/feign_code_test.json")
public String getTestFeignSheeIdCode(){
    return systemCodeFeignApi.getSystemCode("1001");
}
@ApiOperation(value="获取系统单据编号", notes="")
@GetMapping("/getSheetIdCode.json")
public String getSystemCode(@RequestParam(value = "sysCodeId") String sysCodeId){
    try {
        System.out.println("开始休眠");
        //故意设置让客户端等待超时
        Thread.sleep((long)50000);
        System.out.println("休眠结束");
    }catch (Exception e){

    }
    String sysCode="";
    if(StringUtils.isEmpty(sysCodeId)){
        sysCode="-1";
    }
    sysCode=service.doSysCodeByID(sysCodeId);
    return sysCode;

 }
hystrix:
  threadpool:
    # default: 默认参数,作用的所有的hystrix的客户端
    default:
      coreSize: 10
  command:
    default:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds:  60000

turbine:
  cluster-name-expression: "default"
  combine-host-port: true

#默认关闭
feign:
  hystrix:
    enabled: true

###Ribbon的超时            
ribbon:
  ReadTimeout:  60000
  ConnectTimeout: 60000
  
从Spring Cloud Edgware开始,Feign支持使用属性配置超时:
feign:
    client:config:
        feignName:
            connectTimeout:5000
            readTimeout:5000
举报

相关推荐

redis应用场景

ThreadLocal应用场景

activemq应用场景

MongoDB应用场景

xhack应用场景

vmtools应用场景

0 条评论