diff --git a/multi/multi__global_filters.html b/multi/multi__global_filters.html index 61e6956d..2e29dc31 100644 --- a/multi/multi__global_filters.html +++ b/multi/multi__global_filters.html @@ -1,6 +1,39 @@
-The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).
The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.
The LoadBalancerClientFilter 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 LoadBalancerClient 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. +
The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).
When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of GlobalFilter and all route specific instances of GatewayFilter to a filter chain. This combined filter chain is sorted by the org.springframework.core.Ordered interface, which can be set by implementing the getOrder() method or by using the @Order annotation.
As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How It Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase.
ExampleConfiguration.java. +
@Bean +@Order(-1) +public GlobalFilter a() { + return (exchange, chain) -> { + log.info("first pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("third post filter"); + })); + }; +} + +@Bean +@Order(0) +public GlobalFilter b() { + return (exchange, chain) -> { + log.info("second pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("second post filter"); + })); + }; +} + +@Bean +@Order(1) +public GlobalFilter c() { + return (exchange, chain) -> { + log.info("third pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("first post filter"); + })); + }; +}
+
The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.
The LoadBalancerClientFilter 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 LoadBalancerClient 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: diff --git a/single/spring-cloud-gateway.html b/single/spring-cloud-gateway.html index 1298904a..d0dcc2b4 100644 --- a/single/spring-cloud-gateway.html +++ b/single/spring-cloud-gateway.html @@ -333,7 +333,40 @@ using something like args: retries: 3 statuses: BAD_GATEWAY
-
![]() | Note |
|---|---|
At this time a URI using the |
The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).
The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.
The LoadBalancerClientFilter 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 LoadBalancerClient 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. +
![]() | Note |
|---|---|
At this time a URI using the |
The GlobalFilter interface has the same signature as GatewayFilter. These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).
When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of GlobalFilter and all route specific instances of GatewayFilter to a filter chain. This combined filter chain is sorted by the org.springframework.core.Ordered interface, which can be set by implementing the getOrder() method or by using the @Order annotation.
As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How It Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase.
ExampleConfiguration.java. +
@Bean +@Order(-1) +public GlobalFilter a() { + return (exchange, chain) -> { + log.info("first pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("third post filter"); + })); + }; +} + +@Bean +@Order(0) +public GlobalFilter b() { + return (exchange, chain) -> { + log.info("second pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("second post filter"); + })); + }; +} + +@Bean +@Order(1) +public GlobalFilter c() { + return (exchange, chain) -> { + log.info("third pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("first post filter"); + })); + }; +}
+
The ForwardRoutingFilter looks for a URI in the exchange attribute ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR. If the url has a forward scheme (ie forward:///localendpoint), it will use the Spring DispatcherHandler to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR attribute.
The LoadBalancerClientFilter 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 LoadBalancerClient 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: diff --git a/spring-cloud-gateway.xml b/spring-cloud-gateway.xml index 6be827d2..df0b4252 100644 --- a/spring-cloud-gateway.xml +++ b/spring-cloud-gateway.xml @@ -844,7 +844,45 @@ using something like SThe GlobalFilter interface has the same signature asGatewayFilter . These are special filters that are conditionally applied to all routes. (This interface and usage are subject to change in future milestones).Combined Global Filter and GatewayFilter Ordering -TODO: document ordering +When a request comes in (and matches a Route) the Filtering Web Handler will add all instances of +GlobalFilter and all route specific instances ofGatewayFilter to a filter chain. This combined filter chain is sorted by theorg.springframework.core.Ordered interface, which can be set by implementing thegetOrder() method or by using the@Order annotation.As Spring Cloud Gateway distinguishes between "pre" and "post" phases for filter logic execution (see: How It Works), the filter with the highest precedence will be the first in the "pre"-phase and the last in the "post"-phase. ++ ExampleConfiguration.java ++ +@Bean +@Order(-1) +public GlobalFilter a() { + return (exchange, chain) -> { + log.info("first pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("third post filter"); + })); + }; +} + +@Bean +@Order(0) +public GlobalFilter b() { + return (exchange, chain) -> { + log.info("second pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("second post filter"); + })); + }; +} + +@Bean +@Order(1) +public GlobalFilter c() { + return (exchange, chain) -> { + log.info("third pre filter"); + return chain.filter(exchange).then(Mono.fromRunnable(() -> { + log.info("first post filter"); + })); + }; +} +Forward Routing Filter