Spring Cloud GatewaySpring クラウドゲートウェイ 4.3.1

このプロジェクトは、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 サーバー 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 サーバー 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.orb"))
            .build().and(route("host_route")
                .route(host("*.myhost.org"), http())
                .before(uri("https://httpbin.orb"))
            .build().and(route("rewrite_route")
                .route(host("*.rewrite.org"), http())
                .before(uri("https://httpbin.orb"))
                .before(rewritePath("/foo/(?<segment>.*)", "/${segment}"))
            .build().and(route("circuitbreaker_route")
                .route(host("*.circuitbreaker.org"), http())
                .before(uri("https://httpbin.orb"))
                .filter(circuitBreaker("slowcmd"))
            .build().and(route("circuitbreaker_fallback_route")
                .route(host("*.circuitbreakerfallback.org"), http())
                .before(uri("https://httpbin.orb"))
                .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.orb"))
                .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

プロジェクトのクイックスタート

サポートの取得

Tanzu Spring は、1 つのシンプルなサブスクリプションで OpenJDK ™、Spring、Apache Tomcat ® のサポートとバイナリを提供します。

さらに学習したい方に (英語)

今後のイベント

Spring コミュニティで今後開催されるすべてのイベントをチェックしてください。

すべて表示 (英語)