Spring

Cloud Gateway

http://gibb.tech

@spencerbgibb

https://slides.com/spencer/spring-cloud-gateway/live

Spring

Cloud Gateway

http://ryanjbaxter.com

@ryanjbaxter

https://slides.com/spencer/spring-cloud-gateway/live

Spring

Cloud Gateway

@spencerbgibb

https://slides.com/spencer/spring-cloud-gateway/live

@sreetummidi

The API Gateway

Is Dead

Long Live The

API Gateway

http://spencer.gibb.us

@spencerbgibb

https://slides.com/spencer/spring-cloud-gateway/live

Spencer Gibb

Ryan Baxter

What is an
API Gateway?

http://microservices.io/patterns/apigateway.html

Server Side

MVC App

Single Page

JS App

or

Mobile App

API Gateway

Product Service

Recommendation Service

User Service

Responsibilities

  • Routing
  • Surgical Routing
  • Canarying
  • Resiliency
  • Monolith Strangling
  • Security
  • Monitoring
  • Flexibility

Gateway Types

Appliance

SAAS

Web Server

Mesh

Developer-Oriented

Zuul 1

https://www.youtube.com/watch?v=mHHHpxJuTAo

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

Spring Cloud Gateway

Foundations

  • Spring 5
  • Reactor
  • Spring Boot 2

Reactive

Project

Reactor

Non-blocking

Spring WebFlux

  • HandlerMapping
  • WebFilter
  • Predicate
  • ServerWebExchange
  • PathPatternMatcher/PathMatchInfo

RoutePredicateHandlerMapping

  • Plugs into Spring WebFlux HandlerMapping Chain
  • Predicates are scoped to a specific Route
  • Loaded from
    • ​​Configuration
    • Repository
    • Java fluent interface

RoutePredicateFactory

  • Path
  • Host
  • Header
  • Parameter
  • Any request data

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

GatewayFilterFactory

  • RewritePath
  • {Add|Remove}{Request|Response}Header
  • Request RateLimiter
  • Hystrix
  • Spring Cloud LoadBalancerClient*
  • WebSocket*
  • Netty Routing*

* (Global Filters)

Benefits

Zuul Filters pre-Finchley

Hystrix+Ribbon+Spring Retry

Simple Filters

public class SetStatusWebFilterFactory implements GatewayFilterFactory {

	@Override
	public GatewayFilter apply(Config config) {
		final HttpStatus status = parse(config.status);

		return (exchange, chain) -> chain.filter(exchange)
			.then(Mono.fromRunnable(() -> {
				setResponseStatus(exchange, httpStatus);
		}));
	}

}

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}

Route Configuration: 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

Questions?

@spencerbgibb

http://slides.com/spencer/spring-cloud-gateway

https://github.com/spring-cloud/spring-cloud-gateway

https://github.com/spring-cloud-samples/spring-cloud-gateway-sample

Questions?

@ryanjbaxter

http://slides.com/spencer/spring-cloud-gateway

https://github.com/spring-cloud/spring-cloud-gateway

https://github.com/spring-cloud-samples/spring-cloud-gateway-sample

Spring Cloud Gateway

By Spencer Gibb

Spring Cloud Gateway

  • 9,502