SpringCloud Gateway

Gateway网关

Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2和 Project Reactor等技术。Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等。

1.Spring Cloud Gateway 具有如下特性:
  • 基于Spring Framework 5, Project Reactor 和 Spring Boot 2.0 进行构建;
  • 动态路由:能够匹配任何请求属性;
  • 可以对路由指定 Predicate(断言)和 Filter(过滤器);
  • 集成Hystrix的断路器功能;
  • 集成 Spring Cloud 服务发现功能;
  • 易于编写的 Predicate(断言)和 Filter(过滤器);
  • 请求限流功能;
  • 支持路径重写。
2.相关概念
  • Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由;
  • Predicate(断言):指的是Java 8 的 Function Predicate。SpringCloud断言的输入类型是Spring框架中的ServerWebExchange(服务网络交换器,存放着重要的请求-响应属性、请求实例和响应实例等等)。 这使开发人员可以匹配HTTP请求中的所有内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;
  • Filter(过滤器):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。
3.路由配置
3.1环境搭建
  • 导入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.4.Final</version>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>
  • 编写配置
server:
  port: 9000
service-url:
  user-service: http://localhost:8001
spring:
  cloud:
    gateway:
      routes:
        - id: path_route #路由的ID
          uri: ${service-url.user-service}/user/{id} #匹配后路由地址
          predicates: # 断言,路径相匹配的进行路由
            - Path=/user/{id}
  • 启动eureka-server,user-service和api-gateway服务,并调用该地址测试:http://localhost:9000/user/1
  • 我们发现该请求被路由到了user-service的该路径上:http://localhost:8081/user/1
3.2路由规则

Spring Cloud Gateway包括许多内置的Route Predicate工厂。 所有这些Predicate都与HTTP请求的不同属性匹配。 多个Route Predicate工厂可以进行组合,下面我们来介绍下一些常用的Route Predicate。

After Route Predicate

在指定时间之后的请求会匹配该路由。

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: ${service-url.user-service}
          predicates:
            - After=2020-09-20T10:30:00+08:00[Asia/Shanghai]
3.3动态路由

使用Zuul作为网关结合注册中心进行使用时,默认情况下Zuul会根据注册中心注册的服务列表,以服务名为路径创建动态路由,Gateway同样也实现了该功能。下面我们演示下Gateway结合注册中心如何使用默认的动态路由和过滤器

  • pom.xml中添加依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  • 配置yml

方式一

server:
  port: 9000
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        #配置路由:路由ID,路由到微服务的uri,断言(判断条件)
        - id: path_route
          uri: lb://user-service  #服务的ID,uri的协议为lb,这样才能启用Gateway的负载均衡功能
          predicates:
            - Path=/user/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

方式二

server:
  port: 9000
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能
          lower-case-service-id: true #使用小写服务名,默认是大写
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
logging:
  level:
    org.springframework.cloud.gateway: debug
4.Route Filter过滤器

路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。Spring Cloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生,下面我们介绍下常用路由过滤器的用法。

  • 过滤器的生命周期: SpringCloud Gateway过滤器有两个 pre 和 post
    • PRE:在请求被路由之前调用,可以利用这种过滤器实现身份验证,在集群中选择请求的微服务,纪律调试信息等。
    • POST:在请求被路由到微服务以后执行,这种过滤器可以为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端。
  • 过滤器的类型:从作用的范围分为两种GatewayFilter 和 GlobalFilter
    • GatewayFilter:应用在单个路由和分组路由上
    • GlobalFilter:应用到所有的路由上
4.1局部过滤器

局部过滤器(GatewayFilter)是针对单个路由的过滤器,可以对访问的URL进行过滤,进行切面处理。在SpringCloud Gateway中内置了多种不同类型的局部过滤器

4.1.1AddRequestParameter GatewayFilter

给请求添加参数的过滤器。

spring:
  cloud:
    gateway:
      routes:
        - id: add_request_parameter_route
          uri: http://localhost:8081
          filters:
            - AddRequestParameter=username, hello
          predicates:
            - Method=GET

以上配置会对GET请求添加username=hello的请求参数,通过工具进行如下测试

http://localhost:9000/user/getByUsername  

相当于发起该请求:

http://localhost:8081/user/getByUsername?username=hello
4.1.2 StripPrefix GatewayFilter

对指定数量的路径前缀进行去除的过滤器

