2017-01-06 8 views
1

は、これは私が装うとhystrixでmicroservicesアーキテクチャを持ってNetflix Feign - Propagate Status and Exception through Microservices装う伝播するエラー状態

に似質問です。 私の問題は、アンダーリングサービスがエラー応答、たとえばステータス404を返すとき、私のFallbackFactoryはそれを伝播するはずですが、常に200を返します。ここで

私のコードです:

ErrorResponseDecoder:

@Component 
public class ErrorResponseDecoder implements ErrorDecoder { 

private ErrorDecoder delegate = new ErrorDecoder.Default(); 

@Override 
public Exception decode(String methodKey, Response response) { 
    HttpHeaders responseHeaders = new HttpHeaders(); 
    for(Map.Entry<String,Collection<String>> entry : response.headers().entrySet()) { 
     responseHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue())); 
    } 
    HttpStatus statusCode = HttpStatus.valueOf(response.status()); 
    String statusText = response.reason(); 

    byte[] responseBody; 
    try { 
     responseBody = IOUtils.toByteArray(response.body().asInputStream()); 
    } catch (IOException e) { 
     throw new RuntimeException("Failed to process response body.", e); 
    } 

    if (response.status() >= 400 && response.status() <= 499) { 
     return new HttpClientErrorException(statusCode, statusText, responseHeaders, responseBody, null); 
    } 

    if (response.status() >= 500 && response.status() <= 599) { 
     return new HttpServerErrorException(statusCode, statusText, responseHeaders, responseBody, null); 
    } 
    return delegate.decode(methodKey, response); 
} 
} 

フォールバック:

@Component 
public class ServiceFallbackFactory implements FallbackFactory<ServiceFeign> { 

@Override 
public ServiceFeign create(final Throwable cause) { 

    return new ServiceFeign() { 

     @Override 
     public Response getList(String date) { 
      if(cause instanceof HttpStatusCodeException) { 
       HttpStatusCodeException exc = (HttpStatusCodeException)cause; 
       return Response.status(exc.getStatusCode().value()).entity(exc.getMessage()).build(); 
      } else { 
       throw new RuntimeException(cause); 
      } 
     } 
    } 
} 

出力:

{ 
    "statusType": "NOT_FOUND", 
    "entity": "404 Not Found", 
    "entityType": "java.lang.String", 
    "metadata": {}, 
    "status": 404 
} 

HTTPステータス:200 :( どうすれば404にもできますか?

答えて

0

まず第一に、私はそのためのフォールバックのメカニズムは必要ありませんでした。

ほとんどの場合、ResponseEntityが動作しますが、私はこの解決策を試しませんでした。

Hystrix - how to register ExceptionMapper