Spring
Cloud
Gateway
@spencerbgibb
https://slides.com/spencer/spring-cloud-gateway/live
@sreetummidi
1 . 2
Spencer Gibb & Sree Tummidi
2
What is an
API Gateway?
http://microservices.io/patterns/apigateway.html
3
Server Side
MVC App
Single Page
JS App
or
Mobile App APIGateway
Product Service
Recommendation
Service
User Service
4
Responsibilities
Routing
Resiliency
Monolith Strangling
Security
Monitoring
Surgical Routing
Canarying
Flexibility
5
Gateway Types
6
Appliance
7
SAAS
8
Web Server
9
Mesh
10
Developer-Oriented
11
Zuul 1
https://www.youtube.com/watch?v=mHHHpxJuTAo
12
Zuul 2?
https://medium.com/netflix-techblog/zuul-2-the-netflix-journey-to-asynchronous-non-blocking-systems-45947377fb5c
https://twitter.com/agonigberg/status/914895239042740225​
https://github.com/strangeloop/StrangeLoop2017/blob/master/slides/ArthurGonigberg-ZuulsJourneyToNonBlocking.pdf
13
Spring Cloud Gateway
14
Foundations
Spring 5
Reactor
Spring Boot 2
15
Reactive
Project
Reactor
16
Non-blocking
17
Spring WebFlux
HandlerMapping
WebFilter
Predicate
ServerWebExchange
PathPatternMatcher/PathMatchInfo
18
RoutePredicateHandlerMapping
Plugs into Spring WebFlux HandlerMapping Chain
Predicates are scoped to a specific Route
Loaded from
​​Configuration
Repository
Java fluent interface
19
RoutePredicateFactory
Path
Host
Header
Parameter
Any request data
20
FilteringWebHandler
Modeled after Spring WebFlux
Uses GatewayFilter
Filters scoped to specific Route
Filters can be both 'pre' and 'post'
GlobalFilter
Filter AAA
Filter BBB
Filter CCC
21
GatewayFilterFactory
RewritePath
{Add|Remove}{Request|Response}Header
Requeset RateLimiter
Hystrix
Spring Cloud LoadBalancerClient*
WebSocket*
Netty Routing*
* (Global Filters)
22
Benefits
23
Zuul Filters pre-Finchley
Hystrix+Ribbon+Spring Retry
24
Simple Filters
public class SetStatusWebFilterFactory implements GatewayFilterFactory {
@Override
public WebFilter apply(Tuple args) {
String status = args.getRawString("status");
final HttpStatus httpStatus = HttpStatus.valueOf(status);
return (exchange, chain) -> chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
setResponseStatus(exchange, httpStatus);
}));
}
}
25
Route Configuration Now: 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}
26
Route Configuration: Java
@Bean
public RouteLocator myRouteLocator(RouteLocatorBuilder builder,
ThrottleGatewayFilterFactory throttle) {
return builder.routes()
.route(r -> r.host("**.abc.org").and().path("/image/png")
.addResponseHeader("X-TestHeader", "foobar")
.uri("http://httpbin.org:80")
)
.route(r -> r.path("/image/webp")
.addResponseHeader("X-AnotherHeader", "baz")
.uri("http://httpbin.org:80")
)
.route(r -> r.order(-1)
.predicate(host("**.throttle.org").and(path("/get")))
.add(throttle.apply(1, 1, 10, TimeUnit.SECONDS))
.uri("http://httpbin.org:80")
)
.build();
}
27
DEMO
28
Questions?
@spencerbgibb
http://slides.com/spencer/spring-cloud-gateway
@sreetummidi
https://springoneplatform.io/sessions/cloud-foundry-uaa-
as-an-identity-gateway - Thursday - 11:50 - Room 2004
29

Spring Cloud Gateway