table of Contents
A, Spring Cloud Gateway Overview and features
Two, Spring Cloud Gateway Integration
Three, Spring Cloud Gateway terminology and works
Fourth, routing Predicate factory
Eight, RouteDefinitionLocator encoding routing configuration
Nine, CORS configuration to achieve cross-domain
A, Spring Cloud Gateway Overview and features
Before Netflix Zuul Cloud has written the Spring ( Spring Cloud Zuul's Api gateway implementations and Spring Cloud Zuul detailed description of the article) is, Zuul into 1.x and 2.x versions. But first it is sure that Spring Cloud Gateway performance than Zuul, but Gateway based on the Spring 5 , the Spring the Boot 2 and Project Reactor realization (version requirements are relatively high). Spring Cloud Gateway is designed to provide a simple and effective way to route to the api, and provide cross concern for them, such as: security, monitoring / measurement and elasticity.
characteristic:
Built on Spring 5, Spring Boot 2, Project Reactor
Routing can be performed (of Api gateway) requests
Predicates and Filter special request processing for each route
Hystrix integrated circuit breaker
Spring Cloud registry support
Request frequency limit
Rewrite request (similar to the Nginx)
Two, Spring Cloud Gateway Integration
Start.spring.io added in the gateway module, or add the following dependency in the pom:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
Introduced after Sping Cloud Gateway enabled by default gateway associated maven dependent, but may be disposed at bootstrap.properties added (or YML) for closing:
spring.cloud.gateway.enabled=false
Three, Spring Cloud Gateway terminology and works
1, Terminology
Route: route is gateway base module, set by the id, the target uri, Predicate, and set Filter Composition
The Predicate: the Predicate Java8 is a function of the input type of Spring ServerWebExchange, allowing the Http match anything from, or parameters such as the request header.
Filter: Use Spring GatewayFilter Examples of particular plant construction, the request information may be modified before the downstream return or modify the content request occurs after the response to the request (this is consistent with the Zuul filter).
2, the working principle
The client sends a request to the Spring Cloud Gateway service, if the request can be satisfied and a route map is sent to the Gateway Web Handler, then the service logic processing before the request or after the request based Filter call chain configuration.
Fourth, routing Predicate factory
- After Route Predicate Factory
- Before Route Predicate Factory
- Between Route Predicate Factory
- Cookie Route Predicate Factory
- Header Route Predicate Factory
- Host Route Predicate Factory
- Method Route Predicate Factory
- Path Route Predicate Factory
- Query Route Predicate Factory
- RemoteAddr Route Predicate Factory
V. Filter Factory
- AddRequestHeader GatewayFilter Factory
- AddRequestParameter GatewayFilter Factory
- AddResponseHeader GatewayFilter Factory
- DedupeResponseHeader GatewayFilter Factory
- Hystrix GatewayFilter Factory
- FallbackHeaders GatewayFilter Factory
- PrefixPath GatewayFilter Factory
- PreserveHostHeader GatewayFilter Factory
- RequestRateLimiter GatewayFilter Factory
- RedirectTo GatewayFilter Factory
- RemoveHopByHopHeadersFilter GatewayFilter Factory
- RemoveRequestHeader GatewayFilter Factory
- RemoveResponseHeader GatewayFilter Factory
- RewritePath GatewayFilter Factory
- RewriteResponseHeader GatewayFilter Factory
- SaveSession GatewayFilter Factory
- SecureHeaders GatewayFilter Factory
- SetPath GatewayFilter Factory
- SetResponseHeader GatewayFilter Factory
- SetStatus GatewayFilter Factory
- StripPrefix GatewayFilter Factory
- Retry GatewayFilter Factory
- RequestSize GatewayFilter Factory
- Modify Request Body GatewayFilter Factory
- Modify Response Body GatewayFilter Factory
- Default Filters
Sixth, the global filter
- Combined Global Filter and GatewayFilter Ordering
- Forward Routing Filter
- LoadBalancerClient Filter
- Netty Routing Filter
- Netty Write Response Filter
- RouteToRequestUrl Filter
- Websocket Routing Filter
- Gateway Metrics Filter
- Marking An Exchange As Routed
Seven, ssl support
Support is needed to add
Eight, RouteDefinitionLocator encoding routing configuration
Use RouteDefinitionLocator route, Predicate, Filter arrangement, i.e. can be implemented in two ways, profiles and coding method, as follows:
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r ->
r.path("/kevin/**")
.filters(
f -> f.stripPrefix(1)
)
.uri("http://127.0.0.1:8090/helloWorld")
)
.build();
}
Nine, CORS configuration to achieve cross-domain
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "https://www.kevin.com"
allowedMethods:
- GET
About one common solution is similar means Nginx, and gateway services, Spring Cloud Gateway after this configuration, the Get request www.kevin.com services can be cross-domain access, the question as to the relevant cross-domain solutions can be found and I written between cross-domain sharing issues .