1

I'm using Eureka for service discovery and Zuul+ribbon as reverse proxy and load balancer. I have 2 instances registered under Eureka as follows:

MYSERVICE   n/a (2) (2) UP (2) - MYHOST:MyService:8888 , MYHOST:MyService:9999

Below is my zuul config:

@EnableZuulProxy
@EnableDiscoveryClient

zuul:
  debug.request: true
  sensitiveHeaders: 
  routes:
ecm:
   path: /myservice/**
   serviceId: MYSERVICE
   stripPrefix: false
  host:
       maxTotalConnections: 200
       maxPerRouteConnections: 30
  RibbonRoutingFilter:
       route.disable: false

I want a filter or interceptor which would help me log my request URL, my request params and the server chosen by Zuul.

I tried extending the following:

@Component
public class RibbonInterceptor extends ZoneAvoidanceRule {

@Override
public Server choose(Object key) {

Server choose = super.choose(key);
System.out.println(choose);
return choose;
}

But, this just gave me the server info from Ribbon, and here ribbon was just choosing a server. I wanted this info from Zuul along with the request details.

Please help!!

Rakz
  • 156
  • 1
  • 3
  • 11
  • Have you tried creating a Zuul `post` filter? there might be information on which endpoint was chosen, perhaps in the headers even – phoenix7360 Feb 02 '18 at 07:21
  • Zuul filter does not expose ribbon related things in detail. Already tried by implementing PRE, ROUTE & POST filters. – Rakz Feb 07 '18 at 18:44

2 Answers2

1

For the request URL and the server chosen by Zuul, you can set the loglevel of the LoadBalancerContext to DEBUG in application.properties.

#logging load balancing information 
logging.level.com.netflix.loadbalancer.LoadBalancerContext=DEBUG

This will create a log statement like:

2017-09-11T12:59:09.746-07:00: [DEBUG] hystrix-myserviceV3-2 com.netflix.loadbalancer.LoadBalancerContext - myserviceV3 using LB returned Server: myservice-2.abc.com:8080 for request http:///myservice/auth/users

Not sure though, how you can handle the request params.

pan
  • 1,899
  • 1
  • 16
  • 24
  • This would just print out in the log file. I need an actual interceptor kinda thing just after the server is selected along with the request object. – Rakz Feb 07 '18 at 18:42
0

Assuming you use Apache HttpClient, there are many ways to do this but I think the most simple is to add an HttpRequestInterceptor to the CloseableHttpClient used by Ribbon. You can customize the client by providing a bean of type CloseableHttpClient as mentioned in the documentation [1]. You then have the request actually used by HttpClient so you log the details.

@Bean
public HttpClient delegate(IClientConfig clientConfig)
{
    HttpClientBuilder builder = HttpClientBuilder.create();
    //set connection pool configuration

    HttpRequestInterceptor interceptor = (request, context) -> logger.info("Server : {}, headers : {}", request.getRequestLine().getUri(), request.getAllHeaders());
    builder.addInterceptorFirst(interceptor);

    return builder.build();
}

You could also extend HttpClientRibbonCommand and override the run() method to print what you want. You would use your new class by providing a bean of type RibbonCommandFactory<YourExtendedRibbonCommand> and it should wire automatically to the RibbonRoutingFilter.


Finally, if you're using the semaphore isolation strategy in hystrix, you could use your RibbonInterceptor like you indeed in addition to com.netflix.zuul.context.RequestContext. In the RequestContext, you'll find the original HttpServletRequest along with parsed parameters and headers that were processed in the pre filters.


[1] https://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_zuul_http_client

jebeaudet
  • 1,533
  • 16
  • 15