Search code examples
spring-cloudnetflix-zuul

Dynamic Proxying with Zuul


I am trying to proxying with Zuul based on some cookies information. I presume I will have to write some filters that would achieve the filtering dynamically. Presumably, something as describe in this thread:

The first problem is that I am not able to get my basic test to work. I looked at the PreDecorationFilter filter and tried to model the forwarding part. This resulted in the following basic filter:

     @Bean
 public ZuulFilter myFilter() {
     return new ZuulFilter(){
                private UrlPathHelper urlPathHelper = new UrlPathHelper();

                 @Override
                 public Object run() {
                         RequestContext ctx = RequestContext.getCurrentContext();
                         final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest());
                         ctx.put("requestURI", requestURI);
                         ctx.set("forward.to", "localhost:8983");
                         ctx.setRouteHost(null);
                         return null;
                 }

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

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

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

My problem is that this doesn't work. When I hot the Zuul from the browser, the filter gets invoked, but the forwarding doesn't really happen. I get a response with content length of 0.

Which pieces of the puzzle am I missing?

EDIT: Adding my application.yml content:

info:
    component: Zuul Server

endpoints:
    restart:
        enabled: true
    shutdown:
       enabled: true
    health:
       sensitive: false

server:
   port: 8080

zuul:
   proxy:
      addProxyHeaders: true
   routes:
      dynamic:
          path: /dynamic/**
          url: http://route-to-nowhere.com

Solution

  • I think I found the answer. I should be setting routeHost instead of URI and forward.to. The following code works for me. It ignores the entry in application.yml and route to the host I specify dynamically.

    @Bean
     public ZuulFilter myFilter() {
         return new ZuulFilter(){
                     @Override
                     public Object run() {
                             RequestContext ctx = RequestContext.getCurrentContext();
                             try {
                                ctx.setRouteHost(new URL("http://localhost:8983"));
                            } catch (MalformedURLException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                             return null;
                     }
    
                     @Override
                     public boolean shouldFilter() {
                             return true;
                     }
    
                     @Override
                     public int filterOrder() {
                             return 10;
                     }
    
                     @Override
                     public String filterType() {
                             return "pre";
                     }};
     }