Introduction to Spring Cloud Gateway and Its Core Concepts

This article explains the purpose of an API gateway, introduces Spring Cloud Gateway as a modern, WebFlux‑based solution, outlines its features, core concepts such as routes, predicates and filters, shows configuration examples, dynamic routing, and how to create custom predicates and filters for microservice architectures.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Introduction to Spring Cloud Gateway and Its Core Concepts

An API gateway acts as the single entry point for microservices, handling cross‑cutting concerns like authentication, logging, rate limiting, and black‑white list checks, thereby avoiding duplicated code across services.

Spring Cloud Gateway is the next‑generation API gateway in the Spring Cloud ecosystem, built on WebFlux, Project Reactor, and Spring Boot 2.0. It aims to replace Netflix Zuul with better performance, extensibility, and richer features such as security, monitoring, and rate limiting.

Key features include dynamic routing based on any request attribute, support for predicates and filters, integration with Hystrix circuit breaker, service discovery via Spring Cloud DiscoveryClient, and easy predicate/filter definition.

The three core concepts are:

Route : defined by an ID, target URI, a set of predicates, and filters. A route is selected when its predicates evaluate to true.

Predicate : matches requests based on path, method, header, cookie, host, etc.

Filter : an implementation of GatewayFilter that can modify requests or responses before or after routing.

Typical request flow: a client sends a request to Spring Cloud Gateway, the Gateway Handler Mapping finds a matching route, the request passes through the filter chain, and finally reaches the downstream service.

Core configuration example:

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: https://example.org
          predicates:
            - Path=/test/**
          filters:
            - AddRequestHeader=X-Request-Id, 1024
            - AddRequestParameter=color, red

Dependency declaration:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

Main application class:

@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

Example application.yml configuration with static routes and a dynamic route using Nacos service discovery:

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: config_route
          uri: http://ityouknow.com
          predicates:
            - Path=/routeconfig/rest/**
        - id: header_route
          uri: http://ityouknow.com
          predicates:
            - Header=X-Request-Id, \d+
    nacos:
      discovery:
        server-addr: localhost:8848

Dynamic routing can be achieved by integrating with Nacos or Eureka for service discovery and Ribbon for load balancing.

Built‑in predicate factories include Path, After, Cookie, Header, Host, and Method, each configurable via YAML as shown in the examples.

Custom predicates can be created by extending AbstractRoutePredicateFactory and implementing the matching logic.

Custom filters are implemented by implementing GatewayFilter and Ordered, allowing developers to add authentication, logging, or other request‑processing logic.

Spring Cloud Gateway also provides several default filters such as AddRequestHeader, PrefixPath, Hystrix, RateLimit, and Retry.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaMicroservicesapi-gatewaySpring BootSpring Cloud Gateway
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.