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)