Fegin调用错误日志信息

默认错误日志信息

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

```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值