【问题标题】:Hystrix and Custom ExceptionHystrix 和自定义异常
【发布时间】:2016-10-31 09:12:25
【问题描述】:

我有一个如下所示的服务层接口:

public interface MyService {
    void save(DomainClass domainObject) throws MyServiceException;
}

我使用Hystrix保护实现方法:

public class MyServiceImpl implements MyService {
    @HystrixCommand
    public void save(DomainClass domainObject) throws MyServiceException {
        remoteServiceClient.persist(domainObject);
    }
}

remoteServiceClient 失败或超时时,Hystrix 抛出HystrixRuntimeException。但我不希望服务的客户看到任何与 Hystrix 相关的异常(他们应该忘记服务的实现细节,对吧?)。我想抛出我的MyServiceException 检查异常。有可能这样做吗?我应该以不同的方式构建我的实现吗?

【问题讨论】:

    标签: java exception hystrix


    【解决方案1】:

    有人在这里问过类似的问题:Get failure exception in @HystrixCommand fallback method

    所以你的案例可以实现为:

    public class MyServiceImpl implements MyService {
    
        @HystrixCommand(fallbackMethod = "saveFallback")
        public void save(DomainClass domainObject) throws MyServiceException {
            remoteServiceClient.persist(domainObject);
        }
    
        @HystrixCommand
        void saveFallback(Throwable e) {
            throw new MyServiceException();
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用 hystrix-javanica 1.5.7

      在此对错误传播进行了更大程度的微调,异常默认不包装为 HystrixRuntimeException。

      更新了错误传播:

      如果你已经实现了回退方法并且即使失败了,那么 fallback 方法抛出的异常会传播给用户

      //FallbackException and CommandException are user defined
      @HystrixCommand(fallbackMethod = "fall")
      public String getInfo(boolean fail) {
          throw new CommandException("Command failed....falling back");       
      }
      
      public String fall(boolean fail) {  
             throw new FallbackException("Fallback failed....");      
      }
      

      在这种情况下,FallbackException 将被抛出。

      【讨论】:

        【解决方案3】:

        Dharmvir,这不是我所看到的。以下代码摘录始终将任何错误包含在 HystrixRuntimeException 中。我不得不询问该对象并从后备中检索实际错误,如下所示。我不得不解决这个问题的方式并不适合我:

        @Override
        @HystrixCommand(
                commandKey = "gateway",
                threadPoolKey = "gateway",
                fallbackMethod = "fallback")
        public void process(final TxnLog mTxn, final Item item) {
        
        ...force timeout
        }
        

            public void fallback(final TxnLog mTxn, final Item item, final Throwable cause) {
        
            ...other code
        
                if (cause != null && cause instanceof HystrixTimeoutException) {
        
                   throw new InHouseException(ErrorInfoFactory.externalGatewayTimeout("External system communication timeout"));
                }
            }
        

            } catch (HystrixRuntimeException e) {
        
                if (e.getFallbackException().getCause() instanceof InHouseException) {
        
                    InHouseException c = ((InHouseException) e.getFallbackException().getCause());
        
                    ...return actual error
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-08
          • 2021-01-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-03
          • 2016-07-27
          相关资源
          最近更新 更多