默认错误日志信息
com.netflix.hystrix.exception.HystrixRuntimeException: MsFeignClient#pageQueryInvoice(MsInvoiceQueryRequest) timed-out and no fallback available.
熔断日志
{code=-2, message=服务器繁忙,请稍后再试}
配置后日志信息
- 请求参数
- 请求结果
- 错误信息
2020-06-12 09:11:41,806 ERROR (MeetyouKeepErrMsgConfiguration.java:42)- feign调用失败:请求url:http://192.168.1.XXX:8080/ms/invoice/list, 返回结果:{“code”:400,“message”:“用户编号不能为空”},请求参数: {“userCode”:null,“status”:0,“pageNum”:1,“pageSize”:100}
配置方式
FeignClient增加configuration配置
@FeignClient(value = "ms",
path = "/ms",
contextId = "members",
configuration = {MeetyouKeepErrMsgConfiguration.class},//微服务Feign调用日志配置类
fallback = MsFeignClientFallBack.class
)
public interface MsFeignClient {
}
日志配置类
@Slf4j
public class MeetyouKeepErrMsgConfiguration {
@Bean
public ErrorDecoder errorDecoder() {
return new UserErrorDecoder();
}
/**
* 自定义错误
*/
public class UserErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
Exception exception = null;
try {
// 获取原始的返回内容
String json = Util.toString(response.body().asReader());
log.info("request url: {}, request body: {}",
response.request().url() , JSONObject.toJSONString(response.request().requestBody().asString()));
exception = new RuntimeException(json);
// 将返回内容反序列化为Result,这里应根据自身项目作修改
R.Result result = JSONObject.parseObject(json, R.Result.class);
// 业务异常抛出简单的 RuntimeException,保留原来错误信息
if (result.getCode() != 0) {
log.error("feign调用失败:请求url:{}, 返回结果:{},请求参数: {}", response.request().url() , json, response.request().requestBody().asString());
exception = new RuntimeException(result.getMessage());
}
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
return exception;
}
}
}
```