javaspring-cloudnetflix-zuulspring-cloud-netflixnetflix-ribbon

How to get the hostname of the server to which a request will be forwarded to in zuul/ribbon


I'm currently using Zuul and Ribbon as a reverse proxy and load balancer respectively. I'm also using Eureka as service discovery. I have more than one instance of a service in Eureka, and I want to know the hostname of the server chosen by Ribbon.

This is my current configuration:

GatewayApplication.java:

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {
  static RequestQueue q = new RequestQueue();

  public static void main(String[] args) {
    q.start();
    SpringApplication.run(GatewayApplication.class, args);
  }

  @Bean
  public LogIncomingRequest logIncomingRequest() {
    return new LogIncomingRequest(q);
  }

  @Bean
  public LogLeavingRequest logLeavingRequest() {
    return new LogLeavingRequest(q);
  }
}

application.yml:

server:
  port: 4000

spring:
  application:
    name: zuul-gateway

zuul:
  sensitive-headers:

eureka:
  client:
    serviceUrl:
      defaultZone: http://${EUREKA:10.0.2.15:8761}/eureka/
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000

I also have a pre and a post filter. How can I get the information about the server that was chosen by ribbon?

I found this code but I have no idea where to use it and how to access the information.

@Component
public class RibbonInterceptor extends ZoneAvoidanceRule {

@Override
public Server choose(Object key) {
  Server choose = super.choose(key);
  System.out.println(choose);
  return choose;
}

Are there any other solutions?

Thanks in advance!


Solution

  • I just managed to find a solution for my problem. I created a POST filter and i got the information i needed using this code:

    RequestContext ctx = RequestContext.getCurrentContext();
    ((IResponse) ctx.get("ribbonResponse")).getRequestedURI();