该项目提供了一个库,用于在 Spring 之上构建 API 网关。Spring Cloud Gateway 旨在提供一种简单而有效的路径,用于路由到 API,并针对它们提出跨领域的关注点,如安全性、监控/指标和弹性。
特征
Spring Cloud Gateway 功能:
- 基于 Spring Framework 和 Spring Boot 构建
- 兼容 Spring WebFlux 和 Spring Web MVC
- 能够匹配任何请求属性的路由。
- 谓词和过滤器是特定于路由的。
- 春季云断路器集成。
- Spring Cloud DiscoveryClient 集成
- 易写谓词与过滤器
- 请求速率限制
- 路径重写
如何开始使用 Spring Cloud Gateway Server WebFlux
@SpringBootApplication
public class DemogatewayApplication {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/get")
.uri("https://httpbin.org"))
.route("host_route", r -> r.host("*.myhost.org")
.uri("https://httpbin.org"))
.route("rewrite_route", r -> r.host("*.rewrite.org")
.filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
.uri("https://httpbin.org"))
.route("circuit_breaker_route", r -> r.host("*.circuitbreaker.org")
.filters(f -> f.circuitBreaker(c -> c.setName("slowcmd")))
.uri("https://httpbin.org"))
.route("circuit_breaker_fallback_route", r -> r.host("*.circuitbreakerfallback.org")
.filters(f -> f.circuitBreaker(c -> c.setName("slowcmd").setFallbackUri("forward:/circuitbrekerfallback")))
.uri("https://httpbin.org"))
.route("limit_route", r -> r
.host("*.limited.org").and().path("/anything/**")
.filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
.uri("https://httpbin.org"))
.build();
}
}
要运行自己的网关服务器,WebFlux 可以使用依赖。spring-cloud-starter-gateway-server-webflux
开始使用 Spring Cloud Gateway Server Web MVC
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.*;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.*;
//...
@SpringBootApplication
public class DemogatewayApplication {
@Bean
public RouterFunction<ServerResponse> customRoutes() {
// @formatter:off
return route("path_route")
.GET("/get", http())
.before(uri("https://httpbin.org"))
.build().and(route("host_route")
.route(host("*.myhost.org"), http())
.before(uri("https://httpbin.org"))
.build().and(route("rewrite_route")
.route(host("*.rewrite.org"), http())
.before(uri("https://httpbin.org"))
.before(rewritePath("/foo/(?<segment>.*)", "/${segment}"))
.build().and(route("circuitbreaker_route")
.route(host("*.circuitbreaker.org"), http())
.before(uri("https://httpbin.org"))
.filter(circuitBreaker("slowcmd"))
.build().and(route("circuitbreaker_fallback_route")
.route(host("*.circuitbreakerfallback.org"), http())
.before(uri("https://httpbin.org"))
.filter(circuitBreaker(c -> c.setId("slowcmd").setFallbackUri("forward:/fallback")))
.build().and(route("limit_route")
.route(host("*.limited.org").and(path("/anything/**")), http())
.before(uri("https://httpbin.org"))
.filter(rateLimit(c -> c.setCapacity(10).setPeriod(Duration.ofSeconds(1)).setKeyResolver(request ->
request.headers().firstHeader("X-TokenId"))))
.build())))));
// @formatter:on
}
}
要运行自己的网关服务器 WebMVC,请使用依赖关系。spring-cloud-starter-gateway-server-webmvc