Spring Cloud GatewaySpring Cloud Gateway5.0.0

該專案提供了一個庫,用於在 Spring 之上構建 API 閘道器。Spring Cloud Gateway 旨在提供一種簡單而有效的方式來路由到 API,併為它們提供橫切關注點,例如:安全性、監控/指標和彈性。

特性

Spring Cloud Gateway 功能

  • 基於 Spring Framework 和 Spring Boot 構建
  • 相容 Spring WebFlux 和 Spring Web MVC
  • 能夠匹配任何請求屬性上的路由。
  • 斷言和過濾器特定於路由。
  • Spring Cloud Circuit Breaker 整合。
  • 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();
	}
}

要執行您自己的 Gateway Server 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
    }
}

要執行您自己的 Gateway Server WebMVC,請使用 spring-cloud-starter-gateway-server-webmvc 依賴項。

Spring Initializr

快速啟動您的專案

領先一步

VMware 提供培訓和認證,助您加速進步。

瞭解更多

獲得支援

Tanzu Spring 提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案,只需一份簡單的訂閱。

瞭解更多

即將舉行的活動

檢視 Spring 社群所有即將舉行的活動。

檢視所有