• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring Cloud GateWay网关的具体和使用

武飞扬头像
Java程序员调优
帮助1

前言  

网关有以下几个作用:

  • 统一入口:未全部为服务提供一个唯一的入口,网关起到外部和内部隔离的作用,保障了后台服务的安全性。
  • 鉴权校验:识别每个请求的权限,拒绝不符合要求的请求。
  • 动态路由:动态的将请求路由到不同的后端集群中。
  • 减少客户端与服务端的耦合:服务可以独立发展,通过网关层来做映射。

一、什么是Spring Cloud Gateway?

  • Spring Cloud 第一代网关 Zuul , Spring Cloud 第二代网关 GateWay。
  • Spring Cloud Gateway 是由 WebFlux Netty Reactor 实现的响应式的 API 网关。
  • Spring Cloud Gateway 不能在传统的 servlet 容器中工作,也不能构建成 war 包!
  • Spring Cloud Gateway 为微服务架构提供一种简单且有效的 API 路由的管理方式,并基于 Filter 的方式提供网关的基本功能,例如说安全认证、监控、限流等等

注意:Spring Cloud Gateway会和spring-webmvc的依赖冲突,需要排除spring-webmvc 

二、Spring Cloud Gateway核心概念

  • 路由(route)

路由是网关中最基础的部分,路由信息包括一个ID、一个目的URI、一组断言工厂、一组Filter组成。如果断言为真,则说明请求的URL和配置的路由匹配。

  • 断言(predicates)

Java8中的断言函数,SpringCloud Gateway中的断言函数类型是Spring5.0框架中的ServerWebExchange。断言函数允许开发者去定义匹配Http request中的任何信息,比如请求头和参数等。

  • 过滤器(Filter)

SpringCloud Gateway中的filter分为Gateway FilIer和Global Filter。Filter可以对请求和响应进行处理。

三、Spring Cloud Gateway如何工作的?

学新通

 四、Spring Cloud Gateway快速开始

1. 环境搭建:Maven项目子模块,pom配置。

2. 配置文件:基础配置、路由配置断言配置、过滤器配置。

3. 代码部分:自定义路由/断言/过滤器等,(无实现可无此配置)

详情参考:官网配置使用

1、环境搭建

  1.  
    <!-- 依赖 父工程-->
  2.  
    <parent>
  3.  
    <groupId>org.example</groupId>
  4.  
    <artifactId>lxnmall</artifactId>
  5.  
    <version>1.0-SNAPSHOT</version>
  6.  
    </parent>
  7.  
     
  8.  
    <!-- 本工程基础信息-->
  9.  
    <groupId>com.lxnmall</groupId>
  10.  
    <artifactId>gateway</artifactId>
  11.  
    <version>0.0.1-SNAPSHOT</version>
  12.  
    <name>lxnmall-gateway</name>
  13.  
     
  14.  
    <dependencies>
  15.  
    <!-- 本工程依赖其他公共模块 要排除web mvc -->
  16.  
    <dependency>
  17.  
    <groupId>org.lxnmall</groupId>
  18.  
    <artifactId>lxnmall-common</artifactId>
  19.  
    <version>1.0-SNAPSHOT</version>
  20.  
    <exclusions>
  21.  
    <exclusion>
  22.  
    <groupId>com.alibaba</groupId>
  23.  
    <artifactId>druid-spring-boot-starter</artifactId>
  24.  
    </exclusion>
  25.  
    <exclusion>
  26.  
    <groupId>com.github.pagehelper</groupId>
  27.  
    <artifactId>pagehelper-spring-boot-starter</artifactId>
  28.  
    </exclusion>
  29.  
    </exclusions>
  30.  
    </dependency>
  31.  
    <!-- gateway的依赖添加-->
  32.  
    <dependency>
  33.  
    <groupId>org.springframework.cloud</groupId>
  34.  
    <artifactId>spring-cloud-starter-gateway</artifactId>
  35.  
    </dependency>
  36.  
    <!-- 整合其他的依赖添加-->
  37.  
    </dependencies>
学新通

2、 配置文件

1)基础信息

学新通

  1.  
    server:
  2.  
    port: 8888
  3.  
    spring:
  4.  
    application:
  5.  
    name: lixiunan-gateway
  6.  
    cloud:
  7.  
    gateway:
  8.  
    # 是否开启网关
  9.  
    enabled: true
  10.  
    discovery:
  11.  
    locator:
  12.  
    # 默认为false,设为true开启: 通过微服务创建路由的功能,即可以通过微服务名访问服务
  13.  
    lower-case-service-id: true
  14.  
    enabled: true

