12.Spring Cloud Gateway:新一代API网关服务

Spring Cloud Gateway:新一代API网关服务

https://mp.weixin.qq.com/s/bTp4_M3m7rhlhC9wYKExwA

Spring Cloud Gateway 为 SpringBoot 应用提供了API网关支持,具有强大的智能路由与过滤器功能,本文将对其用法进行详细介绍。

1.Gateway 简介

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

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。输入类型是Spring框架中的ServerWebExchange。这使开发人员可以匹配HTTP请求中的所有内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;
  • Filter(过滤器):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。

3.创建 api-gateway模块

这里我们创建一个api-gateway模块来演示Gateway的常用功能。

3.1 在pom.xml中添加相关依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

3.2 两种不同的配置路由方式

Gateway 提供了两种不同的方式用于配置路由,一种是通过yml文件来配置,另一种是通过Java Bean来配置,下面我们分别介绍下。

3.2.1 使用yml配置
3.2.1.1 在application.yml中进行配置:
server:
  port: 9201
service-url:
  user-service: http://localhost:8201
spring:
  cloud:
    gateway:
      routes:
        - id: path_route #路由的ID
          uri: ${
   
   service-url.user-service}/user/{
   
   id} #匹配后路由地址
          predicates: # 断言,路径相匹配的进行路由
            - Path=/user/{
   
   id}
3.2.1.2 启动eureka-server,user-service和api-gateway服务,并调用该地址测试:http://localhost:9201/user/1
3.2.1.3 我们发现该请求被路由到了user-service的该路径上:http://localhost:8201/user/1

有截图

3.2.2 使用Java Bean配置
3.2.2.1 添加相关配置类,并配置一个RouteLocator对象:
/**
 * Created by macro on 2020/1/11.
 */
@Configuration
public class GatewayConfig {
   
   

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
   
   
        return builder.routes()
                .route("path_route2", r -> r.path("/user/getByUsername")
                        .uri("http://localhost:8201/user/getByUsername"))
                .build();
    }
}
3.2.2.2 重新启动api-gateway服务,并调用该地址测试:http://localhost:9201/user/getByUsername?username=allan
3.2.2.3 我们发现该请求被路由到了user-service的该路径上:http://localhost:8201/user/getByUsername?username=allan

有截图

3.Route Predicate 的使用

Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个Route Predicate工厂可以进行组合,下面我们来介绍下一些常用的Route Predicate。

注意:Predicate中提到的配置都在application-predicate.yml文件中进行修改,并用该配置启动api-gateway服务。

3.1 After Route Predicate

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

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: ${
   
   service-url.user-service}
          predicates:
            - After=2019-09-24T16:30:00+08:00[Asia/Shanghai]

3.2 Before Route Predicate

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

spring:
  cloud:
    gateway:
      routes:
        - id: before_route
          uri: ${
   
   service
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值