Zuul Proxy Server + Routing
In microservices architecture, we may have hundreds of services in a single application. It is hard to call every service individually, because every service has a different url and port on which it is running. For clients, it may be a lot of hassle maintaining such api calls.
To overcome this issue, a Gateway is used. A Gateway appilcation works as a gateway for all the services in a single application. Now, the clients have to make calls to different services using the single Gateway. It is the responsibility of the Gateway to route the request to the appropriate microservice. These routes are managed within the Gateway application, and it knows well, where to route a particular request to.
Note -> For Clients there is just one point of contact i.e. Gateway. it is like there is just one service with different endpoints.
Zuul proxy server is used to implement Gateway application. Let’s understand this with below example
We will follow the below steps for this tutorial.
- Create a simple microservice
- Build and run the microservice
- Create a Zuul Proxy Gateway service
- Define routing for the microservice created in the step 1.
- Run the Gateway service and make a call to the microservice using Gateway endpoint.
Step 1 – Create one simple microservice
pom.xml
<?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.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javatrainingschool</groupId>
<artifactId>spring-boot-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Write a rest controller class
package com.javatrainingschool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/basic")
public class BasicController {
@RequestMapping("/home")
public String home() {
return "This is home.";
}
@GetMapping("/welcome")
public String welcome() {
return "Welcome to this simple microservice";
}
}
Add few properties in the application.properties file
spring.application.name = mySampleService
server.port = 8081
Step 2 – Run and test the microservice
Hit the following url – http://localhost:8081/basic/home
Step 3 – Create a Zuul Proxy service
Create another spring starter project with below dependencies
pom.xml
Note -> In the below pom spring-boot-starter-parent version 2.3.3.RELEASE is compatible with spring-cloud.version 2.2.0.RELEASE version. Make sure you keep both the version same
Note -> Keep java version as 1.8 for this example
<?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.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.sks</groupId>
<artifactId>zuul-proxy-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zuul-proxy-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Next, in the spring boot application class, add @EnableZuulProxy annotation.
package com.javatrainingschool;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayServerApplication.class, args);
}
}
Step 4 – Define zuul routes
Now, we need to define zuul routes to access the microservice created in the step 1.
We need to add below two properties
zuul.routes.sampleService.path = /api/**
zuul.routes.sampleService.url = http://localhost:8081/
Here, in zuul.routes.sampleService.path ‘sampleService’ can be replaced with some other name as well. This name doesn’t matter (meaning, you can also write zuul.routes.basicService.path = /api/**). But the values against these two properties do matter.
Zuul.routes.sampleService.url property means whenever there is /api/** in the gateway url, then the request would be forwarded to http://localhost:8081 which is the base url for the microservice ‘mySampleService’ created in the step 1.
The complete application.properties is as follows
spring.application.name = zuulServer
zuul.routes.sampleService.path = /api/**
zuul.routes.sampleService.url = http://localhost:8081/
server.port=8111
Step 5 – Call the service using Gateway
Now, we will call the service using Gateway. The url would be as follows. Since, the gateway is running on the port 8111, the url will be as below
http://localhost:8111/api/basic/home
http://localhost:8111/api/basic/welcome