Spring Cloud Zuul Gateway Example

Welcome readers, in this tutorial, we will explore an interesting and the last Spring Cloud component known as Zuul Gateway to implement a gateway for the microservices.

1. Introduction

  • Spring Boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jar and are directly started using embedded servlet container (such as Tomcat, Jetty or Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them in the application
  • It helps in removing the boilerplate code, extra annotations, and xml configurations
  • It provides a powerful batch processing and manages the rest endpoints
  • It provides an efficient jpa-starter library to effectively connect the application with the relational databases
  • It offers a Microservice architecture and cloud configuration that manages all the application related configuration properties in a centralized manner.

1.1 What is Zuul Gateway?

Zuul Gateway is a front door for all requests coming to a backend application. It enables dynamic routing, monitoring, security, and resiliency to an application. It is a router that provides a single entry point to our application without managing the CORS (Cross-origin Resource Sharing) and authentication for each microservice of an application. It easily interacts with other cloud components like Eureka server for service discovery, Hystrix for fault tolerance mechanism, and Config server for configuration details. Following diagram quickly summarizes the Zuul Gateway.

Spring Cloud Zuul Gateway - Flowchart
Fig. 1: Zuul Gateway Flowchart

Now, open the eclipse ide and let’s see how to implement this tutorial in spring boot. Make note, we will be using the existing Eureka server and client applications created in other two articles of the spring cloud series. Developers must go through these tutorials for a detailed overview.

2. Spring Cloud Zuul Gateway Example

Here is a systematic guide for implementing this tutorial.

2.1 Tools Used

We are using Eclipse Kepler SR2, JDK 8 and Maven.

2.2 Project Structure

In case you are confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

Spring Cloud Zuul Gateway - Application Structure
Fig. 2: Application Structure

2.3 Project Creation

This section will demonstrate how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.

Spring Cloud Zuul Gateway - Maven Project
Fig. 3: Create a Maven Project

In the New Maven Project window, it will ask you to select a project location. By default, ‘Use default workspace location’ will be selected. Just click on the next button to proceed.

Spring Cloud Zuul Gateway - Project Details
Fig. 4: Project Details

Select the Maven Web App archetype from the list of options and click next.

Spring Cloud Zuul Gateway - Archetype Selection
Fig. 5: Archetype Selection

It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in Fig. 5. The version number will be by default: 0.0.1-SNAPSHOT.

Spring Cloud Zuul Gateway - Archetype Parameters
Fig. 6: Archetype Parameters

Click on Finish and the creation of the maven project will be completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created for the project. Let’s start building the application!

3. Application Creation

Below are the steps involved in developing the application.

3.1 Maven Dependencies

Here, we specify the dependencies for Spring Cloud, Eureka Server, and Zuul. Maven will automatically resolve the other dependencies. The updated file will have the following code.


<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

	<name>Spring cloud zuul gateway tutorial</name>

	<!-- importing the spring cloud parent pom -->

		<!-- dependency to support zuul gateway -->
		<!-- dependency to support eureka server -->


3.2 Configuration File

Create a new properties file at the Springcloudzuulgateway/src/main/resources/ location and add the following code to it.


  port: 9292

    name: greetingsuserinfozuulgateway
      path: /feign/**
      stripPrefix: false
      defaultZone: http://localhost:7171/eureka/

3.3 Implementation Class

Add the following code the main class to bootstrap the application from the main method. Always remember, the entry point of the spring boot application is the class containing @SpringBootApplication annotation and the static main method.


package com.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

 * @author yatinbatra
@SpringBootApplication	// This annotation boostraps and auto-configure the application.
@EnableDiscoveryClient	// This annotation lists the application on the eureka server.
@EnableZuulProxy		// This annotation enables the zuul gateway.

// Application url - localhost:9292/feign/getGreetings/en
public class Myzuulgateway {

	public static void main(String[] args) {
		SpringApplication.run(Myzuulgateway.class, args);

4. Run the Applications

As we are ready with all the changes, let us compile the projects and run the applications as a java project.

  • Right click on the Eurekaserverapplication.java class, Run As -> Java Application. The Eureka server will be started on the 7171 port
  • Right click on the WelcomeApp.java class, Run As -> Java Application. The client microservice will be started on the 8181 port
  • Right click on the Springfeignclient.java class, Run As -> Java Application. The client microservice will be started on the 9191 port
  • Right click on the Myzuulgateway.java class, Run As -> Java Application. The client microservice will be started on the 9292 port

Developers can debug the example and see what happens after every step. Enjoy!

5. Project Demo

Now hit the following application url on your favorite browser and developers will see the output page.

Spring Cloud Zuul Gateway - Output
Fig. 7: Output Page

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

6. Conclusion

In this section, developers learned how to create a zuul gateway and interact with a microservice using this gateway. Developers can download the sample application as an Eclipse project in the Downloads section.

7. Download the Eclipse Project

This was an example of implementing the Zuul Gateway with Spring Cloud.

You can download the full source code of this example here: Spring Cloud Zuul Gateway Example


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
ananya gupta
4 years ago

I really don’t have an idea about Gateway but thanks to you for sharing informative content with us.

4 years ago

Indeed a “killer” post on Zuul Gateway. It took me some time to read it but nevertheless, it was worth the read.

Back to top button