How to select route based on header in Zuul

孤者浪人 提交于 2019-12-02 06:05:52

问题


I'm using a Netflix Zuul proxy in front of my services A and 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?


回答1:


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;
 }

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.

Also see this example



来源:https://stackoverflow.com/questions/48538352/how-to-select-route-based-on-header-in-zuul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!