From ff386b0047985058be691a0b3d505eb7865be571 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Tue, 15 Oct 2019 14:58:21 +0000 Subject: [PATCH] Sync docs from 2.1.x to gh-pages --- 2.1.x/multi/multi__global_filters.html | 42 ++++++++---------------- 2.1.x/single/spring-cloud-gateway.html | 42 ++++++++---------------- 2.1.x/spring-cloud-gateway.xml | 44 +++++++++----------------- 3 files changed, 43 insertions(+), 85 deletions(-) diff --git a/2.1.x/multi/multi__global_filters.html b/2.1.x/multi/multi__global_filters.html index c133c39e..7640129f 100644 --- a/2.1.x/multi/multi__global_filters.html +++ b/2.1.x/multi/multi__global_filters.html @@ -1,37 +1,23 @@ - 6. Global Filters

6. Global Filters

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).

6.1 Combined Global Filter and GatewayFilter 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 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.  + 6. Global Filters

6. Global Filters

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).

6.1 Combined Global Filter and GatewayFilter 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 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.

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");
-        }));
-    };
+public GlobalFilter customFilter() {
+    return CustomGlobalFilter();
 }
 
-@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");
-        }));
-    };
-}
+public class CustomGlobalFilter implements GlobalFilter, Ordered {
 
-@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");
-        }));
-    };
+    @Override
+    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+        log.info("custom global filter");
+        return chain.filter(exchange);
+    }
+
+    @Override
+    public int getOrder() {
+        return -1;
+    }
 }

6.2 Forward Routing 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.

6.3 LoadBalancerClient Filter

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:
diff --git a/2.1.x/single/spring-cloud-gateway.html b/2.1.x/single/spring-cloud-gateway.html
index 487ff34b..ce478d61 100644
--- a/2.1.x/single/spring-cloud-gateway.html
+++ b/2.1.x/single/spring-cloud-gateway.html
@@ -555,38 +555,24 @@ This property takes a list of filters

application.yml.  default-filters: - AddResponseHeader=X-Response-Default-Foo, Default-Bar - PrefixPath=/httpbin

-

6. Global Filters

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).

6.1 Combined Global Filter and GatewayFilter 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 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.  +

6. Global Filters

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).

6.1 Combined Global Filter and GatewayFilter 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 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.

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");
-        }));
-    };
+public GlobalFilter customFilter() {
+    return CustomGlobalFilter();
 }
 
-@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");
-        }));
-    };
-}
+public class CustomGlobalFilter implements GlobalFilter, Ordered {
 
-@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");
-        }));
-    };
+    @Override
+    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+        log.info("custom global filter");
+        return chain.filter(exchange);
+    }
+
+    @Override
+    public int getOrder() {
+        return -1;
+    }
 }

6.2 Forward Routing 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.

6.3 LoadBalancerClient Filter

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:
diff --git a/2.1.x/spring-cloud-gateway.xml b/2.1.x/spring-cloud-gateway.xml
index 2ffffa54..b3b37b37 100644
--- a/2.1.x/spring-cloud-gateway.xml
+++ b/2.1.x/spring-cloud-gateway.xml
@@ -1264,42 +1264,28 @@ This property takes a list of filters
 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).
 
Combined Global Filter and GatewayFilter 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 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. +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. +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"); - })); - }; +public GlobalFilter customFilter() { + return CustomGlobalFilter(); } -@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"); - })); - }; -} +public class CustomGlobalFilter implements GlobalFilter, Ordered { -@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"); - })); - }; + @Override + public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { + log.info("custom global filter"); + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return -1; + } }