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.
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.
- Spring Cloud Eureka Server and Client application
- Spring Cloud Feign Client application
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.
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
.
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.
Select the Maven Web App archetype from the list of options and click next.
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
.
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.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jcg.tutorial</groupId> <artifactId>Springcloudzuulgateway</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring cloud zuul gateway tutorial</name> <url>http://maven.apache.org</url> <!-- importing the spring cloud parent pom --> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.RELEASE</version> </parent> <dependencies> <!-- dependency to support zuul gateway --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <!-- dependency to support eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <build> <finalName>Springcloudzuulgateway</finalName> </build> </project>
3.2 Configuration File
Create a new properties file at the Springcloudzuulgateway/src/main/resources/
location and add the following code to it.
application.yml
server: port: 9292 spring: application: name: greetingsuserinfozuulgateway zuul: routes: greetingsinfofeignclient: path: /feign/** stripPrefix: false eureka: client: serviceUrl: 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.
Myzuulgateway.java
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 the7171
port - Right click on the
WelcomeApp.java
class,Run As -> Java Application
. The client microservice will be started on the8181
port - Right click on the
Springfeignclient.java
class,Run As -> Java Application
. The client microservice will be started on the9191
port - Right click on the
Myzuulgateway.java
class,Run As -> Java Application
. The client microservice will be started on the9292
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.
localhost:9292/feign/getGreetings/en
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
I really don’t have an idea about Gateway but thanks to you for sharing informative content with us.
Thank you, reader! :-)
Indeed a “killer” post on Zuul Gateway. It took me some time to read it but nevertheless, it was worth the read.
Thank you, reader! :-)