DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > All and Sundry: Spring Cloud Zuul

All and Sundry: Spring Cloud Zuul

Spring Cloud projects to work with Zuul and other Netflix OSS projects for development.

Biju Kunjummen user avatar by
Biju Kunjummen
·
Jul. 07, 16 · Cloud Zone · Tutorial
Like (3)
Save
Tweet
2.72K Views

Join the DZone community and get the full member experience.

Join For Free

Netflix OSS project Zuul serves as a gateway to backend services and provides support for adding in edge features like security and routing. In the Zuul world, specific edge features are provided by components called the Zuul Filter, and writing such a filter for a Spring Cloud-based project is very simple. A good reference for adding a filter is here. Here, I wanted to demonstrate two small features — deciding whether a filter should act on a request and adding a header before forwarding the request.

Writing a Zuul Filter

Writing a Zuul Filter is very easy for Spring Cloud. All we need to do is to add a Spring bean which implements the ZuulFilter, so for this example, it would look something like this:

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Service;
@Service public class PayloadTraceFilter extendsZuulFilter{
    private static final String HEADER="payload.trace";       
    @Override public String filterType(){        
        return"pre";    
        }       
    @Override public int filterOrder(){
        return 999;
        }
    @Override public boolean shouldFilter() {      ....       }       
    @Override public Object run() {     ....    }
    }


Some high-level details of this implementation — this has been marked as a "Filter type" of "pre" which means that this filter would be called before the request is dispatched to the backend service. FilterOrder determines when this specific filter is called in the chain of filters. Should Filter determines if this filter is invoked at all for this request, and run contains the logic for the filter.

So to my first consideration, whether this filter should act on the flow at all (this can be done on a request-by-request basis, my logic is very simple) — if the request URI starts with /samplesvc, then this filter should act on the request.

@Override public boolean shouldFilter(){    
    RequestContext ctx = RequestContext.getCurrentContext();
    String requestUri = ctx.getRequest().getRequestURI();    
    returnrequestUri.startsWith("/samplesvc");
    }


And the second consideration on modifying the request headers to the backend service:

@Override public Object run(){    
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.addZuulRequestHeader("payload.trace", "true");    
    return null;
    }


A backing service getting such a request can look for the header and act accordingly, say in this specific case looking at the "payload.trace" header and deciding to log the incoming message:

@RequestMapping(value = "/message", method = RequestMethod.POST)
public Resource<MessageAcknowledgement> 
pongMessage(@RequestBodyMessage input, @RequestHeader("payload.trace") boolean tracePayload){    
    if(tracePayload){        
        LOGGER.info("Received Payload: {}", input.getPayload());
        }
  ....

?

Conclusion

As demonstrated here, Spring Cloud really makes it simple to add in Zuul filters for any edge needs. If you want to explore this sample a little further, I have sample projects available in my GitHub repo.

Spring Cloud Spring Framework zuul Filter (software) Requests

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ultra-Fast Microservices: When Microstream Meets Payara
  • Top Soft Skills to Identify a Great Software Engineer
  • Evolving Domain-Specific Languages
  • A Smarter Redis

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo