本文共 12718 字,大约阅读时间需要 42 分钟。
查看请求链路的状态耗时等我们可以使用日志的方式
还是使用service-consumer-feign-7100模块测试,先在resources下增加一个logback.xml,这里不对日志的框架进行赘述
/** * NONE 不记录任何信息,默认 * BASIC 记录请求方法,请求URL,状态码和用时 * HEADERS 在BASIC基础上在记录一些常用信息 * FULL 记录请求和相应的所有信息 * @date: 2021-05-07 14:41 * @author Oliver **/ @Bean public Logger.Level getLogger(){ return Logger.Level.FULL; }
重启服务后随便访问个请求,控制台会打印:
2021-05-07 14:49:01.660 DEBUG 15168 --- [ionManagerTimer] h.i.c.PoolingHttpClientConnectionManager : Closing expired connections2021-05-07 14:49:02.735 DEBUG 15168 --- [nio-7100-exec-7] o.s.web.servlet.DispatcherServlet : GET "/order/pojo?id=1&name=name", parameters={masked}2021-05-07 14:49:02.736 DEBUG 15168 --- [nio-7100-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to consumer.controller.OrderController#pojo(Product)2021-05-07 14:49:02.736 DEBUG 15168 --- [nio-7100-exec-7] o.s.c.openfeign.support.SpringEncoder : Writing [Product(id=1, name=name, num=null, price=null)] using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@39a03ccc]2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] ---> GET http://service-provider/provider/pojo HTTP/1.12021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] Content-Length: 462021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] Content-Type: application/json2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] 2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] {"id":1,"name":"name","num":null,"price":null}2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] ---> END HTTP (46-byte body)2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] c.n.loadbalancer.ZoneAwareLoadBalancer : Zone aware logic disabled or there is only one zone2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] c.n.loadbalancer.LoadBalancerContext : service-provider using LB returned Server: 192.168.3.166:7073 for request http:///provider/pojo2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] o.a.h.client.protocol.RequestAuthCache : Auth cache not set in the context2021-05-07 14:49:02.737 DEBUG 15168 --- [nio-7100-exec-7] h.i.c.PoolingHttpClientConnectionManager : Connection request: [route: {}->http://192.168.3.166:7073][total available: 0; route allocated: 0 of 50; total allocated: 0 of 200]2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] h.i.c.PoolingHttpClientConnectionManager : Connection leased: [id: 2][route: {}->http://192.168.3.166:7073][total available: 0; route allocated: 1 of 50; total allocated: 1 of 200]2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] o.a.http.impl.execchain.MainClientExec : Opening connection {}->http://192.168.3.166:70732021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] .i.c.DefaultHttpClientConnectionOperator : Connecting to /192.168.3.166:70732021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] .i.c.DefaultHttpClientConnectionOperator : Connection established 192.168.3.166:10072<->192.168.3.166:70732021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-2: set socket timeout to 10002021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] o.a.http.impl.execchain.MainClientExec : Executing request GET /provider/pojo HTTP/1.12021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] o.a.http.impl.execchain.MainClientExec : Target auth state: UNCHALLENGED2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] o.a.http.impl.execchain.MainClientExec : Proxy auth state: UNCHALLENGED2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> GET /provider/pojo HTTP/1.12021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> Content-Type: application/json2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> Accept: */*2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> Content-Length: 462021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> Host: 192.168.3.166:70732021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> Connection: Keep-Alive2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 >> User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.11)2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "GET /provider/pojo HTTP/1.1[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "Content-Type: application/json[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "Accept: */*[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "Content-Length: 46[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "Host: 192.168.3.166:7073[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "Connection: Keep-Alive[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.11)[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "[\r][\n]"2021-05-07 14:49:02.738 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 >> "{"id":1,"name":"name","num":null,"price":null}"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "HTTP/1.1 200 [\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "Content-Type: application/json[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "Transfer-Encoding: chunked[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "Date: Fri, 07 May 2021 06:49:02 GMT[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "Keep-Alive: timeout=60[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "Connection: keep-alive[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "2e[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "{"id":1,"name":"name","num":null,"price":null}[\r][\n]"2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 << HTTP/1.1 200 2021-05-07 14:49:02.741 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 << Content-Type: application/json2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 << Transfer-Encoding: chunked2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 << Date: Fri, 07 May 2021 06:49:02 GMT2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 << Keep-Alive: timeout=602021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.headers : http-outgoing-2 << Connection: keep-alive2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] o.a.http.impl.execchain.MainClientExec : Connection can be kept alive for 60000 MILLISECONDS2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] <--- HTTP/1.1 200 (4ms)2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] connection: keep-alive2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] content-type: application/json2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] date: Fri, 07 May 2021 06:49:02 GMT2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] keep-alive: timeout=602021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] transfer-encoding: chunked2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] 2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "0[\r][\n]"2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] org.apache.http.wire : http-outgoing-2 << "[\r][\n]"2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] h.i.c.PoolingHttpClientConnectionManager : Connection [id: 2][route: {}->http://192.168.3.166:7073] can be kept alive for 60.0 seconds2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-2: set socket timeout to 02021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 2][route: {}->http://192.168.3.166:7073][total available: 1; route allocated: 1 of 50; total allocated: 1 of 200]2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] {"id":1,"name":"name","num":null,"price":null}2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] consumer.service.ProductService : [ProductService#selectProductByPojo] <--- END HTTP (46-byte body)2021-05-07 14:49:02.742 DEBUG 15168 --- [nio-7100-exec-7] o.s.w.c.HttpMessageConverterExtractor : Reading to [consumer.entity.Product]2021-05-07 14:49:02.743 DEBUG 15168 --- [nio-7100-exec-7] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]2021-05-07 14:49:02.743 DEBUG 15168 --- [nio-7100-exec-7] m.m.a.RequestResponseBodyMethodProcessor : Writing [Product(id=1, name=name, num=null, price=null)]2021-05-07 14:49:02.744 DEBUG 15168 --- [nio-7100-exec-7] o.s.web.servlet.DispatcherServlet : Completed 200 OK
上面一些关键信息比如,连接的地址,连接的时间,使用的协议信息
>>这种符号代表的是请求,<<这种代表的是响应
feign: httpclient: enabled: true #开启httpclient client: config: service-provider: #需要开启的服务名 loggerLevel: FULL
以后也不会用这种方式,这里仅做记录
Feign负载均衡底层使用的是Ribbon,所以其请求超时的配置实际上是Ribbon的配置
分布式环境中,请求压力比较大的情况下,处理服务的过程需要花费一定时间,而默认的超时时间是一秒,超过后会报超时异常,所以一般需要重新配置
本次的测试URL:
ribbon: ConnectTimeout: 5000 #请求连接超时时间,单位毫秒,默认是1秒 ReadTimeout: 5000 #请求处理的超时时间
service-provider: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule OkToRetryOnAllOperations: true #对所有请求都进行重试 MaxAutoRetries: 2 #对当前实例的重试次数 MaxAutoRetriesNextServer: 0 #切换实例的重试次数 ConnectTimeOut: 3000 #请求连接的超时时间 ReadTimeout: 3000 #请求处理的超时时间
之后对超时会有其他更好的解决方式
转载地址:http://agoqz.baihongyu.com/