Is it possible to selectively ignore service exception in Hystrix?

微笑、不失礼 提交于 2019-12-24 21:59:03

问题


I have client API jar which internally makes external calls and throws single generic Service exception in case of issues. I have written hystrix wrapper over the API calls. There are cases like "user not found" returning exception. Though the call was successful and service responded with valid response, the hystrix is treating it as a failure. I know that we can ignore the exception in Hystrix; but it will whitelist the only exception thrown by service calls. Is there a way to selectively ignore exception thrown by the service calls based on message in exception or http status code or something?


回答1:


If the external service throw different exceptions in different cases, then you can probably ignore those exceptions like this

 @HystrixCommand(ignoreExceptions = {SomeException.class})

But if you have to ignore exceptions bases on error message then the best way to tackle this is put a try catch around your external call. And in the catch block check if it is one of those exceptions which needs to be ignored. If so don't do anything. If not rethrow this exception. Something like this will do. More info about 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
             } 
    }


来源:https://stackoverflow.com/questions/47708195/is-it-possible-to-selectively-ignore-service-exception-in-hystrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!