【问题标题】:How to modify the Zuul ServiceId at Runtime based on request param?如何根据请求参数在运行时修改 Zuul ServiceId?
【发布时间】:2019-09-19 07:58:03
【问题描述】:

如何实现根据请求参数改变servciceId??

下面是我们的 Zuul 配置


zuul:
  host:
    connect-timeout-millis: 200000
    connection-request-timeout-millis: 200000
    socket-timeout-millis: 200000
  ignored-services: "*"
  routes:
    route-1:
      path: /path1/**
      serviceId: ServiceA
    route-2:
      path: /path2/**
      serviceId: ServiceB

这里我们根据 path1/path2 选择 serviceId。

如果 http://localhost:8050/path1/endpointPath?requestParam=ParamValue1 这应该调用 serviceA

如果 http://localhost:8050/path1/endpointPath?requestParam=ParamValue2 这应该调用 serviceB

【问题讨论】:

    标签: netflix-eureka netflix-zuul


    【解决方案1】:

    能够通过使用路由过滤器和配置更改来实现这一目标

    配置:

     routes:
        route-1:
          path: /**
          serviceId: ServiceA
          stripPrefix: true
    

    路由过滤器:

     Optional<String> parameter = Optional.ofNullable(ctx.getRequest().getParameter("requestparam"));
            if (parameter.isPresent()) {
                if (parameter.get().equalsIgnoreCase("ValueA")) {
                    ctx.set("serviceId", "ServiceA");
    
                } else {
                    ctx.set("serviceId", "ServiceB");
    
                }
            }
    

    这很好还是我们有更简单的方法来实现? 这里有什么我们可以限制不在属性文件中定义 serviceId 的吗?

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多