Simple Reverse Proxy with Spring Boot and Netflix Zuul
Solution 1
Simple Reverse Proxy Server
It's easy to set up a simple proxy reverse using Spring Boot without Ribbon, Eureka, or Hystrix.
Simply annotate your main application class with @EnableZuulProxy
and set the following property in your configuration:
ribbon.eureka.enabled=false
Then define your routes in your configuration like such:
zuul.routes.<route_name>.path=<route_path>
zuul.routes.<route_name>.url=http://<url_to_host>/
where <route_name>
is an arbitrary name for your route and <route_path>
is a path using Ant-style path matching.
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 custom authentication and any additional headers by extending and implementing the ZuulFilter
class and adding it as an @Bean
to your @Configuration
class.
So another concrete 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();
}
}
Solution 2
Zuul is a good choice. Am not sure about other alternatives but, we've started building Zuul filters (Pre/Post and Route) that could intercept the request and do all pre/post processing and route based upon your need. It is not mandatory to use the whole bunch of Eureka, Ribbon and Hysterix along with Zuul.
Related videos on Youtube
Centinul
Updated on June 04, 2022Comments
-
Centinul about 2 years
I'm looking to implement a simple reverse proxy with Spring Boot that is:
- Easy to add routes
- Ability to add custom authentication on a per route basis
- Add additional headers as needed
I've looked at the facilities provided by the
@EnableZuulProxy
annotation but it seems too heavyweight as I don't have a desire to use Eureka, Ribbon, or Hystrix. However,@EnableZuulServer
is a bit light on configuration.Would anyone be able to provide an example of what I'm after? Is Netflix Zuul the right choice for this or is there another library I should be looking at?
Thanks!
-
Centinul about 9 yearsWould you be able to provide any examples of routing a request to a different origin?
-
yathirigan about 9 yearswe are trying to implement this by refering to the URL patterns that can be defined in application.yml cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html
-
Centinul about 9 years@yahthirigan are you writing a custom "pre" filter to parse the YAML file? If so are you then passing that in to a "route" filter that was already defined by the spring-cloud-netflix project?
-
yathirigan almost 9 yearsour pre-filters do not parse the YAML but does certain operations on the incoming requests.. something like your requirement of adding custom headers. We then leave it to the already existing route filter to route it to the actual end point as configured in the zuul routes.