2)路由\断言\过滤器配置

  1.  
    server:
  2.  
    port: 8888
  3.  
    spring:
  4.  
    application:
  5.  
    name: lixiunan-gateway
  6.  
    cloud:
  7.  
    gateway:
  8.  
    # 是否开启网关
  9.  
    enabled: true
  10.  
    discovery:
  11.  
    locator:
  12.  
    # 默认为false,设为true开启: 通过微服务创建路由的功能,即可以通过微服务名访问服务
  13.  
    lower-case-service-id: true
  14.  
    enabled: true
  15.  
    # 设置路由:路由id、路由到微服务的uri、断言
  16.  
    routes:
  17.  
    # 路由ID,全局唯一
  18.  
    - id: lxnmall-product
  19.  
    # 详见【解释1
  20.  
    uri: lb://lxnmall-product
  21.  
    # 断言配置
  22.  
    predicates:
  23.  
    # 路径配置:原路径为http://localhost:8001/order/getProduct...
  24.  
    - Path=/product/**
  25.  
    # #######以下非必需#######################
  26.  
    # 时间匹配:在指定的日期时间之后发生的请求才可以进网关,入参是ZonedDateTime类型
  27.  
    - After=2021-10-18T22:22:07.783 08:00[Asia/Shanghai]
  28.  
    # Cookie匹配:cookie里username为lixiunan的可以进
  29.  
    - Cookie=username, lixiunan
  30.  
    # Header匹配 请求中带有请求头名为 x-request-id,其值与 \d 正则表达式匹配
  31.  
    #- Header=X-Request-Id, \d
  32.  
    #配置过滤器工厂
  33.  
    #filters:
  34.  
    # - AddRequestHeader=X-Request-color, red #添加请求头
学新通

【解释1】:目标微服务的请求地址原始 uri: http://localhost:8001;LoadBalancerClientFilter 会查看exchange的属性ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 的值如果该值的scheme是 lb,比如:lb://lxnmall-product ,它将会使用Spring Cloud的LoadBalancerClient 来将 lxnmall-product解析成实际的host和port,并替换掉 ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR 的内容。  

3、自定义路由断言工厂

自定义路由断言工厂步骤:

  • 继承 AbstractRoutePredicateFactory 类,重写 apply 方法的逻辑。

    • 命名需要以 RoutePredicateFactory 结尾

  • yml配置文件中配置

1)继承 AbstractRoutePredicateFactory 类,重写 apply 方法的逻辑

  1.  
    @Component
  2.  
    @Slf4j
  3.  
    public class LixiunanRoutePredicateFactory extends AbstractRoutePredicateFactory<CheckAuthRoutePredicateFactory.Config> {
  4.  
     
  5.  
    public LixiunanRoutePredicateFactory() {
  6.  
    super(Config.class);
  7.  
    }
  8.  
     
  9.  
    @Override
  10.  
    public Predicate<ServerWebExchange> apply(Config config) {
  11.  
    return new GatewayPredicate() {
  12.  
     
  13.  
    @Override
  14.  
    public boolean test(ServerWebExchange serverWebExchange) {
  15.  
    log.info("调用LixiunanRoutePredicateFactory");
  16.  
    //需要实现的断言
  17.  
    //......
  18.  
     
  19.  
    return true;
  20.  
    }
  21.  
    };
  22.  
    }
  23.  
    }
学新通

2) yml配置文件中配置

  1.  
    spring:
  2.  
    cloud:
  3.  
    gateway:
  4.  
    #设置路由:路由id、路由到微服务的uri、断言
  5.  
    routes:
  6.  
    - id: lixiunan_route #路由ID,全局唯一
  7.  
    uri: http://localhost:6666 #目标微服务的请求地址和端口
  8.  
    predicates:
  9.  
    # 测试:http://localhost:8888/lxn/...
  10.  
    - Path=/lxn/** #Path路径匹配
  11.  
    #自定义断言工厂
  12.  
    - name: Lixiunan

3-2、自定义过滤器工厂

自定义路由断言工厂步骤:

  • 继承AbstractNameValueGatewayFilterFactory
    • 名称必须要以GatewayFilterFactory结尾
  • yml配置使用

1) 继承AbstractNameValueGatewayFilterFactory

  1.  
    @Component
  2.  
    @Slf4j
  3.  
    public class LixiunanGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
  4.  
     
  5.  
    @Override
  6.  
    public GatewayFilter apply(NameValueConfig config) {
  7.  
    return (exchange, chain) -> {
  8.  
    log.info("调用LixiunanGatewayFilterFactory");
  9.  
    //实现适配逻辑
  10.  
    //......
  11.  
    //加入监听链中生效
  12.  
    return chain.filter(exchange);
  13.  
    };
  14.  
    }
  15.  
    }
学新通

2)yml配置使用自定义适配器工厂

  1.  
    spring:
  2.  
    cloud:
  3.  
    gateway:
  4.  
    routes:
  5.  
    - id: lixiunan_route #路由ID,全局唯一
  6.  
    uri: http://localhost:8888 #目标微服务的请求地址和端口
  7.  
    #配置过滤器工厂
  8.  
    filters:
  9.  
    - Lixiunan

注:Gateway跨域配置(CORS Configuration) 

Spring Cloud Gateway

  1.  
    spring:
  2.  
    cloud:
  3.  
    gateway:
  4.  
    globalcors:
  5.  
    cors-configurations:
  6.  
    '[/**]':
  7.  
    allowedOrigins: "*"
  8.  
    allowedMethods:
  9.  
    - GET
  10.  
    - POST
  11.  
    - 。。。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggcccb
系列文章
更多 icon
同类精品
更多 icon
继续加载