官网地址:https://spring.io/projects/spring-cloud-gateway#learn
了解一些术语:
https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#glossary
Glossary
Route: The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.
Predicate: This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.
Filter: These are instances of GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.
How It Works
The following diagram provides a high-level overview of how Spring Cloud Gateway works:
Configuring Route Predicate Factories and Gateway Filter Factories
There are two ways to configure predicates and filters: shortcuts and fully expanded arguments. Most examples below use the shortcut way.
The name and argument names will be listed as
code
in the first sentance or two of the each section. The arguments are typically listed in the order that would be needed for the shortcut configuration.Shortcut Configuration
Shortcut configuration is recognized by the filter name, followed by an equals sign (
=
), followed by argument values separated by commas (,
).application.yml
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - Cookie=mycookie,mycookievalue
The previous sample defines the
Cookie
Route Predicate Factory with two arguments, the cookie name,mycookie
and the value to matchmycookievalue
.