【问题标题】:Is it possible to selectively ignore service exception in Hystrix?是否可以选择性地忽略 Hystrix 中的服务异常?
【发布时间】:2018-05-22 08:03:22
【问题描述】:

我有客户端 API jar,它在内部进行外部调用并在出现问题时抛出单个通用服务异常。我已经在 API 调用上编写了 hystrix 包装器。有类似“用户未找到”返回异常的情况。尽管调用成功并且服务响应了有效响应,但 hystrix 将其视为失败。我知道我们可以忽略 Hystrix 中的异常;但它会将服务调用引发的唯一异常列入白名单。有没有办法根据异常消息或http状态码或其他东西选择性地忽略服务调用引发的异常?

【问题讨论】:

    标签: hystrix


    【解决方案1】:

    如果外部服务在不同的情况下抛出不同的异常,那么你可能可以像这样忽略这些异常

     @HystrixCommand(ignoreExceptions = {SomeException.class})
    

    但是,如果您必须根据错误消息忽略异常,那么解决此问题的最佳方法是在您的外部调用周围放置一个 try catch。并在 catch 块中检查它是否是需要忽略的异常之一。如果是这样,不要做任何事情。如果不重新抛出此异常。这样的事情会做。更多关于HystrixBadRequestException的信息

    @HystrixCommand(fallbackMethod = "fallBackMethod", groupKey = "CircuitBreaker", commandKey = "somekey", threadPoolKey = "somekey",
            commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000"),
                @HystrixProperty(name = "execution.timeout.enabled", value = "false"),
                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
                @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "1200000"),
            },
            threadPoolProperties = {
                @HystrixProperty(name = "coreSize", value = "30"),
                @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")
            })
        public void someMethod(....){
                 try {
                   // Call external service
                 } catch(Exception e) {
                   if(exception to be ignored)
                     throw new HystrixBadRequestException("Some message", e);
                   else
                      throw e
                 } 
        }
    

    【讨论】:

    猜你喜欢
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2022-06-12
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多