diff --git a/multi/multi__configuration.html b/multi/multi__configuration.html index 275d6896..854be6b8 100644 --- a/multi/multi__configuration.html +++ b/multi/multi__configuration.html @@ -22,24 +22,26 @@

For some usages of the gateway, properties will be adequate, but some production use cases will benefit from loading configuration from an external source, such as a database. Future milestone versions will have RouteDefinitionLocator implementations based off of Spring Data Repositories such as: Redis, MongoDB and Cassandra.

7.1 Fluent Java Routes API

To allow for simple configuration in Java, there is a fluent API defined in the Routes class.

GatewaySampleApplication.java. 

// static imports from GatewayFilters and RoutePredicates
 @Bean
-public RouteLocator customRouteLocator(ThrottleGatewayFilterFactory throttle) {
-    return Routes.locator()
-            .route("test")
-                .predicate(host("**.abc.org").and(path("/image/png")))
-                .addResponseHeader("X-TestHeader", "foobar")
+public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
+    return builder.routes()
+            .route(r -> r.host("**.abc.org").and().path("/image/png")
+                .filters(f ->
+                        f.addResponseHeader("X-TestHeader", "foobar"))
                 .uri("http://httpbin.org:80")
-            .route("test2")
-                .predicate(path("/image/webp"))
-                .add(addResponseHeader("X-AnotherHeader", "baz"))
+            )
+            .route(r -> r.path("/image/webp")
+                .filters(f ->
+                        f.addResponseHeader("X-AnotherHeader", "baz"))
                 .uri("http://httpbin.org:80")
-            .route("test3")
-                .order(-1)
-                .predicate(host("**.throttle.org").and(path("/get")))
-                .add(throttle.apply(tuple().of("capacity", 1,
-                     "refillTokens", 1,
-                     "refillPeriod", 10,
-                     "refillUnit", "SECONDS")))
+            )
+            .route(r -> r.order(-1)
+                .host("**.throttle.org").and().path("/get")
+                .filters(f -> f.filter(throttle.apply(1,
+                        1,
+                        10,
+                        TimeUnit.SECONDS)))
                 .uri("http://httpbin.org:80")
+            )
             .build();
 }

This style also allows for more custom predicate assertions. The predicates defined by RouteDefinitionLocator beans are combined using logical and. By using the fluent Java API, you can use the and(), or() and negate() operators on the Predicate class.

7.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

\ No newline at end of file diff --git a/single/spring-cloud-gateway.html b/single/spring-cloud-gateway.html index 0c2ee9c4..c8671a40 100644 --- a/single/spring-cloud-gateway.html +++ b/single/spring-cloud-gateway.html @@ -303,24 +303,26 @@ using something like RouteDefinitionLocator implementations based off of Spring Data Repositories such as: Redis, MongoDB and Cassandra.

7.1 Fluent Java Routes API

To allow for simple configuration in Java, there is a fluent API defined in the Routes class.

GatewaySampleApplication.java. 

// static imports from GatewayFilters and RoutePredicates
 @Bean
-public RouteLocator customRouteLocator(ThrottleGatewayFilterFactory throttle) {
-    return Routes.locator()
-            .route("test")
-                .predicate(host("**.abc.org").and(path("/image/png")))
-                .addResponseHeader("X-TestHeader", "foobar")
+public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
+    return builder.routes()
+            .route(r -> r.host("**.abc.org").and().path("/image/png")
+                .filters(f ->
+                        f.addResponseHeader("X-TestHeader", "foobar"))
                 .uri("http://httpbin.org:80")
-            .route("test2")
-                .predicate(path("/image/webp"))
-                .add(addResponseHeader("X-AnotherHeader", "baz"))
+            )
+            .route(r -> r.path("/image/webp")
+                .filters(f ->
+                        f.addResponseHeader("X-AnotherHeader", "baz"))
                 .uri("http://httpbin.org:80")
-            .route("test3")
-                .order(-1)
-                .predicate(host("**.throttle.org").and(path("/get")))
-                .add(throttle.apply(tuple().of("capacity", 1,
-                     "refillTokens", 1,
-                     "refillPeriod", 10,
-                     "refillUnit", "SECONDS")))
+            )
+            .route(r -> r.order(-1)
+                .host("**.throttle.org").and().path("/get")
+                .filters(f -> f.filter(throttle.apply(1,
+                        1,
+                        10,
+                        TimeUnit.SECONDS)))
                 .uri("http://httpbin.org:80")
+            )
             .build();
 }

