Spring Cloud Gateway

1. What is Spring Cloud Gateway?

Spring Cloud Gateway is a library that provides API gateway functionality. It allows you to:

  • Route requests to different microservices
  • Apply filters (like authentication, logging, rate limiting)
  • Handle cross-cutting concerns in a microservice architecture

It’s the modern alternative to Netflix Zuul.


2. Project Setup

You can use Spring Initializr with:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.x (or latest stable)
  • Dependencies:
    • Spring Boot Web
    • Spring Cloud Gateway
    • Spring Boot Actuator (optional, for monitoring)

pom.xml Example

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>gateway-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway-demo</name>
    <description>Spring Cloud Gateway Example</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2023.0.6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>
</project>

3. Application Properties

In application.yml:

server:
  port: 8080

spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: http://localhost:8081
          predicates:
            - Path=/users/**
          filters:
            - AddRequestHeader=X-Gateway-Header, GatewayDemo

        - id: product-service
          uri: http://localhost:8082
          predicates:
            - Path=/products/**
          filters:
            - AddRequestHeader=X-Gateway-Header, GatewayDemo

Explanation:

  • uri: The target microservice URL
  • predicates: When to route (e.g., /users/** path)
  • filters: Modify request/response (e.g., add a header)

4. Microservice Example

You can create two simple Spring Boot apps:

User Service (port 8081)

@RestController
@RequestMapping("/users")
public class UserController {

    @GetMapping
    public List<String> getUsers() {
        return List.of("Alice", "Bob", "Charlie");
    }
}

Product Service (port 8082)

@RestController
@RequestMapping("/products")
public class ProductController {

    @GetMapping
    public List<String> getProducts() {
        return List.of("Laptop", "Phone", "Tablet");
    }
}

5. Running the Gateway

  1. Start User Service on port 8081.
  2. Start Product Service on port 8082.
  3. Start Gateway on port 8080.

Test the Routes

  • Users: http://localhost:8080/users → routed to http://localhost:8081/users
  • Products: http://localhost:8080/products → routed to http://localhost:8082/products

Headers: The gateway will add X-Gateway-Header: GatewayDemo.


6. Summary

  • Spring Cloud Gateway is a lightweight API gateway.
  • Use routes to map paths to microservices.
  • Use filters for cross-cutting concerns.
  • Works perfectly with Spring Boot microservices.

Leave a Reply