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

时间:2017-10-10 09:39:17

标签: spring spring-cloud-netflix netflix-feign spring-cloud-feign

我正在使用Feign Client成功调用REST端点,并将日志记录设置为FULL。这有助于向我显示发送的请求和收到的响应。但是,我无法看到请求发送到哪个服务器。它只显示我发布到http://foo-service这是服务的名称,而不是服务器的名称。

如何记录此请求发送到哪个服务器名称

这就是我在日志中看到的内容:

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)

要打开日志记录,我声明了这个bean:

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

我们使用的是spring-cloud-netflix-core:1.1.0.M4

2 个答案:

答案 0 :(得分:1)

您需要提供自己的Logger才能实现此目标。默认Logger非常简单,并在发送请求之前记录主机。使用Ribbon时,Response会提供相关信息。如果您覆盖logAndRebufferResponse方法,则response.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());
}

答案 1 :(得分:0)

启用通过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"/>

我可以看到每个请求的去向(服务器:端口),并且请求在哪个服务器上失败

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)

从日志中清除,第一个请求转到端口7001,那里的连接被拒绝,第二个请求转到7003,那里的一切都很好。