目录

一.API网关

二.业界流行的网关

三 .SpringCloud Gateway简介

四 SpringCloud Gateway快速入门

五 若依平台—ruoyi-gateway网关

六 重要功能配置——路由断言和过滤器

七 高级配置——网关限流



一.API网关

API网关,就是指系统的统一入口,它封装来应用程序的内部结构,为客户端提供统一服务,一些与业务本身功能无关的公共逻辑可以在这里实现,诸如认证,鉴权,监控,路由转发等等。

二.业界流行的网关

(1)Ngnix+lua :使用nginx的反向代理和负载均衡可实现api服务器的负载均衡及高可用。lua是一种脚步语言,可以来编写一些简单的nginx支持lua脚本。

(2)Kong:基于Nginx+Lua开发,性能高,稳定,有多个可用的插件(限流,鉴权等等)可以开箱即用。缺点:只支持http协议;二次开发,自由扩展困难;提供管理API,缺乏更易用的管控,配置方式。

(3)Zuul:Netflix开源的网关,功能丰富,使用Java开发,易于二次开发。缺点:缺乏管控,无法动态配置;依赖组件较多;处理http请求依赖的是web容器,性能不如nginx

(4)Spring Cloud Gateway:Spring公司为了替换Zuul而开发的网关服务

SpringCloud alibaba技术栈中并没有提供自己的网关,我们可以采用Spring Cloud Gateway来做网关

三 .SpringCloud Gateway简介

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

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

(1)优点:

性能强劲:是第一代网关Zuul的1.6倍

功能强大:内置了很多使用的功能,例如转发,监控,限流等

设计优雅,容易扩展

(2)缺点:

其实现依赖Netty与WebFlux,不是传统的Servlet编程模型,学习成本高

不能将其部署在tomcat,jetty等Servlet容器里,只能打成jar包执行

需要Spingboot2.0及以上的版本,才支持。

四 SpringCloud Gateway快速入门

1. 创建新模块,导入依赖

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

2. 创建主类

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

3. 添加配置文件application.yml

server:
  port: 8080

spring: 
  application:
    name: ruoyi-gateway
  cloud:
    gateway:
      routes: #路由数组[路由就是指定当请求满足什么条件的时候转到哪个微服务]
        # 系统模块
        - id: ruoyi-system #当前路有的标示,要求唯一
          uri: http://localhost:9201/ #请求要转发到的地址
          predicates: #断言(就是路由转发要满足的条件)
            - Path=/system/** #当前请求路径满足Path指定的规则时,才进行路有转发
          filters: #过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
            - StripPrefix=1  #转发之前去掉1层路径

4. 运行效果

在地址栏输入 localhost:8080/system/user/1就会自动跳转到 localhost:9201/user/1

其中路径中的system被过滤掉,8080变成9201

5. 使用nacos注册中心配置方式进行优化

(1)现在在配置文件中写死了转发路径的地址,我们需要使用nacos获取此地址,

(2)并使用  uri: lb://ruoyi-system #请求要转发到的地址 自动实现负载均衡

第1步,加入nacos依赖

  <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

第2部:应用加上注解@EnableDiscoveryClient

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

第3部:修改配置文件

server:
  port: 8080

spring: 
  application:
    name: ruoyi-gateway
  cloud:
    nacos:
      discovery:
      server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
        lowerCaseServiceId: true
        enabled: true   #让gateway可以发现nacos的微服务
      routes: #路由数组[路由就是指定当请求满足什么条件的时候转到哪个微服务]
        # 系统模块
        - id: ruoyi-system #当前路有的标示,要求唯一
          uri: lb://ruoyi-system #请求要转发到的地址
          predicates: #断言(就是路由转发要满足的条件)
            - Path=/system/** #当前请求路径满足Path指定的规则时,才进行路有转发
          filters: #过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
            - StripPrefix=1  #转发之前去掉1层路径

五 若依平台—ruoyi-gateway网关

(1)若依平台单独建立ruoyi-gateway网关模块,代码里有4个过滤器:

AuthFilter(全局过滤器),
BlackListUrlFilter(黑名单过滤器)
CacheRequestFilter 
ValidateCodeFilter (验证码过滤器)

(2)在nacos里配置了路由信息 和白名单

 

spring:
  redis:
    host: localhost
    port: 6379
    password: 
  cloud:
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        # 认证中心
        - id: ruoyi-auth
          uri: lb://ruoyi-auth
          predicates:
            - Path=/auth/**
          filters:
            # 验证码处理
            - CacheRequestFilter
            - ValidateCodeFilter
            - StripPrefix=1
        # 代码生成
        - id: ruoyi-gen
          uri: lb://ruoyi-gen
          predicates:
            - Path=/code/**
          filters:
            - StripPrefix=1
        # 定时任务
        - id: ruoyi-job
          uri: lb://ruoyi-job
          predicates:
            - Path=/schedule/**
          filters:
            - StripPrefix=1
        # 系统模块
        - id: ruoyi-system
          uri: lb://ruoyi-system
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1
        # 文件服务
        - id: ruoyi-file
          uri: lb://ruoyi-file
          predicates:
            - Path=/file/**
          filters:
            - StripPrefix=1
         # whaletest模块
        - id: ruoyi-whaletest
          uri: lb://ruoyi-whaletest
          predicates:
            - Path=/whaletest/**
          filters:
            - StripPrefix=1

# 不校验白名单
ignore:
  whites:
    - /auth/logout
    - /auth/login
    - /*/v2/api-docs
    - /csrf
    - /whaletest/test

