Spring Cloud Gateway
Spring Cloud Gateway(SCG) provides a library for building API gateways on top of Spring and Java.
It aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.
SCG is a non blocking API. When using non blocking API, a thread is always available to process the incoming request. These request are then processed asynchronously in the background and once it is complete the response is returned. So,no incoming request never gets blocked when using SCG.
Spring Cloud Gateway Architecture
It consists of the following building blocks:-
1.Route: Route the basic building block of the gateway. It consists of:
- ID
- Destination URL
- Collection of predicates and a collection of filters
2.Predicate: This is similar to Java 8 Function Predicate.
Using this functionality we can match HTTP request, such as headers , url, cookies or parameters.
3.Filter: These are instances Spring Framework Gateway Filter.
Using this we can modify the request or response as per the requirement.
When the client makes a request to the SCG, the Gateway Handler Mapping first checks if the request matches a route. This matching is done using the predicates. If it matches the predicate then the request is sent to the filters.
Implementing Spring Cloud Gateway
Using SCG we can create routes in either of the two ways :
- Use java based configuration to programmatically create routes.
- Use property based configuration(i.e application.properties or application.yml) to create
Here,we will be implementing SCG using property based configurations.We will be implementing SCG application which routes request to two other microservices depending on the url pattern.
Implement First Microservice:
Firstly,Add the following dependency to pom.xml:
<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-gateway</artifactId> </dependency> </dependencies>
Define the application.yml as follows:
spring: application: name: first-service server: port: 8081
Create a Controller class that exposes the GET REST service:
import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/employee") public class FirstController { @GetMapping("/message") public String test() { return "Hello in First Service"; } }
Create the bootstrap class with the @SpringBootApplication annotation:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FirstApplication { public static void main(String[] args) { SpringApplication.run(FirstApplication.class, args); } }
Here ,we will do same thing another microservice,
Implement Second Microservice:
Firstly,Add the following dependency to pom.xml:
<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-gateway</artifactId> </dependency> </dependencies>
Define the application.yml as follows:
spring: application: name: second-service server: port: 8082
Create a Controller class that exposes the GET REST service:
import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/consumer") public class SecondController { @GetMapping("/message") public String test() { return "Hello in Second Service"; } }
Create the bootstrap class with the @SpringBootApplication annotation:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SecondApplication { public static void main(String[] args) { SpringApplication.run(SecondApplication.class, args); } }
Implement Spring Cloud Gateway using property based config:
Firstly,Add the following dependency to pom.xml:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
Define the application.yml as follows:
server: port: 8080 spring: cloud: gateway: routes: - id: employeeModule uri: http://localhost:8081/ predicates: - Path=/employee/** - id: consumerModule uri: http://localhost:8082/ predicates: - Path=/consumer/**
Create the bootstrap class with the @SpringBootApplication annotation:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class APIGatewayApplication { public static void main(String[] args) { SpringApplication.run(APIGatewayApplication.class, args); } }
Now,after doing this,start the three microservices we have just developed.
Go to url – localhost:8080/employee/message:
Go to url – localhost:8080/consumer/message
Conclusion:
In this blog, we learned about the basics of a SCG and how to implement API Gateway using Spring Cloud Gateway.Now you can easily use SCG with your microservices.
For more, you can refer to: https://spring.io/projects/spring-cloud-gateway