在 HystrixFeign 客户端回退中获取原因的问题
Issue in getting cause in HystrixFeign client fallback
我有 HystrixFeign 客户端,我正试图在我的回退实现中获得 cause/exception,因为我真的很想知道回退的原因,这样我就可以解决服务调用失败的问题.
但是下面的实现并没有让我明白原因。这工作得很好,回退一直被调用。但我不知道为什么。我是 Feign 和 Hystrix 的新手。我的应用程序是在 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;
}
}
}
从 open feign git hub 获取 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 目标方法时对 Fallback 工厂进行类型转换,因为 Fallback class 正在实现您的客户端界面和 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]
我有 HystrixFeign 客户端,我正试图在我的回退实现中获得 cause/exception,因为我真的很想知道回退的原因,这样我就可以解决服务调用失败的问题. 但是下面的实现并没有让我明白原因。这工作得很好,回退一直被调用。但我不知道为什么。我是 Feign 和 Hystrix 的新手。我的应用程序是在 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;
}
}
}
从 open feign git hub 获取 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 目标方法时对 Fallback 工厂进行类型转换,因为 Fallback class 正在实现您的客户端界面和 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]