Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

API Gateway is a concept used to name an application, where external applications will have to call to it in order to call to an application of the internal system. The API Gateway will hold the information of the internal applications and depending on the user’s request, it will forward the request to the right application we need. We have many different ways to implement an API Gateway application, if your application is using Spring Cloud Netflix, you can use Zuul Proxy. In this tutorial, I will guide you to use Zuul Proxy to create an API Gateway application!

The first thing I need to tell you about Zuul Proxy is: Zuul Proxy is part of Zuul, a routing library, and load balancer server from Netflix. It will configure the service id of the service in Eureka Server with the corresponding context path of the service id, expose a URL so that the external application can access it, then rely on the context path that the application is requesting to forward the request to the correct internal application.

For you to better understand, I will make an example using the Eureka Server I created in this tutorial using Spring Cloud Netflix along with service “Eureka Client Example” in this tutorial. To illustrate, I will add a simple URL request “/hello” to the service “Eureka Client Example” as follows:

Result:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

Now, if you start this application, you will see the information in Eureka Server as follows:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

OK, now I will create an API Gateway for service application “Eureka Client Example” using Zuul with Eureka Client as follows:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

Using Eureka Client dependency makes Zuul able to get information about services in the Eureka Server!

Result:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

To enable Zuul Proxy for this project, first you need to add @EnableZuulProxy annotation to SpringCloudZuulApplication class as follows:

Next, you need to declare the Eureka Server information so that Zuul Proxy can get the information of service “Eureka Client Example” in application.properties file:

The “eureka.client.fetchRegistry” property is very important, it will help Zuul get information about services in Eureka Server.

In order for Zuul Proxy to forward requests from users to service “Eureka Client Example”, we need to configure this service in the application.properties file of the API Gateway application as follows:

In which, eurekaclientservice in the property key is used to distinguish this service from other services. The “/eurekaclient/**” value in the “zuul.routes.eurekaclientservice.path” property is used to determine which requests start with “/eurekaclient/” will be forwarded by the API Gateway to “Eureka Client Example”!

Now if you run the API Gateway and request the URL “http://localhost:8085/eurekaclient/hello”, you will see the same result as when we request the service “Eureka Client Example” as follows:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

In case you want to keep the prefix path of the “zuul.routes.eurekaclientservice.path” property, you need to declare the “zuul.routes.eurekaclientservice.strip-prefix” property as follows:

Now you can request to Zuul with the request “http://localhost:8085/hello” as follows:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

If you have a lot of services and want to configure a common context path for all services, you can configure in the application.properties file a Zuul property is zuul.prefix with a value that is the common context path.

If now I configured the context path for all services in the above example as /huongdanjava as follows:

then the result will be:

Build API Gateway for web application using Spring Cloud Netflix Zuul Proxy

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment