SpringCloud学习记录 | 第七篇:OpenFeign超时控制和日志打印

OpenFeign超时

消费服务去调用提供和服务由于网络等等各种因素肯定会出现超时的情况,OpenFeign的Ribbon默认超时1秒。

1.我们在提供服务的地方设置超时,时间为3秒:

    @GetMapping("/payment/timeout")
    public String paymentTimeOut(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return servicePort;
    }

2.在客户端调用:

    @GetMapping("/consumer/payment/timeout")
    public String paymentTimeOut(){
        return paymentService.paymentTimeOut();
    }

3.看客户端调用返回结果:

4.在客户端设置OpenFeign的超时时间:

ribbon:
  ReadTimeout: 5000 #设置超时时间为5秒,默认为1秒
  ConnectTimeout: 5000

 

OpenFeign日志打印

1.OpenFeign的日志级别分为以下四个级别:

NONE:默认的,不显示任何日志。

BASIC:仅仅记录请求方法、url、响应状态码、执行时间

HEADERS:除了BASIC信息还添加了请求和响应的头信息

PULL:除了HEADERS信息还添加了请求和响应的正文和元信息

2.配置日志级别:

@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

3.在application.yml中声明需要输出日志的接口:

#配置OpenFeign日志级别
logging:
  level:
    com.tlh.springcloud.service.IPaymentService: debug

4.输出日志信息:

2020-07-22 20:57:22.766 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] ---> GET http://CLOUD-PAYMENT-SERVICE/payment/timeout HTTP/1.1
2020-07-22 20:57:22.766 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] ---> END HTTP (0-byte body)
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] <--- HTTP/1.1 200 (3016ms)
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] connection: keep-alive
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] content-length: 4
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] content-type: text/plain;charset=UTF-8
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] date: Wed, 22 Jul 2020 12:57:25 GMT
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] keep-alive: timeout=60
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] 
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] 8001
2020-07-22 20:57:25.783 DEBUG 6988 --- [p-nio-80-exec-3] c.t.springcloud.service.IPaymentService  : [IPaymentService#paymentTimeOut] <--- END HTTP (4-byte body)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值