如何在Zuul中基于标题选择路线 [英] How to select route based on header in Zuul

查看:65
本文介绍了如何在Zuul中基于标题选择路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务A和服务B之前使用Netflix Zuul代理.

I'm using a Netflix Zuul proxy in front of my services A and B.

如何使Zuul代理根据传入请求中的HTTP标头在通往A和B的路由之间进行选择?

How can I make the Zuul proxy to choose between routes to A and B based on a HTTP header in the incoming request?

推荐答案

您应根据自己的逻辑创建一个预过滤器.像这样的东西:

You should create a prefilter based on your logic. Something like this :

@Component
public class RedirectionFilter extends ZuulFilter {

@Override
public String filterType() {
   return "pre";
}

@Override
public int filterOrder() {
   return 2;
}

@Override
public boolean shouldFilter() {
  return true;
}

@Override
public Object run() {
  RequestContext ctx = RequestContext.getCurrentContext();
  HttpServletRequest request = ctx.getRequest();`
  String header = request.getHeader("YOUR_HEADER_PARAM");

  if ("YOUR_A_LOGIC".equals(header) ) {
    ctx.put("serviceId", "serviceA");
    //ctx.setRouteHost(new URL("http://Service_A_URL"));
  } else { // "YOUR_B_LOGIC"
    ctx.put("serviceId", "serviceB");
    //ctx.setRouteHost(new URL("http://Service_B_URL"));
  }
  log.info(String.format("%s request to %s", request.getMethod(), 
  request.getRequestURL().toString()));
  return null;
 }

我不确定100%的重定向部分,但这是您需要的开始. 我添加了重定向的第二个选项(带注释的行),也许这两个选项之一会为您提供帮助.

Im not sure 100% about the redirection part, but it's a beginning for your needs. i added second option for redirection (commented lines), maybe one of the 2 options will help you.

另请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