六 重要功能配置——路由断言和过滤器

我们看到每个路由主要对predicates  和filter进行了配置。这两个一个是断言,一个是过滤器。接下来我们对这两个重要功能进行说明

打开nacos中的ruoyi-gateway-dev.yml ,显示有路由的配置信息如下

  # 系统模块
        - id: ruoyi-system #当前路有的标示,要求唯一
          uri: lb://ruoyi-system #请求要转发到的地址
          predicates: #断言(就是路由转发要满足的条件)
            - Path=/system/** #当前请求路径满足Path指定的规则时,才进行路有转发
          filters: #过滤器,请求在传递过程中可以通过过滤器对其进行一定的修改
            - StripPrefix=1  #转发之前去掉1层路径

一.predicates 断言:路有需要满足什么条件才能路由,是路由转发的判断条件,目前SpringCloud Gateway支持多种方式,常见如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式

Predicate 接受一个输入参数,返回一个布尔值结果。

(1)系统内置的断言

规则实例说明
Path- Path=/gate/,/rule/## 当请求的路径为gate、rule开头的时,转发到http://localhost:9023服务器上
Before- Before=2017-01-20T17:42:47.789-07:00[America/Denver]在某个时间之前的请求才会被转发到 http://localhost:9023服务器上
After- After=2017-01-20T17:42:47.789-07:00[America/Denver]在某个时间之后的请求才会被转发
Between- Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver]在某个时间段之间的才会被转发
Cookie- Cookie=chocolate, ch.p名为chocolate的表单或者满足正则ch.p的表单才会被匹配到进行请求转发
Header- Header=X-Request-Id, \d+携带参数X-Request-Id或者满足\d+的请求头才会匹配
Host- Host=www.hd123.com当主机名为www.hd123.com的时候直接转发到http://localhost:9023服务器上
Method- Method=GET只有GET方法才会匹配转发请求,还可以限定POST、PUT等请求方式

(2)自定义断言

二.filters过滤

1. 局部过滤,是针对单个路有的过滤器

(1)内置过滤规则

过滤规则实例说明
PrefixPath- PrefixPath=/app在请求路径前加上app
RewritePath- RewritePath=/test, /app/test访问localhost:9022/test,请求会转发到localhost:8001/app/test
SetPath- SetPath=/{segment}设置请求路径,与RewritePath类似。如/red/blue的请求被转发到/blue。
SetStatus- SetStatus=401设置回执状态码。
StripPrefix- StripPrefix=2跳过指定路径。请求/name/blue/red会转发到/red。

(2)自定义过滤规则

若依平台中nacos中ruoyi-gateway-dev.yml,有添加自定义过滤规则

          filters:
            # 验证码处理
            - CacheRequestFilter
            - ValidateCodeFilter
            - StripPrefix=1

同时需要实现AbstractGatewayFilterFactory<>方法

2. 全局过滤:全局过滤器作用于所有的路由,不需要单独配置,我们可以用它来实现很多统一化处理的业务需求,比如权限认证,IP访问限制等等。目前网关统一鉴权AuthFilter.java就是采用的全局过滤器。

自定义全局过滤规则,只需要实现GlobalFilterOrdered这两个接口就可以了。

/**
 * 网关鉴权
 * 
 * @author ruoyi
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered
{
    
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
    {
        
    }

   
    @Override
    public int getOrder()
    {
        return -200;
    }
}

七,高级配置——网关限流

 网关是所有请求的公共入口,所以可以在网关进行限流,而且限流方式也很多。Sentinel支持对SpringCloud Gateway,Zuul等主流网关进行限流。

Sentinel可以提供两种资源的限流

1. 路由route限流:即在Spring配置文件中配置的路由条目,资源名为对应的ruoteId

2.自定义API维度:用户可以利用sentinel提供的API来自定义一些API分组

需要添加如下依赖

  <!-- SpringCloud Alibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <!-- SpringCloud Alibaba Sentinel Gateway -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>

Logo

快速构建 Web 应用程序

更多推荐