spring:
  cloud:
    gateway:
      routes:
      - id: strip_prefix_route
        uri: http://localhost:8081
        predicates:
        - Path=/user-service/**
        filters:
        - StripPrefix=2

以上配置会把以/user-service/开头的请求的路径去除两位,通过工具进行如下测试

http://localhost:9000/user-service/a/user/1

相当于发起该请求:

http://localhost:8081/user/1
4.1.3 PrefixPath GatewayFilter

与StripPrefix过滤器恰好相反,会对原有路径进行增加操作的过滤器。

spring:
  cloud:
    gateway:
      routes:
      - id: prefix_path_route
        uri: http://localhost:8081
        predicates:
        - Method=GET
        filters:
        - PrefixPath=/user

以上配置会对所有GET请求添加/user路径前缀,通过工具进行如下测试

 http://localhost:9000/1

相当于发起该请求:

http://localhost:8081/user/1
4.1.4 Hystrix GatewayFilter

Hystrix 过滤器允许你将断路器功能添加到网关路由中,使你的服务免受级联故障的影响,并提供服务降级处理。

  • 要开启断路器功能,我们需要在pom.xml中添加Hystrix的相关依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 然后添加相关服务降级的处理类:
@RestController
public class FallbackController {

    @GetMapping("/fallback")
    public Object fallback() {
        Map<String,Object> result = new HashMap<>();
        result.put("data",null);
        result.put("message","Get request fallback!");
        result.put("code",500);
        return result;
    }
}
  • 在application.yml中添加相关配置,当路由出错时会转发到服务降级处理的控制器上:
spring:
  cloud:
    gateway:
      routes:
        - id: hystrix_route
          uri: http://localhost:8081
          predicates:
            - Method=GET
          filters:
            - name: Hystrix
              args:
                name: fallbackcmd
                fallbackUri: forward:/fallback
  • 关闭user-service,调用该地址进行测试:http://localhost:9000/user/1 ,发现已经返回了服务降级的处理信息
4.1.5 RequestRateLimiter GatewayFilter

RequestRateLimiter 过滤器可以用于限流,使用RateLimiter实现来确定是否允许当前请求继续进行,如果请求太大默认会返回HTTP 429-太多请求状态。

  • 在pom.xml中添加相关依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
  • 添加限流策略的配置类,这里有两种策略一种是根据请求参数中的username进行限流,另一种是根据访问IP进行限流;
@Configuration
public class RedisRateLimiterConfig {
    @Bean
    KeyResolver userKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("username"));
    }

    @Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
}
  • 我们使用Redis来进行限流,所以需要添加Redis和RequestRateLimiter的配置,这里对所有的GET请求都进行了按IP来限流的操作;
server:
  port: 9000
spring:
  redis:
    host: localhost
    password: 123456
    port: 6379
  cloud:
    gateway:
      routes:
        - id: requestratelimiter_route
          uri: http://localhost:8081
          filters:
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 1 #每秒允许处理的请求数量
                redis-rate-limiter.burstCapacity: 2 #每秒最大处理的请求数量
                key-resolver: "#{@ipKeyResolver}" #限流策略,对应策略的Bean
          predicates:
            - Method=GET
logging:
  level:
    org.springframework.cloud.gateway: debug
  • 多次请求该地址:http://localhost:9000/user/1 ,会返回状态码为429的错误。
4.1.6 Retry GatewayFilter

对路由请求进行重试的过滤器,可以根据路由请求返回的HTTP状态码来确定是否进行重试。

  • 修改配置文件:
spring:
  cloud:
    gateway:
      routes:
      - id: retry_route
        uri: http://localhost:8081
        predicates:
        - Method=GET
        filters:
        - name: Retry
          args:
            retries: 1 #需要进行重试的次数
            statuses: BAD_GATEWAY #返回哪个状态码需要进行重试,返回状态码为5XX进行重试
            backoff:
              firstBackoff: 10ms
              maxBackoff: 50ms
              factor: 2
              basedOnPreviousValue: false
  • 当调用返回500时会进行重试,访问测试地址:http://localhost:9000/user/111

  • 可以发现user-service控制台报错2次,说明进行了一次重试。

4.2全局过滤器

全局过滤器(GlobalFilter)作用于所有路由,SpringCloud Gateway提供了GlobalFilter接口,可以自定义过滤器;通过全局过滤器可以实现对权限的统一校验、安全验证等功能。

以下代码演示全局过滤器的实现

@Component
public class LoginFilter implements GlobalFilter, Ordered {
    /**
     * 执行过滤器的业务逻辑
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("执行了全局过滤器");
        String token = exchange.getRequest().getQueryParams().getFirst("token");
        if(token == null){
            System.out.println("请登录");
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();//请求结束
        }
        return chain.filter(exchange);//继续执行后续的过滤器
    }

    /**
     * 定义过滤器的执行顺序
     * 值越小优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
5.跨域
spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': 
            # 允许携带认证信息
            # 允许跨域的源(网站域名/ip),设置*为全部
            # 允许跨域请求里的head字段,设置*为全部
            # 允许跨域的method, 默认为GET和OPTIONS,设置*为全部
            # 跨域允许的有效期
            allow-credentials: true
            allowed-origins: 
            - "http://localhost:13009"
            - "http://localhost:13010"
            allowed-headers: "*"
            allowed-methods: 
            - OPTIONS
            - GET
            - POST
            max-age: 3600
            # 允许response的head信息
            # 默认仅允许如下6个:
            #     Cache-Control
            #     Content-Language
            #     Content-Type
            #     Expires
            #     Last-Modified
            #     Pragma
            #exposed-headers:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值