Simple reverse proxy with Spring Boot and Netflix Zuul

I am looking to implement a simple reverse proxy with Spring Boot, which:

  • Easy to add routes
  • Ability to add user authentication on each route
  • Add additional headers if necessary

I looked at the possibilities provided by the annotation @EnableZuulProxy, but it seems too heavy because I have no desire to use Eureka, Ribbon or Hystrix. However, @EnableZuulServerit illuminates the configuration a bit.

Can anyone provide an example of what I need? Is Netflix Zuul the right choice for this, or is there another library I should look at?

Thank!

+4
source share
2 answers

Simple reverse proxy

Easily set up a simple proxy reverse using Spring Boot without tape, Eureka or Hystrix.

Just comment on your main application class with @EnableZuulProxyand set the following property in your configuration:

ribbon.eureka.enabled=false

Then define your routes in your configuration as follows:

zuul.routes.<route_name>.path=<route_path>    
zuul.routes.<route_name>.url=http://<url_to_host>/

where <route_name>is the arbitrary name for your route, and <route_path>is the path using the Ant path match.

So a concrete example would be something like this

zuul.routes.userservice.path=users/**
zuul.routes.userservice.url=http://localhost:9999/

Custom filters

You can also implement your own authentication and any additional headers by expanding and implementing the class ZuulFilterand adding it as @Beanto your class @Configuration.

So, another specific example:

public class MyFilter extends ZuulFilter {

  @Override
  public String filterType() {
    // can be pre, route, post, and error
    return "pre";
  }

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

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

  @Override
  public Object run() {
    // RequestContext is shared by all ZuulFilters
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();

    // add custom headers
    ctx.addZuulRequestHeader("x-custom-header", "foobar");    

    // additional custom logic goes here

    // return isn't used in current impl, null is fine
    return null;
  }

}

and then

@Configuration
public class GatewayApplication {

  @Bean
  public MyFilter myFilter() {
    return new myFilter();
  }

}
+3

- . , Zuul (Pre/Post Route), /- . Eureka, Ribbon Hysterix Zuul.

0

All Articles