温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何正确的使用spring cloud hystrix

发布时间:2021-01-05 15:06:12 来源:亿速云 阅读:144 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关如何正确的使用spring cloud hystrix,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1:pom 引入jar包

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
      </dependency>

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服务超时,请稍后再试");
  }

看完上述内容,你们对如何正确的使用spring cloud hystrix有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI