Loading

Gateway入门案例

Zuul 1.x 是一个基于阻塞 IO 的 API Gateway 以及 Servlet;直到 2018 年 5 月,Zuul 2.x(基于Netty,也是非阻塞的,支持长连接)才发布,但 Spring Cloud 暂时还没有整合计划。Spring CloudGateway 比 Zuul 1.x 系列的性能和功能整体要好。

Gateway简介

简介

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式,统一访问接口。SpringCloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。它是基于Nttey的响应式开发模式。
上表为Spring Cloud Gateway与Zuul的性能对比,从结果可知,Spring Cloud Gateway的RPS是Zuul的1.6倍 

核心概念

1. 路由(route) 路由是网关最基础的部分,路由信息由一个ID、一个目的URL、一组断言工厂和一组Filter组成。如果断言为真,则说明请求URL和配置的路由匹配。
2. 断言(predicates) Java8中的断言函数,Spring Cloud Gateway中的断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的断言函数允许开发者去定义匹配来自Http Request中的任何信息,比如请求头和参数等。
3. 过滤器(fifilter) 一个标准的Spring webFilter,Spring Cloud Gateway中的Filter分为两种类型,分别是Gateway Filter和Global Filter。过滤器Filter可以对请求和响应进行处理。 

入门案例

(1) 创建工程导入依赖

在项目中添加新的模块 shop_gateway_server ,并导入依赖
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
  </dependency>
注意SpringCloud Gateway使用的web框架为webflflux,和SpringMVC不兼容。引入的限流组件是hystrix。redis底层不再使用jedis,而是lettuce。

(2) 配置启动类

@SpringBootApplication
public class GatewayServerApplication {
  public static void main(String[] args) {
      SpringApplication.run(GatewayServerApplication.class, args);
  }
}

(3) 编写配置文件

创建 application.yml 配置文件
  server:
    port: 8080 #服务端口
  spring:
    application:
      name: api-gateway #指定服务名
    cloud:
      gateway:
        routes:
        - id: product-service
          uri: http://127.0.0.1:9002
          predicates:
          - Path=/product/**
id:我们自定义的路由 ID,保持唯一
uri:目标服务地址
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
fifilters:过滤规则,暂时没用。
上面这段配置的意思是,配置了一个 id 为 product-service的路由规则,当访问网关请求地址以product 开头时,会自动转发到地址: http://127.0.0.1:9002/ 。配置完成启动项目即可在浏览器访问进行测试,当我们访问地址 http://localhost:8080/product/1 时会展示页面展示如下:

路由规则

Spring Cloud Gateway 的功能很强大,前面我们只是使用了 predicates 进行了简单的条件匹配,其实Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。在 Spring Cloud Gateway 中 Spring 利用Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。 

示例

  #路由断言之后匹配
  spring:
    cloud:
      gateway:
        routes:
        - id: after_route
          uri: https://xxxx.com
          #路由断言之前匹配
          predicates:
          - After=xxxxx
  #路由断言之前匹配
  spring:
    cloud:
      gateway:
        routes:
        - id: before_route
          uri: https://xxxxxx.com
          predicates:
          - Before=xxxxxxx
  #路由断言之间
  spring:
    cloud:
      gateway:
        routes:
        - id: between_route
          uri: https://xxxx.com
          predicates:
          - Between=xxxx,xxxx
  #路由断言Cookie匹配,此predicate匹配给定名称(chocolate)和正则表达式(ch.p)
  spring:
    cloud:
      gateway:
        routes:
        - id: cookie_route
          uri: https://xxxx.com
          predicates:
          - Cookie=chocolate, ch.p
  #路由断言Header匹配,header名称匹配X-Request-Id,且正则表达式匹配\d+
spring:
    cloud:
      gateway:
        routes:
        - id: header_route
          uri: https://xxxx.com
          predicates:
          - Header=X-Request-Id, \d+
  #路由断言匹配Host匹配,匹配下面Host主机列表,**代表可变参数
  spring:
    cloud:
      gateway:
        routes:
        - id: host_route
          uri: https://xxxx.com
          predicates:
          - Host=**.somehost.org,**.anotherhost.org
  #路由断言Method匹配,匹配的是请求的HTTP方法
  spring:
    cloud:
      gateway:
        routes:
        - id: method_route
          uri: https://xxxx.com
          predicates:
          - Method=GET
  #路由断言匹配,{segment}为可变参数
  spring:
    cloud:
      gateway:
        routes:
        - id: host_route
          uri: https://xxxx.com
          predicates:
          - Path=/foo/{segment},/bar/{segment}
  #路由断言Query匹配,将请求的参数param(baz)进行匹配,也可以进行regexp正则表达式匹配 (参数包含
  foo,并且foo的值匹配ba.)
  spring:
    cloud:
      gateway:
        routes:
        - id: query_route
          uri: https://xxxx.com
          predicates:
          - Query=baz 或 Query=foo,ba.

  #路由断言RemoteAddr匹配,将匹配192.168.1.1~192.168.1.254之间的ip地址,其中24为子网掩码位
  数即255.255.255.0
  spring:
    cloud:
      gateway:
        routes:
        - id: remoteaddr_route
          uri: https://example.org
          predicates:
          - RemoteAddr=192.168.1.1/24

动态路由

和zuul网关类似,在SpringCloud GateWay中也支持动态路由:即自动的从注册中心中获取服务列表并访问。

(1)添加注册中心依赖

在工程的pom文件中添加注册中心的客户端依赖(这里以Eureka为例)
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

(2)配置动态路由

修改 application.yml 配置文件,添加eureka注册中心的相关配置,并修改访问映射的URL为服务名称 
  server:
    port: 8080 #服务端口
  spring:
    application:
      name: api-gateway #指定服务名
    cloud:
      gateway:
        routes:
        - id: product-service
          uri: lb://shop-service-product
          predicates:
          - Path=/product/**
  eureka:
    client:
      serviceUrl:
        defaultZone: http://127.0.0.1:8761/eureka/
        registry-fetch-interval-seconds: 5 # 获取服务列表的周期:5s
    instance:
      preferIpAddress: true
      ip-address: 127.0.0.1
uri : uri以 lb: //开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称 

重写转发路径

在SpringCloud Gateway中,路由转发是直接将匹配的路由path直接拼接到映射路径(URI)之后,那么在微服务开发中往往没有那么便利。这里就可以通过RewritePath机制来进行路径重写。

(1) 案例改造

修改 application.yml ,将匹配路径改为 /product-service/** 
重新启动网关,我们在浏览器访问http://127.0.0.1:8080/product-service/product/1,会抛出404。这是由于路由转发规则默认转发到商品微服务( http://127.0.0.1:9002/product-service/product/1 )路径上,而商品微服务又没有 product-service 对应的映射配置。 

(2) 添加RewritePath重写转发路径

修改 application.yml ,添加重写规则
  spring:
    application:
      name: api-gateway #指定服务名
    cloud:
      gateway:
        routes:
        - id: product-service
          uri: lb://shop-service-product
          predicates:
          - Path=/product-service/**
          filters:
          - RewritePath=/product-service/(?<segment>.*), /$\{segment}
通过RewritePath配置重写转发的url,将/product-service/(?.*),重写为{segment},然后转发到订单微服务。比如在网页上请求http://localhost:8080/product-service/product,此时会将请求转发到http://127.0.0.1:9002/product/1( 值得注意的是在yml文档中 $ 要写成 $\ )
 
posted @ 2021-07-29 11:30  1640808365  阅读(559)  评论(0编辑  收藏  举报