简体   繁体   English

Feign Client:如何记录请求发送到的服务器名称?

[英]Feign Client: How to log the server name which the request was sent to?

I am using a Feign Client to call a REST endpoint with success, and have logging turned on to FULL.我正在使用 Feign Client 成功调用 REST 端点,并将日志记录打开到 FULL。 This is helpfully shows me the request sent and the response received.这有助于向我显示发送的请求和收到的响应。 However, I cannot see which server the request was sent to.但是,我看不到请求被发送到哪个服务器。 It only shows me that it was POSTed to http://foo-service which is the name of the service , not the name of the server .这只能说明我,它被张贴到http://foo-service服务,而不是服务器的名称的名称。

How can I log which server name this request was sent to?如何记录此请求发送到的服务器名称

This is what I see in the logs:这是我在日志中看到的:

c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] ---> POST http://foo-service/some-endoint HTTP/1.1
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Accept: application/json
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Content-Type: application/json
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Content-Length: 15
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] 
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] {"name":"John"}
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] ---> END HTTP (15-byte body)
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] <--- HTTP/1.1 200 (8ms)
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Transfer-Encoding: chunked
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] X-Application-Context: fooService:9006
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Date: Tue, 10 Oct 2017 09:25:36 GMT
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] Content-Type: application/json;charset=UTF-8
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] 
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] {"result":"Hello John"}
c.l.l.r.service.FooFeignClient : [FooFeignClient#bar] <--- END HTTP (23-byte body)

To turn on logging, I declare this bean:要打开日志记录,我声明了这个 bean:

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

We are using spring-cloud-netflix-core:1.1.0.M4我们正在使用 spring-cloud-netflix-core:1.1.0.M4

You will need to provide your own Logger to achieve this.您需要提供自己的Logger来实现这一点。 The default Logger is very simple and logs the host before the request is sent.默认的Logger非常简单,它在发送请求之前记录主机。 When using Ribbon the information will be available with the Response .使用Ribbon ,信息将随Response一起提供。 If you override the logAndRebufferResponse method, the response.url property will be the entire url submitted, including host name.如果您覆盖logAndRebufferResponse方法,则response.url属性将是提交的整个 url,包括主机名。

protected Response logAndRebufferResponse(
    String configKey, 
    Level logLevel, 
    Response response,
    long elapsedTime) throws IOException {

    /* ask the response for the request and log the uri */
    log(response.request.url());
}

Enable logging via logback and set log level of debug or these classes通过 logback 启用日志记录并设置调试或这些类的日志级别

<logger name="com.netflix.loadbalancer.BaseLoadBalancer" level="DEBUG"/>
<logger name="com.netflix.loadbalancer.LoadBalancerContext" level="DEBUG"/>
<logger name="com.netflix.loadbalancer.reactive.LoadBalancerCommand" level="DEBUG"/>

I can see where each request is going (server:port) and request failed on which server我可以看到每个请求的去向(服务器:端口)并且请求在哪个服务器上失败

DEBUG [          c.a.m.c.f.CommonProducerClient][72] - [CommonProducerClient#findAllEmployee] ---> GET http://COMMON-PRODUCER/allemployee HTTP/1.1
DEBUG [    c.n.loadbalancer.LoadBalancerContext][492] - COMMON-PRODUCER using LB returned Server: localhost:7001 for request http:///allemployee
DEBUG [      c.n.l.reactive.LoadBalancerCommand][314] - Got error java.net.ConnectException: Connection refused: connect when executed on server localhost:7001
DEBUG [      c.n.l.reactive.LoadBalancerCommand][314] - Got error java.net.ConnectException: Connection refused: connect when executed on server localhost:7001
DEBUG [      c.n.l.reactive.LoadBalancerCommand][314] - Got error java.net.ConnectException: Connection refused: connect when executed on server localhost:7001
DEBUG [    c.n.loadbalancer.LoadBalancerContext][492] - COMMON-PRODUCER using LB returned Server: localhost:7003 for request http:///allemployee
DEBUG [          c.a.m.c.f.CommonProducerClient][72] - [CommonProducerClient#findAllEmployee] <--- HTTP/1.1 200 (4010ms)

From Logs its clear, first request went to port 7001, connection refused there, second request goes to 7003, all good there.从日志中可以清楚地看出,第一个请求到达端口 7001,连接被拒绝,第二个请求到达 7003,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM