Spring API Gateway Implementation with sample apps

Spring Cloud Gateway Overview

The Spring Cloud Gateway (SCG) is an API gateway proxy. It’s open-source based on the Java language. It has tons of features and can be embedded with code and can also be deployed as a separate service and scaled easily on Kubernetes containers.

The SCG is capable of handling client requests traffic by routing to desired microservices using the gateway handler mapping and aggregate responses of different back-end REST API endpoints of microservices.

SCG runs on Netty the non-blocking web server which provides asynchronous request processing for faster non-blocking processing of client requests.

It’s based on the following three major pillars:

  • Route traffic to microservices: It handles routing client requests to designated REST API endpoint destinations. Imagine a use case where only the /catalogue API is being called. In this use case, the API gateway forwards client requests with payload to the Catalogue microservice. We will see this in our next source code example in the next section.
  • Predicates: It helps to add the condition on incoming client requests like checking request URI, parameter, or assigning weight.
  • Filters: It helps to implement the Spring framework web filters. Developers can modify the request and response based on the client’s preferences or security reasons. Developers can also add their custom filtering logic to filter incoming client requests and outgoing responses with no source code changes.

Spring Cloud Gateway Implementation Step by Step

In this section, we will implement the Spring Cloud Gateway routing features. In this coding exercise, we will cover major API gateway features route traffic, filters, and predicates.

We will use the dynamic routing configuration by making changes in application properties files. It will make changes applied dynamically without restarting microservices web apps. It will be deployed as a separate microservice on the Kubernetes container and can be deployed on multiple containers to provide HA

Prerequisite

These are basic installation requirements to build:

• Java 8+
• Spring Boot v2.4.1+
• Spring Cloud Gateway
• Java v8.x+
• Source code reference: https://github.com/rajivmca2004/spring-gateway-demo

Let’s create a simple Spring Boot Java microservice using the Spring Cloud Gateway:

1. Create the catalogue-cache-service project using the SprCreate the spring-gateway-demo project using the Spring Initializer web portal: https://start.spring.io/

2. Now, we will define Spring Cloud Gateway routes, predicates, and filters in the application.yaml file.

3. We will configure API gateway routing for two separate microservices customer-management-service and catalogue-service. We will test and verify these in the upcoming points.

1.	spring:  
2.	  application:  
3.	    name: catalogue-service  
4.	  jpa:  
5.	    hibernate:  
6.	      ddl-auto: update  
7.	  cache:  
8.	    type: redis  
9.	  redis:  
10.	    host: localhost  
11.	    port: 6379  
12.	  
13.	server:  
14.	  port : 8010  
15.	springdoc:  

4. We will use filters attributes [RS1] [RS2] of the Spring Cloud Gateway in the rate limiting implementation section in the same chapter:


1.	spring:  
2.	  application:  
3.	    name: spring-gateway-demo  
4.	  redis:  
5.	    host: localhost  
6.	    port: 6379      
7.	  cloud:  
8.	    gateway:  
9.	      routes:  
10.	      - id: catalogues_route  
11.	        uri: http://localhost:8010  
12.	        predicates:  
13.	        - Path=/catalogue  
14.	        - Weight=group1, 6  
15.	      - id: customers_route  
16.	        uri: http://localhost:8011  
17.	        predicates:  
18.	        - Path=/customers  

5. If the client goes to http://localhost:8080/customers, then it will route to the customer-management-service microservice REST API http://localhost:8011/customers which is running as a separate web service on a different container and port number:

6. If a client click to http://localhost:8080/catalogue, then it will route to the customer-management-service microservice REST API http://localhost:8010/catalogue which is running as a separate web service on a different container and port number:

Published by

Rajiv Srivastava

Cloud Native Architect with VMware

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s