一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

spring cloud hystrix超时时间使用方式代码解析

时间:2021-01-04 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下spring cloud hystrix超时时间使用方式代码解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

我们在使用后台微服务的时候,各个服务之前会有很多请求和交叉业务。这里会引起雪崩、超时等异常处理。SpringCloud Hystrix服务降级、容错机治理使 hystrix 有很好的支持,引入后实现断路器功能。

1:pom引入jar包


        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix
      

2:添加注解

Application中增加 @EnableCircuitBreaker 开启hystrix功能

3:配置文件配置

注意:feign中的hystrix的enabled属性要设置true

hystrix:
 command:
  transferApprove: # 这里是要设置超时时间的方法,如新增其他方法需要增加此节点信息。
   execution:
    isolation:
     thread:
      timeoutInMilliseconds: 6000 #默认连接超时时间是1秒

4:增加代码

代码注意fastOrBatchFallback的出参和入参要和设置了@HystrixCommand注解方法一致,否则会抛出异常。

 @Override
  @HystrixCommand(fallbackMethod = "fastOrBatchFallback")
  public ResultModel transferApprove(TransferApproveDto dto) {
    log.info("调动流程审批:{}", dto);
    if (StringUtils.isEmpty(dto.getOperatorId())
        || StringUtils.isEmpty(dto.getFlowNos())
        || StringUtils.isEmpty(dto.getOperatorId())) {
      return ResultModel.fail(-1, "参数异常");
    }
  }
 
 
  public ResultModel fastOrBatchFallback(TransferApproveDto transferApproveDto) {
    log.info("请求ps服务超时,请稍后再试.入参:{}", GsonUtils.toJsonString(transferApproveDto));
    return ResultModel.fail("请求服务超时,请稍后再试", "ps服务超时,请稍后再试");
  }

热门栏目