【问题标题】:Get the hostname/IP:port of the server to which the request is to be forwarded from Zuul+ribbon从Zuul+ribbon获取请求转发到的服务器的hostname/IP:port
【发布时间】:2018-07-12 04:50:25
【问题描述】:

我使用 Eureka 进行服务发现,使用 Zuul+ribbon 作为反向代理和负载均衡器。 我在 Eureka 下注册了 2 个实例,如下所示:

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

下面是我的zuul配置:

@EnableZuulProxy
@EnableDiscoveryClient

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

我想要一个过滤器或拦截器,它可以帮助我记录我的请求 URL、我的请求参数和 Zuul 选择的服务器。

我尝试扩展以下内容:

@Component
public class RibbonInterceptor extends ZoneAvoidanceRule {

@Override
public Server choose(Object key) {

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

但是,这只是给了我来自 Ribbon 的服务器信息,而这里的 Ribbon 只是选择一个服务器。我想从 Zuul 获得此信息以及请求详细信息。

请帮忙!!

【问题讨论】:

  • 您是否尝试过创建 Zuul post 过滤器?可能有关于选择哪个端点的信息,甚至可能在标题中
  • Zuul 过滤器没有详细暴露功能区相关的东西。已经尝试通过实现 PRE、ROUTE 和 POST 过滤器。

标签: spring spring-boot spring-cloud netflix-zuul netflix-ribbon


【解决方案1】:

假设您使用 Apache HttpClient,有很多方法可以做到这一点,但我认为最简单的方法是在 Ribbon 使用的CloseableHttpClient 中添加一个HttpRequestInterceptor。如文档 [1] 中所述,您可以通过提供 CloseableHttpClient 类型的 bean 来自定义客户端。然后,您将获得 HttpClient 实际使用的请求,以便记录详细信息。

@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();
}

您还可以扩展 HttpClientRibbonCommand 并覆盖 run() 方法来打印您想要的内容。您将通过提供RibbonCommandFactory<YourExtendedRibbonCommand> 类型的bean 来使用您的新类,它应该自动连接到RibbonRoutingFilter


最后,如果你在 hystrix 中使用信号量隔离策略,除了com.netflix.zuul.context.RequestContext 之外,你还可以像你一样使用RibbonInterceptor。在RequestContext 中,您将找到原始HttpServletRequest 以及在pre 过滤器中处理的已解析参数和标头。


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

【讨论】:

  • 这个我没试过。将尝试尽快发布更新。谢谢:)
  • 您能否列出其他可能的方法?
【解决方案2】:

对于Zuul选择的请求URL和服务器,可以在application.properties中将LoadBalancerContext的loglevel设置为DEBUG

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

这将创建一个日志语句,如:

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

但不确定如何处理请求参数。

【讨论】:

  • 这只会在日志文件中打印出来。在选择服务器和请求对象之后,我需要一个实际的拦截器。
猜你喜欢
  • 2018-11-03
  • 1970-01-01
  • 2020-04-06
  • 2021-07-11
  • 2019-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多