javascript
Spring Cloud Gateway –配置简单路由
可以將Spring Cloud Gateway視為Spring Cloud Netflix Zuul項目的后續產品,并有助于在微服務環境中實現Gateway模式 。 它建立在
Spring Boot 2和Spring Webflux端到端都是無阻塞的-它公開了一個基于Netty的服務器,使用基于Netty的客戶端進行下游微服務調用,并在其余流程中使用了反應堆核心 。
我的目的是演示如何使用Spring Cloud Gateway以多種方式轉換基于Spring Cloud Netflix Zuul的小型路由。
Spring Cloud Netflix Zuul
Spring Cloud Zuul允許使用此處表示為yaml的屬性文件來配置簡單的路由規則:
zuul:routes:sample:path: /zuul/**url: http://httpbin.org:80strip-prefix: true該路由將在Zuul中暴露一個終結點,該終結點將攔截對前綴為“ / zuul”的uri的任何請求,并在去除“ zuul”前綴后將其轉發至下游系統。
Spring Cloud Gateway
Spring Cloud Gateway允許以三種方式對等效功能進行編碼-使用基于Java的DSL,使用基于Kotlin的DSL和使用基于簡單屬性的配置。
可以使用出色的http://start.spring.io網站生成一個入門項目:
基于Java的DSL
以下是創建類似于Zuul路由的基于Java的dsl:
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class GatewayRoutes {@Beanpublic RouteLocator routeLocator(RouteLocatorBuilder builder) {return builder.routes().route(r ->r.path("/java/**").filters(f -> f.stripPrefix(1)).uri("http://httpbin.org:80")).build();}}這是可讀的DSL,它配置一條路由,該路由以“ java”為前綴攔截uri,并在去除該前綴后將其發送到下游系統。
基于Kotlin的DSL
基于Kotlin的DSL配置此路由如下所示。
import org.springframework.cloud.gateway.route.RouteLocator import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder import org.springframework.cloud.gateway.route.builder.filters import org.springframework.cloud.gateway.route.builder.routes import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration@Configuration class KotlinRoutes {@Beanfun kotlinBasedRoutes(routeLocatorBuilder: RouteLocatorBuilder): RouteLocator =routeLocatorBuilder.routes {route { path("/kotlin/**")filters { stripPrefix(1) }uri("http://httpbin.org")}} }我最初為Spring Cloud Gateway路由提交了基于Kotlin的DSL的PR ,因此偏向于使用Kotlin配置Spring Cloud Gateway :-)。 該路由采用前綴為“ kotlin”的URL,并在進行下游微服務調用之前將其剝離。
物業路線
最后是基于屬性的屬性:
spring:cloud:gateway:routes: - predicates:- Path=/props/**filters:- StripPrefix=1uri: "http://httpbin.org"像Java和Kotlin版本一樣,此路由使用帶有前綴“ props”的url,并在進行下游調用之前將其刪除。 基于屬性的版本具有在運行時可刷新的附加優點。
結論
通過比較Spring Cloud Netflix Zuul的典型配置如何映射到Spring Cloud Gateway,這是Spring Cloud Gateway的快速入門。
翻譯自: https://www.javacodegeeks.com/2018/04/spring-cloud-gateway-configuring-a-simple-route.html
總結
以上是生活随笔為你收集整理的Spring Cloud Gateway –配置简单路由的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 坚强的后盾是什么意思 坚强的后盾的意思
- 下一篇: javafx中的tree_JavaFX中