zuul修改請求url

除了修改請求參數、設置響應header,響應body外,還有一種需求就是url從新,或者是修改url,這裏簡述一下怎麼在zuul修改url。java

轉發配置

demo:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    listOfServers: 192.168.99.100,192.168.99.101
zuul:
  routes:
    demo:
      path: /demo/**
      stripPrefix: true
      serviceId: demo

filter配置

@Component
public class UrlPathFilter extends ZuulFilter{

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
    }

    @Override
    public boolean shouldFilter() {
        final String serviceId = (String) RequestContext.getCurrentContext().get("proxy");
        return "demo".equals(serviceId);
    }

    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        Object originalRequestPath = context.get(FilterConstants.REQUEST_URI_KEY);
        //http://localhost:10000/demo/list/data
        //-->/api/prefix/list/data
        String modifiedRequestPath = "/api/prefix" + originalRequestPath;
        context.put(FilterConstants.REQUEST_URI_KEY, modifiedRequestPath);
        return null;
    }
}

這樣就大功告成了git

doc

相關文章
相關標籤/搜索