微服务⽹关Spring Cloud Gateway
单块应⽤时代单块+API时代微服务?微服务+⽹关⽹关职责关于Zuul1基于blocking servlet API不⽀持SSE或者WebSocket路由灵活度⼀般Netflix专有功能Spring Cloud Gateway基于Spring, Reactor, Boot 2⽀持基于配置或Java代码⽅式的灵活路由路由⽀持pat...
·
单块应⽤时代
单块+API时代
微服务?
微服务+⽹关
⽹关职责
关于Zuul1
基于blocking servlet API
不⽀持SSE或者WebSocket
路由灵活度⼀般
Netflix专有功能
Spring Cloud Gateway
基于Spring, Reactor, Boot 2
⽀持基于配置或Java代码⽅式的灵活路由
路由⽀持path/host/header/parameters等
⽀持过滤器
https://cloud.spring.io/spring-cloud-gateway/spring-cloud-gateway.html
架构
简单过滤器
@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");
}));
}
}
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}
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();
}
更多推荐
已为社区贡献2条内容
所有评论(0)