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.
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, redDependency 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:8848Dynamic 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
