Last Updated:
The world famous nine arch bridge in Sri Lanka [https://visitella.com]

How to build an API-Gateway with Netflix Zuul + Spring Boot

Java

When you develop applications in microservice architecture, it is often required to route API requests to the respective service(s). Doing this with a general purpose load balancer like nginx or apache was the practice before. But it will take some control away from the application. Also it is important to remember that, maintaining an API-Gateway service brings you more benefits other than load balancing like below.

  • Authentication and Security
  • Monitoring
  • Dynamic Routing
  • Static Response handling
  • Rate limiting

You can implement all the above features with Spring Boot + Zuul, but this article will only cover how to create your first API-Gateway application. Let's start with an example.

Microservice architecture

Here we have multiple services executing their own business logic, so it is up to the Gateway to send requests to the correct microservice. This Gateway is a layer 7 load balancer which routes requests to the correct microservice based on the information presented in the request url.

Layer 4 load balancer bases the load-balancing decision on the source and destination IP addresses and ports recorded in the packet header, without considering the contents of the packet.

Layer 7 load balancers base their routing decisions on various characteristics of the HTTP header and on the actual contents of the message, such as the URL, the type of data (text, video, graphics), or information in a cookie.

-- Nginx

Netflix Zuul to the rescue 

Zuul is a library developed by Netflix, that provides dynamic routing, monitoring, resiliency, security, and more amazing features. Best thing is Spring Boot has already bundled Zuul with their Spring Cloud dependency. Lets see how we can implement our own API Gateway with Spring Cloud + Zuul.

  1. Generate the Spring Boot project. Go to Spring Initializer page and add Zuul as a dependency.
  2. Generating Spring Boot project
  1. Download and extract the project.
  2. You just need to add @EnableZuulProxy annotation to the Main class to make this project a Zuul proxy server. Spring Boot will auto configure everything based on this annotation.
  1. Now you can configure routing (i.e. Request paths and respective service urls) in the application.properties file.
  1. Execute mvn clean install to build the project. Then run it using java –jar <JARFILE>. You will see the API-Gateway is starting to listen on port 8080.
  2. Send two requests to http://localhost:8080/api/service_1/ and http://localhost:8080/api/service_2/, you will see requests are routed to service_1 and service_2 respectively as we have configured on application.properties file.

Did you see how awesome the Spring Boot and Zuul is, that we can configure an API-Gateway application with just one annotation and couple of configuration entries.

Next article of this series: Implement rate limiting for API Gateways with Spring boot + Zuul