diff --git a/2.1.x/multi/multi__global_filters.html b/2.1.x/multi/multi__global_filters.html index 7640129f..795df6d5 100644 --- a/2.1.x/multi/multi__global_filters.html +++ b/2.1.x/multi/multi__global_filters.html @@ -34,7 +34,31 @@ the scheme specified in the request made to the Gateway. For example, if the re but the ServiceInstance indicates it is not secure, then the downstream request will be made over HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the -route URL will override the ServiceInstance configuration.

6.4 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

6.5 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

6.6 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute`.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

6.7 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml.  +route URL will override the ServiceInstance configuration.

[Warning]Warning

LoadBalancerClientFilter uses a blocking Ribbon LoadBalancerClient under the hood. +We suggest you use ReactiveLoadBalancerClientFilter instead. +You can switch to using it by adding org.springframework.cloud:spring-cloud-loadbalancer dependency to your project +and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false.

6.4 ReactiveLoadBalancerClientFilter

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute +ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), +it will use the Spring Cloud ReactorLoadBalancer to resolve the name (myservice in the previous example) +to an actual host and port and replace the URI in the same attribute. The unmodified +original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. +The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals +lb and then the same rules apply.

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: myRoute
+        uri: lb://service
+        predicates:
+        - Path=/service/**

+

[Note]Note

By default when a service instance cannot be found by the ReactorLoadBalancer, a 503 will be returned. +You can configure the Gateway to return a 404 by setting spring.cloud.gateway.loadbalancer.use404=true.

[Note]Note

The isSecure value of the ServiceInstance returned from the ReactiveLoadBalancerClientFilter will override +the scheme specified in the request made to the Gateway. For example, if the request comes into the Gateway over HTTPS +but the ServiceInstance indicates it is not secure, then the downstream request will be made over +HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the +route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the +route URL will override the ServiceInstance configuration.

6.5 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

6.6 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

6.7 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute`.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

6.8 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml. 

spring:
   cloud:
     gateway:
