Spring Cloud: Exploring Zuul Gateway

This is a quick tutorial for Spring cloud Zuul component. In this tutorial, we will explore Zuul functionality and will create a gateway to our Spring cloud environment using Zuul.

What is Zuul?

Zuul is a gateway component in Spring cloud ecosystem. It provides access to the services present in our Spring cloud from services/applications which are outside of our environment.

Zuul creates a single entry point for our application which we can use from the outside world. In our environment, we might have 100’s of services running, each one serving a special purpose. Now if we want to access those services via another network or from the internet, then exposing those 100 services will not be a great idea.

In such cases, Zuul comes handy. It provides us with a single proxy to access those services. Apart from that Zuul also provides request filters. We can use those to check/process each and every request before it hits the actual service.

TL;DR You can download whole project by clicking following link.


Setting up Eureka Server

Since we will be working with Spring cloud, we will need Eureka server up and running. Either you can refer Setting Up Eureka Server Using Spring Cloud post which has detailed step by step explanation of it or you can download the following project and run it directly.


Creating Zuul Gateway

Let’s create the Zuul gateway. For that, we will be creating a Maven Project. Our project structure is given below. We will be creating two files which are as follows:

  1. AsmZuulApplication.java – Spring boot runable file
  2. application.properties – Spring properties file
Project Structure

Adding Dependencies

Let’s add the required dependencies. Replace content in pom.xml file with the following content. Don’t forget to update the project using Maven > Update option. That will download all the necessary dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>asm-zuul</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>asm-zuul</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

If you have noticed, we have added ‘spring-cloud-starter-netflix-zuul‘ dependency. That is the required one for this project.


Creating Spring Boot Application

Create file AsmZuulApplication.java and add the following content. This file will serve as the entry point for our application. We will be adding @EnableZuulProxy annotation on this class. This will turn our Spring boot application into Zuul gateway.

package com.example.asmzuul;

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;

@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class AsmZuulApplication {

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

}

Adding Properties

Under resources folder create a file with name application.properties and add the following content.

spring.application.name=AwesomeZuulGateway
eureka.client.serviceUrl.defaultZone=http://localhost:11800/eureka
server.port=11810

zuul.routes.producer1.path=/producer1/**
zuul.routes.producer1.url=http://localhost:11801
zuul.routes.producer2.path=/producer2/**
zuul.routes.producer2.url=StudentProducer

Here I have shown two ways to configure Zuul gateway routes. There are two properties in each configuration which you will need to mention i.e. path and url.

The ‘path’ property mention which path needs to be forwarded and the ‘url’ property mentions where it is to be forwarded.

In the case of producer1, request coming to http://localhost:11810/producer1/* will be forwarded to http://localhost:11801/*. In this case, we have directly specified the URL where requests are supposed to be forwarded.

There is another way to configure Zuul i.e. using Eureka. See the case of producer2. In this case, we have mentioned url as ‘StudentProducer’ which is nothing but one of the applications registered in Eureka server. In this case, Zuul will internally connect with Eureka server to get the actual path.

You can download this whole project by clicking on following link.


Producer Service

We will need something to consume. So we will have a producer service first. We will be using producer service created in the previous tutorial. You can refer how to develop a eureka client application post.

We will need to make some changes in application.properties of the producer. We will add ‘server.port’ property since we will be configuring it in one of the routes of Zuul.

spring.application.name=StudentProducer
eureka.client.serviceUrl.defaultZone=http://localhost:11800/eureka
eureka.instance.instanceId=:
server.port=11801

TL;DR you can download producer project by clicking on following link.


Testing it…

Now run all three applications i.e. Eureka server, Zuul gateway project and Producer service. Once all three applications are up, you can check Eureka dashboard.

Now let’s consume our service through Zuul. For that, you will need to navigate to ‘http://localhost:11810/producer1/student/all’ or ‘http://localhost:11810/producer2/student/all’. In both of the cases, you should see the output as shown below.

Sample Output

Keep in mind, here we have used ‘http://localhost:11810‘ i.e. our Zuul URL and Zuul is internally invoking our actual services and giving us the results.

That’s all about configuring Zuul. Add a dependency, add an annotation and configure routes in the properties file and you are good to go.


Downloads for this tutorial:


References:

  1. Spring Cloud- Netflix Zuul Simple Example by JavaInUse
  2. Netflix zuul example – zuul api gateway pattern – spring cloud tutorial by HowToDoInJava

Leave a Reply

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