Spring Cloud Gateway –配置简单路由

可以将Spring Cloud Gateway视为Spring Cloud Netflix Zuul项目的后续产品,并有助于在微服务环境中实现Gateway模式 。 它建立在
Spring Boot 2Spring 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:80
      strip-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 {

    @Bean
    public 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 {

    @Bean
    fun kotlinBasedRoutes(routeLocatorBuilder: RouteLocatorBuilder): RouteLocator =
            routeLocatorBuilder.routes {
                route { 
                    path("/kotlin/**")
                    filters { stripPrefix(1) }
                    uri("http://httpbin.org")
                }
            }
}

我最初为Spring Cloud Gateway路由提交了基于Kotlin的DSLPR ,因此偏向于使用Kotlin配置Spring Cloud Gateway :-)。 该路由采用前缀为“ kotlin”的URL,并在进行下游微服务调用之前将其剥离。

物业路线

最后是基于属性的属性:

spring:
  cloud:
    gateway:
      routes: 
        - predicates:
            - Path=/props/**
          filters:
            - StripPrefix=1
          uri: "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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值