假装客户端:如何记录请求发送到的服务器名称



我正在使用Feign客户端成功调用REST端点,并将日志记录设置为FULL。这有助于向我显示发送的请求和收到的响应。但是,我看不到请求发送到哪个服务器。它只向我显示它被 POST 到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

您需要

提供自己的Logger才能实现此目的。 默认Logger非常简单,并在发送请求之前记录主机。 使用Ribbon时,信息将与Response一起提供。 如果重写 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());
}

通过日志启用日志记录并设置调试或这些类的日志级别

<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,那里一切正常。

相关内容

  • 没有找到相关文章

最新更新