This style also allows for more custom predicate assertions. The predicates defined by RouteDefinitionLocator beans are combined using logical and. By using the fluent Java API, you can use the and(), or() and negate() operators on the Predicate class.

7.2 DiscoveryClient Route Definition Locator

The Gateway can be configured to create routes based on services registered with a DiscoveryClient compatible service registry.

To enable this, set spring.cloud.gateway.discovery.locator.enabled=true and make sure a DiscoveryClient implementation is on the classpath and enabled (such as Netflix Eureka, Consul or Zookeeper).

8. Actuator API

TODO: document the /gateway actuator endpoint

9. Developer Guide

TODO: overview of writing custom integrations

9.1 Writing Custom Route Predicate Factories

TODO: document writing Custom Route Predicate Factories

9.2 Writing Custom GatewayFilter Factories

TODO: document writing Custom GatewayFilter Factories

9.3 Writing Custom Global Filters

TODO: document writing Custom Global Filters

9.4 Writing Custom Route Locators and Writers

TODO: document writing Custom Route Locators and Writers

10. Building a Simple Gateway Using Spring MVC

Spring Cloud Gateway provides a utility object called ProxyExchange which you can use inside a regular Spring MVC handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs, or forwarding to a local handler via the forward() method.

Example (proxying a request to "/test" downstream to a remote server):

@RestController
diff --git a/spring-cloud-gateway.xml b/spring-cloud-gateway.xml
index 104a438a..6b851fa1 100644
--- a/spring-cloud-gateway.xml
+++ b/spring-cloud-gateway.xml
@@ -758,24 +758,26 @@ using something like S
 
 // static imports from GatewayFilters and RoutePredicates
 @Bean
-public RouteLocator customRouteLocator(ThrottleGatewayFilterFactory throttle) {
-    return Routes.locator()
-            .route("test")
-                .predicate(host("**.abc.org").and(path("/image/png")))
-                .addResponseHeader("X-TestHeader", "foobar")
+public RouteLocator customRouteLocator(RouteLocatorBuilder builder, ThrottleGatewayFilterFactory throttle) {
+    return builder.routes()
+            .route(r -> r.host("**.abc.org").and().path("/image/png")
+                .filters(f ->
+                        f.addResponseHeader("X-TestHeader", "foobar"))
                 .uri("http://httpbin.org:80")
-            .route("test2")
-                .predicate(path("/image/webp"))
-                .add(addResponseHeader("X-AnotherHeader", "baz"))
+            )
+            .route(r -> r.path("/image/webp")
+                .filters(f ->
+                        f.addResponseHeader("X-AnotherHeader", "baz"))
                 .uri("http://httpbin.org:80")
-            .route("test3")
-                .order(-1)
-                .predicate(host("**.throttle.org").and(path("/get")))
-                .add(throttle.apply(tuple().of("capacity", 1,
-                     "refillTokens", 1,
-                     "refillPeriod", 10,
-                     "refillUnit", "SECONDS")))
+            )
+            .route(r -> r.order(-1)
+                .host("**.throttle.org").and().path("/get")
+                .filters(f -> f.filter(throttle.apply(1,
+                        1,
+                        10,
+                        TimeUnit.SECONDS)))
                 .uri("http://httpbin.org:80")
+            )
             .build();
 }