2016-09-07 6 views
1

HystrixFeign 클라이언트가 있는데 대체물에 대한 이유를 알고 싶기 때문에 fallback 구현에서 원인/예외를 얻으려고합니다. 그 이유를 해결할 수있는 이유는 무엇입니까? 서비스 호출에 실패했습니다. 그러나 아래 구현은 원인을 얻지 못합니다. 이것은 정상적으로 작동하며 폴백은 항상 호출됩니다. 그러나 나는 단서가 없다. 나는 Feign과 Hystrix에 처음 온 사람입니다. 내 응용 프로그램은 Java 1.6 년 전으로 작성되었으며 이것은 향상된 호출의 일종입니다. 그래서 나는 어떤 람다 식으로도 갈 수 없다.HystrixFeign 클라이언트 대체에서 원인을 얻는 데 문제가 발생했습니다.

나는

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; 
     } 

    } 

} 

답변

0

아래 하나 하나 도움이 수정 개방 체하다 자식 허브에서 retroApi 예를 들어, 테스트 케이스 코드를 촬영하고 시작처럼 아래에 다음과 같이 정의 클라이언트 인터페이스가 문제를 해결합니다. 아래는 작업 코드입니다. 폴백 클래스는 클라이언트 인터페이스와 FallbackFactory 모두 구현되어 있기 때문에

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 대상 메소드를 호출하면서 대체 공장을 던져 입력해야합니다.

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]

의 대화를 추적 할 수 있습니다