Spring Cloud(六) 服务网关GateWay 入门

前文回顾:

Spring Cloud(一)Eureka Server-单体及集群搭建

Spring Cloud(二) 配置Eureka Client

Spring Cloud(三) 熔断器Hystrix

Spring Cloud(四) API网关Zuul

Spring Cloud(五) Zuul Filter

一.简介

Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

二.快速入门

Spring Cloud Gateway 网关路由有两种配置方式:

  • 在配置文件 yml 中配置

  • 通过@Bean自定义 RouteLocator,在启动主类 Application 中配置

(1)pom中添加依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> 
</parent>
​
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

(2)配置文件

server:
  port: 8080
spring:
  cloud:
    gateway:
      routes:
      - id: neo_route
        uri: https://blog.csdn.net
        predicates:
        - Path=/fy_java1995

各字段含义如下:

  • id:我们自定义的路由 ID,保持唯一

  • uri:目标服务地址

  • predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。

  • filters:过滤规则,本示例暂时没用。

上面这段配置的意思是,配置了一个 id 为 neo_route 的路由规则,当访问地址 http://localhost:8080/fy_java1995时会自动转发到地址:https://blog.csdn.net/fy_java1995`。

(3)启动类配置

我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator() 来定制转发规则。

@SpringBootApplication
public class GateWayApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }
​
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("path_route", r -> r.path("/fy_java1995")
                        .uri("https://blog.csdn.net"))
                .build();
    }
​
}

三.路由规则

Spring Cloud Gateway 是通过 Spring WebFlux 的 HandlerMapping 做为底层支持来匹配到转发路由,Spring Cloud Gateway 内置了很多 Predicates 工厂,这些 Predicates 工厂通过不同的 HTTP 请求参数来匹配,多个 Predicates 工厂可以组合使用。

Predicate 来源于 Java 8,是 Java 8 中引入的一个函数,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。

在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。网上有一张图总结了 Spring Cloud 内置的几种 Predicate 的实现。

(1)通过时间匹配

spring:
  cloud:
    gateway:
      routes:
       - id: time_route
        uri: https://blog.csdn.net/fy_java1995
        predicates:
         - After=2018-01-20T06:06:06+08:00[Asia/Shanghai]

Spring 是通过 ZonedDateTime 来对时间进行的对比,ZonedDateTime 是 Java 8 中日期时间功能里,用于表示带时区的日期与时间信息的类,ZonedDateTime 支持通过时区来设置时间,中国的时区是:Asia/Shanghai

After Route Predicate 是指在这个时间之后的请求都转发到目标地址。上面的示例是指,请求时间在 2018年1月20日6点6分6秒之后的所有请求都转发到地址https://blog.csdn.net/fy_java1995+08:00是指时间和UTC时间相差八个小时,时间地区为Asia/Shanghai

添加完路由规则之后,访问地址http://localhost:8080会自动转发到https://blog.csdn.net/fy_java1995

Before Route Predicate 刚好相反,在某个时间之前的请求的请求都进行转发。我们把上面路由规则中的 After 改为 Before,如下:

spring:
  cloud:
    gateway:
      routes:
       - id: after_route
        uri: https://blog.csdn.net/fy_java1995
        predicates:
         - Before=2018-01-20T06:06:06+08:00[Asia/Shanghai]

(2)通过Cookie匹配

spring:
  cloud:
    gateway:
      routes:
       - id: cookie_route
         uri: https://blog.csdn.net/fy_java1995
         predicates:
         - Cookie=ityouknow, kee.e

使用 curl 测试,命令行输入:

curl http://localhost:8080 --cookie "ityouknow=kee.e"

(3)通过Header匹配

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://blog.csdn.net/fy_java1995
        predicates:
        - Header=X-Request-Id, \d+

使用 curl 测试,命令行输入:

curl http://localhost:8080  -H "X-Request-Id:666666" 

(4)通过Method匹配

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://blog.csdn.net/fy_java1995
        predicates:
        - Method=GET
使用 curl 测试,命令行输入:
# curl 默认是以 GET 的方式去请求
curl http://localhost:8080

(5)通过Path匹配

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://blog.csdn.net/fy_java1995
        predicates:
        - Path=/foo/{segment}

如果请求路径符合要求,则此路由将匹配,例如:/foo/1 或者 /foo/bar。

使用 curl 测试,命令行输入:

curl http://localhost:8080/foo/1
curl http://localhost:8080/foo/xx
curl http://localhost:8080/boo/xx

经过测试第一和第二条命令可以正常获取到页面返回值,最后一个命令报404,证明路由是通过指定路由来匹配。

(6)通过查询参数匹配

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://blog.csdn.net/fy_java1995
        predicates:
        - Query=smile

这样配置,只要请求中包含 smile 属性的参数即可匹配路由。

使用 curl 测试,命令行输入:

curl localhost:8080?smile=x

猜你喜欢

转载自blog.csdn.net/fy_java1995/article/details/94407680