How Zuul API Gateway Proxy Forwards Requests

In this tutorial, I would explain to you how an API Gateway forwards requests to backend Microservices.

 

  1. What is Zuul?
  2. How Zuul Routes Requests
  3. Routing by Service Names
  4. Routing by IP Address/Hostnames
  5. Performing Load Balancing

 

1. What is Zuul?

Zuul is an open-source proxy and routing application provided by Netflix. This means that Zuul can be configured to route incomming request to certain microservices. Hence Zuul (also called Zuul Proxy) can be used to implement an API Gateway.

Additionally, Zuul can perform load-balancing functions.

 

2. How Zuul Routes Requests

First, we need to note that a Zuul API Gateway is just another Spring Boot Application. Then you need to add the Spring Web and the Zuul Proxy dependencies.

After you have the application, you need to annotate the main application class with @EnableZuulProxy annotation.

Finally, you will create the application.yml file and places the configurations there

 

3. Routing by Service Names

In this examples, we would consider a microservices scenario where the services are registered with a discovery server. Therefore, they are referenced by their service names instead of ip addresses or hostnames

Example 1

Assuming that a http request coming to Zuul is /myusers and has to be forwarded to a service named users, then we would say:

 zuul:
  routes:
    users: /myusers/**

This means that requests coming to /myusers would be forwarded to users service. For instance /myusers/abc would be fowarded to users/abc)

 

Example 2

Look at the code below

 zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users_service

This code above means that any request coming to /myusers as before would be forwarded to users_service/myusers

  • /myusers/all  is forwarded to users_service/all
  • /myusers/vip/usa is forwarded to users_service/vip/usa
  • /myusers/kindson/dob is forwarded to users_service/kindson/dob
  • /myusers

 

4. Routing by IP Addresses/Hostnames

The Zuul proxy can also route requests to any url. In this case, instead of using serviceId, you use url. This is shown below:

 zuul:
  routes:
    users:
      path: /lessons/**
      url: https://youtube.com/kindsonthetechpro

In the example above, any requests coming to /lessons/ would be routed to https://www.youtube.com/kindsonthetechpro

 

5. Performing Load Balancing

Now assuming you want to forward to more than one end point or service. Or say the backend server is running two or more instances. This can be done in such a way that the requests load-balances between the servers.

The code below illustrates this.

zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users

ribbon:
  eureka:
    enabled: false

users:
  ribbon:
    listOfServers: munonye.com,google.com

 

Another example

In the example below, we have provided an application running two instances, each of a different port:

zuul:
  routes:
    users:
      path: /myusers/**
      serviceId: users

ribbon:
  eureka:
    enabled: false

users:
  ribbon:
    listOfServers: http://localhost:8081",http://localhost:8082"

 

I would recommend you watch the video for  as it becomes clearer.