Craig Walls
Twitter: @habuma
Spring Cloud
Gateway
Who is Craig?
API Gateway Pattern
The Mighty Monolith
The Monolithic API
Busting the Monolith
The Gateway
Gateway
Responsibilities
Routing
Surgical Routing
Canary Routing
Monolith Strangling
Security
Resiliency
Monitoring
Flexibility
What about Zuul?
Zuul 1
Based on blocking servlet API
Unable to do SSE or WebSocket
Limited to path-based routing
Proprietary features
What about Zuul?
Zuul 2
Non-blocking I/O based on RxNetty
Supports HTTP/2 and WebSocket
Not backward compatible with Zuul 1
Proprietary features
Spring Cloud
Gateway
Spring Cloud Gateway
Based on Spring 5, Reactor, Boot 2
Routes can be configured in Java, configuration,
or with repositories
Can route on path, host, headers, parameters, or
anything in the request
Filters
Born in Colorado!
Out-of-the-Box Filters
Rewrite Path
Add/Remove Request/Response Headers
Rate Limiting
Hystrix
Spring Cloud LoadBalancerClient*
WebSocket*
Netty Routing*
* Global
Simple Filter
@Slf4j
public class SimpleFilter implements GatewayFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("BEFORE");
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
log.info("AFTER");
}));
}
}
Route Config: YAML
spring:
cloud:
gateway:
- id: foo_route
uri: lb://foo
predicates:
- Host=**.foo.org
- Path=/headers
- Method=GET
- Header=X-Request-Id, d+
- Query=foo, ba.
- Query=baz
- Cookie=chocolate, ch.p
- After=1900-01-20T17:42:47.789-07:00[America/Denver]
filters:
- AddRequestHeader=X-Request-Foo, Bar
- AddResponseHeader=X-Response-Foo, Bar
- Hystrix=foo
- SecureHeaders
- RewritePath=/foo/(?<segment>.*), /${segment}
Route Config: Java
@Bean
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(r -> r.path("/image/webp")
.filters(f -> f.addResponseHeader("X-AnotherHeader", "baz"))
.uri("http://httpbin.org:80")
)
.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();
}
DEMO
Thank you!
Craig Walls
Twitter: @habuma

SpringOne Tour Denver - Spring Cloud Gateway