微服务网关GateWay

Gateway简介
简介
Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开
发的网关,旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式,统一访问接口。Spring
Cloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix 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. 过滤器(filter) 一个标准的Spring webFilter,Spring Cloud Gateway中的Filter分为两种类型,
分别是Gateway Filter和Global Filter。过滤器Filter可以对请求和响应进行处理。

入门案例
(1) 创建工程导入依赖
在项目中添加新的模块 shop_gateway_server ,并导入依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

注意 SpringCloud Gateway使用的web框架为webflux,和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: 9014
eureka:
  instance:
    instance-id: gateway-server
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:9010/eureka/
spring:
  application:
    name: gateway-server
  cloud:
    gateway:
      routes:
      - id: order-service
        uri: lb://service-order2
        predicates:
        - Path=/order/**

id :我们自定义的路由 ID,保持唯一
uri :目标服务地址
predicates :路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
filters :过滤规则,暂时没用。

 uri以 lb: //开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称

路由规则
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

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

  cloud:
    gateway:
      routes:
      - id: order-service
        uri: lb://service-order2
        predicates:
        # - Path=/order/**
        - Path=/order-service/**
        filters:
        - RewritePath=/order-service/(?<segment>.*), /$\{segment

 根据微服务名进行转发(这样默认和Zuul一样)

spring:
  application:
    name: gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 启动根据服务名称进行转发
          lower-case-service-id: true #忽略大小写
#      routes:
#      - id: order-service
#        uri: lb://service-order2
#        predicates:
#        # - Path=/order/**
#        - Path=/order-service/**
#        filters:
#        - RewritePath=/order-service/(?<segment>.*), /$\{segment}

猜你喜欢

转载自www.cnblogs.com/dalianpai/p/12288456.html