@@ -49,7 +73,7 @@ route URL will override the ServiceInstance configu
         uri: ws://localhost:3001
         predicates:
         - Path=/websocket/**

-

6.8 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

6.9 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted +

6.9 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

6.10 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted to the exchange attributes. Once a request has been marked as routed, other routing filters will not route the request again, essentially skipping the filter. There are convenience methods that you can use to mark an exchange as routed or check if an exchange has already been routed.

\ No newline at end of file diff --git a/2.1.x/multi/multi_spring-cloud-gateway.html b/2.1.x/multi/multi_spring-cloud-gateway.html index 2272acbf..d583795c 100644 --- a/2.1.x/multi/multi_spring-cloud-gateway.html +++ b/2.1.x/multi/multi_spring-cloud-gateway.html @@ -1,3 +1,3 @@ - Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Route Predicate Factories
4.1. After Route Predicate Factory
4.2. Before Route Predicate Factory
4.3. Between Route Predicate Factory
4.4. Cookie Route Predicate Factory
4.5. Header Route Predicate Factory
4.6. Host Route Predicate Factory
4.7. Method Route Predicate Factory
4.8. Path Route Predicate Factory
4.9. Query Route Predicate Factory
4.10. RemoteAddr Route Predicate Factory
4.10.1. Modifying the way remote addresses are resolved
5. GatewayFilter Factories
5.1. AddRequestHeader GatewayFilter Factory
5.2. AddRequestParameter GatewayFilter Factory
5.3. AddResponseHeader GatewayFilter Factory
5.4. DedupeResponseHeader GatewayFilter Factory
5.5. Hystrix GatewayFilter Factory
5.6. FallbackHeaders GatewayFilter Factory
5.7. MapRequestHeader GatewayFilter Factory
5.8. PrefixPath GatewayFilter Factory
5.9. PreserveHostHeader GatewayFilter Factory
5.10. RequestRateLimiter GatewayFilter Factory
5.10.1. Redis RateLimiter
5.11. RedirectTo GatewayFilter Factory
5.12. RemoveHopByHopHeadersFilter GatewayFilter Factory
5.13. RemoveRequestHeader GatewayFilter Factory
5.14. RemoveResponseHeader GatewayFilter Factory
5.15. RewritePath GatewayFilter Factory
5.16. RewriteLocationResponseHeader GatewayFilter Factory
5.17. RewriteResponseHeader GatewayFilter Factory
5.18. SaveSession GatewayFilter Factory
5.19. SecureHeaders GatewayFilter Factory
5.20. SetPath GatewayFilter Factory
5.21. SetRequestHeader GatewayFilter Factory
5.22. SetResponseHeader GatewayFilter Factory
5.23. SetStatus GatewayFilter Factory
5.24. StripPrefix GatewayFilter Factory
5.25. Retry GatewayFilter Factory
5.26. RequestSize GatewayFilter Factory
5.27. Modify Request Body GatewayFilter Factory
5.28. Modify Response Body GatewayFilter Factory
5.29. Default Filters
6. Global Filters
6.1. Combined Global Filter and GatewayFilter Ordering
6.2. Forward Routing Filter
6.3. LoadBalancerClient Filter
6.4. Netty Routing Filter
6.5. Netty Write Response Filter
6.6. RouteToRequestUrl Filter
6.7. Websocket Routing Filter
6.8. Gateway Metrics Filter
6.9. Marking An Exchange As Routed
7. TLS / SSL
7.1. TLS Handshake
8. Configuration
8.1. Fluent Java Routes API
8.2. DiscoveryClient Route Definition Locator
8.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
9. Reactor Netty Access Logs
10. CORS Configuration
11. Actuator API
11.1. Verbose Actuator Format
11.2. Retrieving route filters
11.2.1. Global Filters
11.2.2. Route Filters
11.3. Refreshing the route cache
11.4. Retrieving the routes defined in the gateway
11.5. Retrieving information about a particular route
11.6. Creating and deleting a particular route
11.7. Recap: list of all endpoints
12. Troubleshooting
12.1. Log Levels
12.2. Wiretap
13. Developer Guide
13.1. Writing Custom Route Predicate Factories
13.2. Writing Custom GatewayFilter Factories
13.3. Writing Custom Global Filters
13.4. Writing Custom Route Locators and Writers
14. Building a Simple Gateway Using Spring MVC or Webflux
\ No newline at end of file + Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Route Predicate Factories
4.1. After Route Predicate Factory
4.2. Before Route Predicate Factory
4.3. Between Route Predicate Factory
4.4. Cookie Route Predicate Factory
4.5. Header Route Predicate Factory
4.6. Host Route Predicate Factory
4.7. Method Route Predicate Factory
4.8. Path Route Predicate Factory
4.9. Query Route Predicate Factory
4.10. RemoteAddr Route Predicate Factory
4.10.1. Modifying the way remote addresses are resolved
5. GatewayFilter Factories
5.1. AddRequestHeader GatewayFilter Factory
5.2. AddRequestParameter GatewayFilter Factory
5.3. AddResponseHeader GatewayFilter Factory
5.4. DedupeResponseHeader GatewayFilter Factory
5.5. Hystrix GatewayFilter Factory
5.6. FallbackHeaders GatewayFilter Factory
5.7. MapRequestHeader GatewayFilter Factory
5.8. PrefixPath GatewayFilter Factory
5.9. PreserveHostHeader GatewayFilter Factory
5.10. RequestRateLimiter GatewayFilter Factory
5.10.1. Redis RateLimiter
5.11. RedirectTo GatewayFilter Factory
5.12. RemoveHopByHopHeadersFilter GatewayFilter Factory
5.13. RemoveRequestHeader GatewayFilter Factory
5.14. RemoveResponseHeader GatewayFilter Factory
5.15. RewritePath GatewayFilter Factory
5.16. RewriteLocationResponseHeader GatewayFilter Factory
5.17. RewriteResponseHeader GatewayFilter Factory
5.18. SaveSession GatewayFilter Factory
5.19. SecureHeaders GatewayFilter Factory
5.20. SetPath GatewayFilter Factory
5.21. SetRequestHeader GatewayFilter Factory
5.22. SetResponseHeader GatewayFilter Factory
5.23. SetStatus GatewayFilter Factory
5.24. StripPrefix GatewayFilter Factory
5.25. Retry GatewayFilter Factory
5.26. RequestSize GatewayFilter Factory
5.27. Modify Request Body GatewayFilter Factory
5.28. Modify Response Body GatewayFilter Factory
5.29. Default Filters
6. Global Filters
6.1. Combined Global Filter and GatewayFilter Ordering
6.2. Forward Routing Filter
6.3. LoadBalancerClient Filter
6.4. ReactiveLoadBalancerClientFilter
6.5. Netty Routing Filter
6.6. Netty Write Response Filter
6.7. RouteToRequestUrl Filter
6.8. Websocket Routing Filter
6.9. Gateway Metrics Filter
6.10. Marking An Exchange As Routed
7. TLS / SSL
7.1. TLS Handshake
8. Configuration
8.1. Fluent Java Routes API
8.2. DiscoveryClient Route Definition Locator
8.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
9. Reactor Netty Access Logs
10. CORS Configuration
11. Actuator API
11.1. Verbose Actuator Format
11.2. Retrieving route filters
11.2.1. Global Filters
11.2.2. Route Filters
11.3. Refreshing the route cache
11.4. Retrieving the routes defined in the gateway
11.5. Retrieving information about a particular route
11.6. Creating and deleting a particular route
11.7. Recap: list of all endpoints
12. Troubleshooting
12.1. Log Levels
12.2. Wiretap
13. Developer Guide
13.1. Writing Custom Route Predicate Factories
13.2. Writing Custom GatewayFilter Factories
13.3. Writing Custom Global Filters
13.4. Writing Custom Route Locators and Writers
14. Building a Simple Gateway Using Spring MVC or Webflux
\ No newline at end of file diff --git a/2.1.x/single/spring-cloud-gateway.html b/2.1.x/single/spring-cloud-gateway.html index ce478d61..1333b51c 100644 --- a/2.1.x/single/spring-cloud-gateway.html +++ b/2.1.x/single/spring-cloud-gateway.html @@ -1,6 +1,6 @@ - Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Route Predicate Factories
4.1. After Route Predicate Factory
4.2. Before Route Predicate Factory
4.3. Between Route Predicate Factory
4.4. Cookie Route Predicate Factory
4.5. Header Route Predicate Factory
4.6. Host Route Predicate Factory
4.7. Method Route Predicate Factory
4.8. Path Route Predicate Factory
4.9. Query Route Predicate Factory
4.10. RemoteAddr Route Predicate Factory
4.10.1. Modifying the way remote addresses are resolved
5. GatewayFilter Factories
5.1. AddRequestHeader GatewayFilter Factory
5.2. AddRequestParameter GatewayFilter Factory
5.3. AddResponseHeader GatewayFilter Factory
5.4. DedupeResponseHeader GatewayFilter Factory
5.5. Hystrix GatewayFilter Factory
5.6. FallbackHeaders GatewayFilter Factory
5.7. MapRequestHeader GatewayFilter Factory
5.8. PrefixPath GatewayFilter Factory
5.9. PreserveHostHeader GatewayFilter Factory
5.10. RequestRateLimiter GatewayFilter Factory
5.10.1. Redis RateLimiter
5.11. RedirectTo GatewayFilter Factory
5.12. RemoveHopByHopHeadersFilter GatewayFilter Factory
5.13. RemoveRequestHeader GatewayFilter Factory
5.14. RemoveResponseHeader GatewayFilter Factory
5.15. RewritePath GatewayFilter Factory
5.16. RewriteLocationResponseHeader GatewayFilter Factory
5.17. RewriteResponseHeader GatewayFilter Factory
5.18. SaveSession GatewayFilter Factory
5.19. SecureHeaders GatewayFilter Factory
5.20. SetPath GatewayFilter Factory
5.21. SetRequestHeader GatewayFilter Factory
5.22. SetResponseHeader GatewayFilter Factory
5.23. SetStatus GatewayFilter Factory
5.24. StripPrefix GatewayFilter Factory
5.25. Retry GatewayFilter Factory
5.26. RequestSize GatewayFilter Factory
5.27. Modify Request Body GatewayFilter Factory
5.28. Modify Response Body GatewayFilter Factory
5.29. Default Filters
6. Global Filters
6.1. Combined Global Filter and GatewayFilter Ordering
6.2. Forward Routing Filter
6.3. LoadBalancerClient Filter
6.4. Netty Routing Filter
6.5. Netty Write Response Filter
6.6. RouteToRequestUrl Filter
6.7. Websocket Routing Filter
6.8. Gateway Metrics Filter
6.9. Marking An Exchange As Routed
7. TLS / SSL
7.1. TLS Handshake
8. Configuration
8.1. Fluent Java Routes API
8.2. DiscoveryClient Route Definition Locator
8.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
9. Reactor Netty Access Logs
10. CORS Configuration
11. Actuator API
11.1. Verbose Actuator Format
11.2. Retrieving route filters
11.2.1. Global Filters
11.2.2. Route Filters
11.3. Refreshing the route cache
11.4. Retrieving the routes defined in the gateway
11.5. Retrieving information about a particular route
11.6. Creating and deleting a particular route
11.7. Recap: list of all endpoints
12. Troubleshooting
12.1. Log Levels
12.2. Wiretap
13. Developer Guide
13.1. Writing Custom Route Predicate Factories
13.2. Writing Custom GatewayFilter Factories
13.3. Writing Custom Global Filters
13.4. Writing Custom Route Locators and Writers
14. Building a Simple Gateway Using Spring MVC or Webflux

2.1.4.BUILD-SNAPSHOT

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

1. How to Include Spring Cloud Gateway

To include Spring Cloud Gateway in your project use the starter with group org.springframework.cloud + Spring Cloud Gateway

Spring Cloud Gateway


Table of Contents

1. How to Include Spring Cloud Gateway
2. Glossary
3. How It Works
4. Route Predicate Factories
4.1. After Route Predicate Factory
4.2. Before Route Predicate Factory
4.3. Between Route Predicate Factory
4.4. Cookie Route Predicate Factory
4.5. Header Route Predicate Factory
4.6. Host Route Predicate Factory
4.7. Method Route Predicate Factory
4.8. Path Route Predicate Factory
4.9. Query Route Predicate Factory
4.10. RemoteAddr Route Predicate Factory
4.10.1. Modifying the way remote addresses are resolved
5. GatewayFilter Factories
5.1. AddRequestHeader GatewayFilter Factory
5.2. AddRequestParameter GatewayFilter Factory
5.3. AddResponseHeader GatewayFilter Factory
5.4. DedupeResponseHeader GatewayFilter Factory
5.5. Hystrix GatewayFilter Factory
5.6. FallbackHeaders GatewayFilter Factory
5.7. MapRequestHeader GatewayFilter Factory
5.8. PrefixPath GatewayFilter Factory
5.9. PreserveHostHeader GatewayFilter Factory
5.10. RequestRateLimiter GatewayFilter Factory
5.10.1. Redis RateLimiter
5.11. RedirectTo GatewayFilter Factory
5.12. RemoveHopByHopHeadersFilter GatewayFilter Factory
5.13. RemoveRequestHeader GatewayFilter Factory
5.14. RemoveResponseHeader GatewayFilter Factory
5.15. RewritePath GatewayFilter Factory
5.16. RewriteLocationResponseHeader GatewayFilter Factory
5.17. RewriteResponseHeader GatewayFilter Factory
5.18. SaveSession GatewayFilter Factory
5.19. SecureHeaders GatewayFilter Factory
5.20. SetPath GatewayFilter Factory
5.21. SetRequestHeader GatewayFilter Factory
5.22. SetResponseHeader GatewayFilter Factory
5.23. SetStatus GatewayFilter Factory
5.24. StripPrefix GatewayFilter Factory
5.25. Retry GatewayFilter Factory
5.26. RequestSize GatewayFilter Factory
5.27. Modify Request Body GatewayFilter Factory
5.28. Modify Response Body GatewayFilter Factory
5.29. Default Filters
6. Global Filters
6.1. Combined Global Filter and GatewayFilter Ordering
6.2. Forward Routing Filter
6.3. LoadBalancerClient Filter
6.4. ReactiveLoadBalancerClientFilter
6.5. Netty Routing Filter
6.6. Netty Write Response Filter
6.7. RouteToRequestUrl Filter
6.8. Websocket Routing Filter
6.9. Gateway Metrics Filter
6.10. Marking An Exchange As Routed
7. TLS / SSL
7.1. TLS Handshake
8. Configuration
8.1. Fluent Java Routes API
8.2. DiscoveryClient Route Definition Locator
8.2.1. Configuring Predicates and Filters For DiscoveryClient Routes
9. Reactor Netty Access Logs
10. CORS Configuration
11. Actuator API
11.1. Verbose Actuator Format
11.2. Retrieving route filters
11.2.1. Global Filters
11.2.2. Route Filters
11.3. Refreshing the route cache
11.4. Retrieving the routes defined in the gateway
11.5. Retrieving information about a particular route
11.6. Creating and deleting a particular route
11.7. Recap: list of all endpoints
12. Troubleshooting
12.1. Log Levels
12.2. Wiretap
13. Developer Guide
13.1. Writing Custom Route Predicate Factories
13.2. Writing Custom GatewayFilter Factories
13.3. Writing Custom Global Filters
13.4. Writing Custom Route Locators and Writers
14. Building a Simple Gateway Using Spring MVC or Webflux

2.1.4.BUILD-SNAPSHOT

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

1. How to Include Spring Cloud Gateway

To include Spring Cloud Gateway in your project use the starter with group org.springframework.cloud and artifact id spring-cloud-starter-gateway. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

If you include the starter, but, for some reason, you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.

[Important]Important

Spring Cloud Gateway is built upon Spring Boot 2.x, Spring WebFlux, @@ -589,7 +589,31 @@ the scheme specified in the request made to the Gateway. For example, if the re but the ServiceInstance indicates it is not secure, then the downstream request will be made over HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the -route URL will override the ServiceInstance configuration.

6.4 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

6.5 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

6.6 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute`.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

6.7 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml.  +route URL will override the ServiceInstance configuration.

[Warning]Warning

LoadBalancerClientFilter uses a blocking Ribbon LoadBalancerClient under the hood. +We suggest you use ReactiveLoadBalancerClientFilter instead. +You can switch to using it by adding org.springframework.cloud:spring-cloud-loadbalancer dependency to your project +and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false.

6.4 ReactiveLoadBalancerClientFilter

The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute +ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), +it will use the Spring Cloud ReactorLoadBalancer to resolve the name (myservice in the previous example) +to an actual host and port and replace the URI in the same attribute. The unmodified +original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. +The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals +lb and then the same rules apply.

application.yml.  +

spring:
+  cloud:
+    gateway:
+      routes:
+      - id: myRoute
+        uri: lb://service
+        predicates:
+        - Path=/service/**

+

[Note]Note

By default when a service instance cannot be found by the ReactorLoadBalancer, a 503 will be returned. +You can configure the Gateway to return a 404 by setting spring.cloud.gateway.loadbalancer.use404=true.

[Note]Note

The isSecure value of the ServiceInstance returned from the ReactiveLoadBalancerClientFilter will override +the scheme specified in the request made to the Gateway. For example, if the request comes into the Gateway over HTTPS +but the ServiceInstance indicates it is not secure, then the downstream request will be made over +HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the +route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the +route URL will override the ServiceInstance configuration.

6.5 Netty Routing Filter

The Netty Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a http or https scheme. It uses the Netty HttpClient to make the downstream proxy request. The response is put in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute for use in a later filter. (There is an experimental WebClientHttpRoutingFilter that performs the same function, but does not require netty)

6.6 Netty Write Response Filter

The NettyWriteResponseFilter runs if there is a Netty HttpClientResponse in the ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR exchange attribute. It is run after all other filters have completed and writes the proxy response back to the gateway client response. (There is an experimental WebClientWriteResponseFilter that performs the same function, but does not require netty)

6.7 RouteToRequestUrl Filter

The RouteToRequestUrlFilter runs if there is a Route object in the ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR exchange attribute. It creates a new URI, based off of the request URI, but updated with the URI attribute of the Route object. The new URI is placed in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute`.

If the URI has a scheme prefix, such as lb:ws://serviceid, the lb scheme is stripped from the URI and placed in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR for use later in the filter chain.

6.8 Websocket Routing Filter

The Websocket Routing Filter runs if the url located in the ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR exchange attribute has a ws or wss scheme. It uses the Spring Web Socket infrastructure to forward the Websocket request downstream.

Websockets may be load-balanced by prefixing the URI with lb, such as lb:ws://serviceid.

[Note]Note

If you are using SockJS as a fallback over normal http, you should configure a normal HTTP route as well as the Websocket Route.

application.yml. 

spring:
   cloud:
     gateway:
@@ -604,7 +628,7 @@ route URL will override the ServiceInstance configu
         uri: ws://localhost:3001
         predicates:
         - Path=/websocket/**

-

6.8 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

  • routeId: The route id
  • routeUri: The URI that the API will be routed to
  • outcome: Outcome as classified by HttpStatus.Series
  • status: Http Status of the request returned to the client
  • httpStatusCode: Http Status of the request returned to the client
  • httpMethod: The Http method used for the request

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

6.9 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted +

6.9 Gateway Metrics Filter

To enable Gateway Metrics add spring-boot-starter-actuator as a project dependency. Then, by default, the Gateway Metrics Filter runs as long as the property spring.cloud.gateway.metrics.enabled is not set to false. This filter adds a timer metric named "gateway.requests" with the following tags:

  • routeId: The route id
  • routeUri: The URI that the API will be routed to
  • outcome: Outcome as classified by HttpStatus.Series
  • status: Http Status of the request returned to the client
  • httpStatusCode: Http Status of the request returned to the client
  • httpMethod: The Http method used for the request

These metrics are then available to be scraped from /actuator/metrics/gateway.requests and can be easily integrated with Prometheus to create a Grafana dashboard.

[Note]Note

To enable the prometheus endpoint add micrometer-registry-prometheus as a project dependency.

6.10 Marking An Exchange As Routed

After the Gateway has routed a ServerWebExchange it will mark that exchange as "routed" by adding gatewayAlreadyRouted to the exchange attributes. Once a request has been marked as routed, other routing filters will not route the request again, essentially skipping the filter. There are convenience methods that you can use to mark an exchange as routed or check if an exchange has already been routed.

  • ServerWebExchangeUtils.isAlreadyRouted takes a ServerWebExchange object and checks if it has been "routed"
  • ServerWebExchangeUtils.setAlreadyRouted takes a ServerWebExchange object and marks it as "routed"

7. TLS / SSL

The Gateway can listen for requests on https by following the usual Spring server configuration. Example:

application.yml.  diff --git a/2.1.x/spring-cloud-gateway.xml b/2.1.x/spring-cloud-gateway.xml index cf075e9c..eae70d17 100644 --- a/2.1.x/spring-cloud-gateway.xml +++ b/2.1.x/spring-cloud-gateway.xml @@ -1322,6 +1322,47 @@ but the ServiceInstance indicates it is not secure, then the route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the route URL will override the ServiceInstance configuration. + +LoadBalancerClientFilter uses a blocking Ribbon LoadBalancerClient under the hood. +We suggest you use ReactiveLoadBalancerClientFilter instead. +You can switch to using it by adding org.springframework.cloud:spring-cloud-loadbalancer dependency to your project +and setting the value of the spring.cloud.loadbalancer.ribbon.enabled to false. + + +

+ReactiveLoadBalancerClientFilter +The ReactiveLoadBalancerClientFilter looks for a URI in the exchange attribute +ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a lb scheme (ie lb://myservice), +it will use the Spring Cloud ReactorLoadBalancer to resolve the name (myservice in the previous example) +to an actual host and port and replace the URI in the same attribute. The unmodified +original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute. +The filter will also look in the ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR attribute to see if it equals +lb and then the same rules apply. + +application.yml + +spring: + cloud: + gateway: + routes: + - id: myRoute + uri: lb://service + predicates: + - Path=/service/** + + + +By default when a service instance cannot be found by the ReactorLoadBalancer, a 503 will be returned. +You can configure the Gateway to return a 404 by setting spring.cloud.gateway.loadbalancer.use404=true. + + +The isSecure value of the ServiceInstance returned from the ReactiveLoadBalancerClientFilter will override +the scheme specified in the request made to the Gateway. For example, if the request comes into the Gateway over HTTPS +but the ServiceInstance indicates it is not secure, then the downstream request will be made over +HTTP. The opposite situation can also apply. However if GATEWAY_SCHEME_PREFIX_ATTR is specified for the +route in the Gateway configuration, the prefix will be stripped and the resulting scheme from the +route URL will override the ServiceInstance configuration. +
Netty Routing Filter