Spring Cloud Gateway

https://docs.spring.io/spring-cloud-gateway/docs/

https://cloud.spring.io/spring-cloud-gateway/reference/html/

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

SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 2.0之前的非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

Spring Cloud Gateway 的目标,不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

SpringCloud官方,对SpringCloud Gateway 特征介绍如下:

  • 基于Spring Framework 5、Project Reactor和Spring Boot 2.0构建
  • 能够在任意请求属性上匹配路由
  • predicates(谓词) 和 filters(过滤器)是特定于路由的
  • 集成了Hystrix断路器,也可以集成Sentinel降级功能
  • 集成了Spring Cloud DiscoveryClient
  • 易于编写谓词和过滤器
  • 请求速率限制
  • 路径重写

从以上的特征来说,和Zuul的特征差别不大。SpringCloud Gateway和Zuul主要的区别,还是在底层的通信框架上。

简单说明一下上文中的三个术语:

1Filter(过滤器)

和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。

(2)Route(路由):

网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。

3Predicate(断言)

这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloudalibaba</artifactId>
        <groupId>com.wsm.springcloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway</artifactId>

    <dependencies>
        <!-- gateway的依赖 springcloud开发 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>


</project>
package com.wsm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GatewayApplication {

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

}

application.yml

server:
  port: 8060
spring:
  application:
    name: api-gateway
  cloud:
    # gateway的配置
    gateway:
      # 路由规则
      routes:
        - id: order_route # 路由的唯一标识, 路由到 order
          uri: http://localhost:8020 # 需要转发的地址
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-service/**
              # http://localhost:8060/order-service/order/add 路由转到
              # http://localhost:8020/order-service/order/add
          filters:
            - StripPrefix=1  # 转发之前去掉第一层路径
              # http://localhost:8020/order-service/order/add 过虑成
              # http://localhost:8020/order/add

原文地址:https://www.cnblogs.com/mingforyou/p/15771637.html