Microservices – Getting Started Wth Netflix Zuul, Eureka And Ribbon Using Spring Boot 2

In this article, we are going to implement basic microservice architecture using Spring boot Netflix OSS stack. Netflix OSS is a set of tools for building microservice architecture. Netflix has developed all the tools to handle their massive amount of video streaming requests.

We will implement the basic setup of Zuul, Eureka, and Ribbon with minimal configuration. Later on, in other articles, we will add more component with advanced configuration.

Zuul API Gateway

An API gateway is the front door of your application. It acts as a layer between  API client (Web, Mobile, Other microservice) and your application server. Zuul is the API gateway developed by Netflix. It is a JVM-based router and server-side load balancer. Netflix uses Zuul for the following purposes.

  • Authentication
  • Insights
  • Stress Testing
  • Canary Testing
  • Dynamic Routing
  • Service Migration
  • Load Shedding
  • Security
  • Static Response handling
  • Active/Active traffic management

Following diagrams shows the simple architecture with and without Zuul API gateway server.

Microservice with Zuul, Ribbon, Eureka

Microservice with Zuul, Ribbon, Eureka

Monolithic Client-Server Architecture

If you want to know more about API gateway than you can read my article on API Gateway.

Eureka Service Discovery

Eureka service discovery server keeps the list of nodes(instances) of microservice architecture. Each node of microservice including API gateway also acts as a Eureka client. They send the heartbeat message of their presence to Eureka. Eureka server also sends the list of all available node to Eureka clients. Zuul and Ribbon use the list of available nodes t0 forward the request to the specific client.

Ribbon Loadbalancer

While application deals with millions of concurrent API calls, It needs to scale horizontally. Horizontal scaling can be achieved by using load balancers. The ribbon is JVM based client-side(HTTP/s) load balancer developed by Netflix and the part of the Netflix OSS. It can easily be integrated with Zuul and Eureka and you can get started with ribbon almost zero configuration.

Configure Zuul, Eureka, and Ribbon with spring boot 2

We will create the following 3 maven projects:

  1. Eureka service discovery server
  2. Student Microservice
  3. Zuul and Ribbon Server

The project structure in eclipse’s project view will be like this :

eureka-zuul-ribbon-starter-project

ECLIPSE PROJECT VIEW

Source code used in this article is also available on my Github Repo.

1. Eureka service discovery server

Spring boot starter provides the dependencies for the Eureka server. You can go with Eureka with a minimal set of configurations.

First, add Eureka dependencies to POM file.

Then add configurations in the service.properties file

We can configure all the basic configuration with service.properties file. We do not need to add much java code.

Then we will enable Eureka server by adding a @EnableEurekaServer annotation to our spring boot application.

That’s all we need to do for starting with the Eureka server. I will explain the advanced configuration of the Eureka server in the further part of this article series.

To create the project from start.spring.io use the following dependencies:

eureka-service-discovery-server

2. Student Microservice

The student microservice returns the list of students and port number of the application server so that we can ensure that the ribbon load balancer is working correctly.

Pom file of student service looks like:

service.properties file of student microservice:

We set the port number 0 in the service.properties file of the application. By setting port 0 spring boot application selects random available port during startup. The student microservice will also act as a service discovery client. It will register itself to the Eureka service discovery server.  

Sample API of student service. 

To create the project from start.spring.io use the following dependencies:

3. Zuul and Ribbon Server

To set up Zuul and ribbon you need to add the Netflix zuul starter dependency to your pom file. complete pom file will look like as follows:

Zuul starter dependency also has the Ribbon dependency so you do not need to add it separately for this demo.

In the service.properties file of the Zuul server we add the routes to the student service. We will access student microservice via Zuul proxy.

Then after add @EnableZuulProxy annotation in your spring boot application.

By adding it spring boot automatically setup the Zuul and ribbon.

To create the project from start.spring.io use the following:

eureka-service-discovery-server

Run the microservice cluster

You can start these 3 applications in any order but to understand the actual flow, We will start the Eureka service discovery server first. Immediately it will be ready to listen from the discovery clients about their presence in the cluster.

You can see the Eureka dashboard on localhost:8090

eureka-dashboard

Then start the student service. We have assigned port 0 to the student service, So it will automatically pick a random available port. I recommend starting at least two instances of student service to test load balancing.

At last start the Zuul server. It will also register itself to the Eureka service discovery server.

By refreshing the Eureka dashboard in the browser, You can see the Zuul server and student microservice registered with the number of active instances on the dashboard.

Now we can hit the API of student service via Zuul proxy. Postman collection of  API is also provided in the GitHub repo. Try to hit the same API multiple time. You can see the port numbers are changing in every subsequent request. This is due to the ribbon load balancer server.

You can hit this URL to test the demo or can also get Postman collection from GitHub repo:

localhost:8090/zuul-proxy/api/student/student/students

The response will look like this. If you spin up multiple instances of the student service than you can see that the port in the response will change in every subsequent response.

zuul-eureka-ribbon

You can spin up any number of instance of the student microservice. Eureka and zuul automatically pick up the instance and ribbon will distribute the load across the instances.

However, we can also spin up microservice instance automatically using Zuul. We will see it in the latter articles.

If you have experience with setting up Microservice architecture with Netflix OSS or any other stack than please share your experience and challenges with us in a comment section. Also, let me know if you want a specific article on any tool and part of microservice.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *