怎么说Hystrix不会触发Hystrix命令中的一些例外的回退

问题描述 投票:7回答:3

我们通过直接扩展HystrixCommand类来使用Hystrix功能。但是对于一些业务异常,Hystrix的后备方法正在被触发。

我不想为某些特定于业务的异常触发Hystrix回退。如何在没有基于注释的情况下实现它?

java hystrix netflix
3个回答
1
投票

使用ignoreExceptions注释参数

@HystrixCommand(ignoreExceptions = { BaseException.class, MissingServletRequestParameterException.class, TypeMismatchException.class })

https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#error-propagation

我看到你正在扩展HystrixCommand而不是使用注释,但这没关系,只需在命令中设置该属性,它应该具有相同的效果。

不幸的是,Hystrix Command是由Builder模式创建的,所以你必须做一些黑客攻击。 ignoreExceptions被添加到DefaultProperties.java,它在HystrixCommandBuilder中使用


1
投票

如果将逻辑包装在try / catch中并在HystrixBadRequestException中重新抛出任何异常,则它不会触发回退。

@Override
protected Object run() throws Exception {
    try {
        return //call goes here
    }
    catch (Throwable e) {
        //We wrap any exceptions in a HystrixBadRequestException because this way any other errors will not
        //trip the short circuit
        throw new HystrixBadRequestException("Exception thrown hystrix call", e);
    }
}

来自文档:http://netflix.github.io/Hystrix/javadoc/com/netflix/hystrix/exception/HystrixBadRequestException.html

表示提供的参数或状态而不是执行失败的错误的异常。与HystrixCommand抛出的所有其他异常不同,这不会触发回退,不会计入故障指标,因此不会触发断路器。

注意:仅当用户输入错误(例如IllegalArgumentException)时才应该使用此选项,否则会违反容错和回退行为的目的。


1
投票

两种方法可以做到这一点。

  1. 使用HystrixCommand批注并指定异常类型。 @HystrixCommand(ignoreExceptions = { HttpStatusCodeException.class, JsonMappingException.class })
  2. 使用“HystrixBadRequestException”并自定义代码以忽略少数异常情况或状态代码。此实现将检查与您的后端API契约预期的异常的特定错误代码,并且不会调用hystrix回退。抛出“HystrixBadRequestException”不会算作hystrix错误。 @HystrixCommand(commandKey = "MyHystrixCommand", fallbackMethod = "myHystrixFallback", threadPoolKey = "ThreadPoolKey") public ResponseEntity<String> getServiceCallResponse(String serviceUrl, HttpEntity<?> entity) { ResponseEntity<String> resp = null; try { resp = restTemplate.exchange(serviceUrl, HttpMethod.POST, entity, String.class) .getBody(); } catch(Exception e) { handleExceptionForHystrix("getServiceCallResponse", e); } return resp; } private void handleExceptionForHystrix(String function, Exception e) { if (e instanceof HttpStatusCodeException) { HttpStatus httpStatusCode = ((HttpStatusCodeException)e).getStatusCode(); if(httpStatusCode.equals(HttpStatus.BAD_REQUEST) || httpStatusCode.equals(HttpStatus.INTERNAL_SERVER_ERROR)) { throw new HystrixBadRequestException("Hystrix Bad Request Exception Occurred" + httpStatusCode, e); } throw new RuntimeException(function, e); } throw new RuntimeException(function, e); } public ResponseEntity<String> myHystrixFallback(String serviceUrl, HttpEntity<?> entity, Throwable hystrixCommandExp) { return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR); }
© www.soinside.com 2019 - 2024. All rights reserved.