问题在HystrixFeign客户后备越来越原因(Issue in getting cause in

2019-09-26 08:29发布

我有HystrixFeign客户,我试图让事业/异常在我的候补执行,因为我真的想知道回退的原因,所以我可以解决,为什么服务呼叫失败的问题。 但是,下面的实现并不让我的事业。 这只是正常和备用获取调用所有的时间。 但我不知道为什么。 我是新来假装和豪猪。 我的应用程序是用Java 1.6的。几年前,这是一种增强的调用。 所以我不能去任何lambda表达式。

我已经定义像下面的客户端界面

public interface MyServiceFeignClient {
      @RequestLine("POST /myService/order")
      @Headers("Content-Type:application/vnd.org.company.domain.order+json;version=1.0")
      ServiceResponse sendOrder(String content);
}

我FeignClientFacory就像下面

public class FeignClientFactory {

    private static final Logger LOG = LoggerFactory.getLogger(FeignClientFactory.class);

    private String serviceUrl;

    public FeignClientFactory(final String serviceUrl) {
        this.serviceUrl = serviceUrl;
    }

    public MyServiceFeignClient newInstance() {
        return HystrixFeign.builder()
            .decoder(new GsonDecoder())
            .target(MyServiceFeignClient.class, serviceUrl);

    }

    class ClientFallbackFactory implements MyServiceFeignClient, FallbackFactory<ClientFallbackFactory> {

        final Throwable cause;

        public ClientFallbackFactory() {
            this(null);
        }

        ClientFallbackFactory(Throwable cause) {
            this.cause = cause;
        }
        // note that this method is not getting called at all
        @Override
        public ClientFallbackFactory create(Throwable cause) {
            if (cause != null) {
                String errMessage = StringUtils.isNotBlank(cause.getMessage()) ? cause.getMessage() : "unknown error occured";
                LOG.debug("Client fallback called for the cause : {}", errMessage);
            }
            return new ClientFallbackFactory(cause);

        }
        // everytime this method is called as fallback and the cause is just null
        @Override
        public ServiceResponse sendOrder(String content) {
            LOG.debug("service client api fallback called");
            ServiceResponse response = new ServiceResponse();
            String errMessage = (cause == null ? "service client api fallback called" : cause.getMessage());
            response.setErrorMessage(errMessage);
            response.setResultStatus("WARN");
            return response;
        }

    }

}

Answer 1:

以从开假死git的枢纽retroApi例如测试用例的代码,并开始一个修改一个帮我解决问题。 下面是工作的代码。

public static class ClientFallbackFactory implements MyServiceFeignClient, FallbackFactory<ClientFallbackFactory> {

    @Override
    public ClientFallbackFactory create(Throwable cause) {
        return new PRSClientFallback(cause);
    }

    final Throwable cause; // nullable

    public ClientFallbackFactory() {
        this(null);
    }

    ClientFallbackFactory(Throwable cause) {
        this.cause = cause;
    }

    @Override
    public PaymentRiskServiceResponse sendOrder(String content) {
        String errorMessage = (cause == null) ? "No cause returned" : cause.getMessage();
        LOG.debug("Client fallback called : {} ", errorMessage);
        MyServiceResponse response = new MyServiceResponse();
        response.setResultStatus("WARN");
        response.setErrorMessage("Client fallback called");
        return response;
    }
}

确保类型转换的后备工厂,同时调用HystrixFeign目标的方法,因为后备类是实现两个客户端接口和FallbackFactory。

return HystrixFeign.builder()
        .encoder(new JacksonEncoder(mapper))
        .decoder(new GsonDecoder())
        .target(MyServiceFeignClient.class, prsUrl, (FallbackFactory<ClientFallbackFactory>) new ClientFallbackFactory());

您可以跟踪这个问题的对话[ https://github.com/OpenFeign/feign/issues/458]



文章来源: Issue in getting cause in HystrixFeign client fallback