Spring Cloud下Gateway的崭新网关定位

一、概述

(1)定义阐释

  1. 官方站点:Spring Cloud Gateway<p>Spring Cloud下Gateway的崭新网关定位</p><p>Spring Cloud下Gateway的崭新网关定位</p>
  2. 体系中的定位<p>Spring Cloud下Gateway的崭新网关定位</p>

(2)微服务架构里网关的位置

<p>Spring Cloud下Gateway的崭新网关定位</p>

(3)具备的功能

  1. 反向代理
  2. 身份验证
  3. 流量调控
  4. 熔断机制
  5. 日志监测

(4)总结归纳

<p>Spring Cloud下Gateway的崭新网关定位</p><p>Spring Cloud下Gateway的崭新网关定位</p>

二、Gateway 三大核心要素

(1)官网总述

<p>Spring Cloud下Gateway的崭新网关定位</p>

(2)细分解读

2.2.1Route(路由)

路由是构建网关的基本组成部分,它由ID、目标URI以及一系列的断言和过滤器构成,当断言结果为true时就匹配该路由

2.2.2Predicate(断言)
  1. 参考的是Java8中的java.util.function.Predicate
  2. 开发人员能够匹配HTTP请求中的各类内容,比如请求头或者请求参数等,如果请求与断言相契合就进行路由转发
2.2.3Filter(过滤)

指的是Spring框架中GatewayFilter的实例,借助过滤器,可以在请求被路由前后对请求进行修改

(3)总结概括

<p>Spring Cloud下Gateway的崭新网关定位</p>

  1. web前端发起请求,通过一些匹配条件,定位到真实的服务节点。并且在这个转发过程的前后,进行一些精细化的控制
  2. predicate就是我们的匹配条件
  3. filter可以理解为一个功能强大的拦截器。有了这两个元素,再加上目标uri,就能够实现一个具体的路由

三、Gateway 工作流程

(1)官网总结

  1. 客户端的请求发送到Spring Cloud Gateway后,首先通过Gateway Handler Mapping来匹配路由,然后交给Gateway Web Handler。Handler会借助过滤器链,将请求发送到实际服务去处理业务并返回结果
  2. 过滤器分为两类
    1. Pre 过滤器:在代理请求之前执行,用于参数校验、权限校验、流量监控、日志输出、协议转换等
    2. Post 过滤器:在代理请求之后执行,能够修改响应内容或头信息,输出日志、监控流量等,作用十分关键

(2)核心逻辑

路由转发+断言判断+执行过滤器链

四、入门配置

(1)创建Module

cloud-gateway9527

(2)修改POM

<?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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.atguigu.cloud</groupId>
        <artifactId>mscloudV5</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>cloud-gateway9527</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--服务注册发现consul discovery,网关也要注册进服务注册中心统一管控-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <!-- 指标监控健康检查的actuator,网关是响应式编程删除掉spring-boot-starter-web dependency-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

(3)编写YML

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true
        service-name: ${spring.application.name}

(4)主启动类

<p>Spring Cloud下Gateway的崭新网关定位</p>

(5)业务类

无,不编写任何业务代码,网关与业务并无直接关联

(6)测试环节

  1. 首先启动8500服务中心Consul
  2. 然后启动9527网关进行入驻<p>Spring Cloud下Gateway的崭新网关定位</p>

五、9527 网关如何进行路由映射

(1)9527 网关路由映射的方式

5.1.1需求阐述

我们当下不希望暴露8001端口,期望在8001真正的支付微服务外面套一层9527网关

5.1.28001 新建 PayGateWayController

<p>Spring Cloud下Gateway的崭新网关定位</p>

5.1.3启动 8001

支付服务<p>Spring Cloud下Gateway的崭新网关定位</p>

5.1.48001 自测验证
  1. http://localhost:8001/pay/gateway/get/1
  2. http://localhost:8001/pay/gateway/info

(2)9527 网关 YML 新增配置

(3)测试 1

5.3.1启动 Consul8500 服务
5.3.2启动 8001 支付服务
5.3.3启动 9527 网关

<p>Spring Cloud下Gateway的崭新网关定位</p>

5.3.4访问说明
(1)添加网关前
  1. http://localhost:8001/pay/gateway/get/1
  2. http://localhost:8001/pay/gateway/info
(2)隐真示假,映射说明

<p>Spring Cloud下Gateway的崭新网关定位</p>

(3)添加网关后
  1. http://localhost:9527/pay/gateway/get/1
  2. http://localhost:9527/pay/gateway/info
5.3.5目前 8001 支付微服务前面添加 GateWay 成功

<p>Spring Cloud下Gateway的崭新网关定位</p>

(4)测试 2

5.4.1启动订单微服务进行测试,查看是否能通过网关?
(1)修改 cloud-api-commons

PayFeignApi接口<p>Spring Cloud下Gateway的崭新网关定位</p>

(2)修改 cloud-consumer-feign-order80

<p>Spring Cloud下Gateway的崭新网关定位</p>

(3)网关开启状态
  1. 测试通过<p>Spring Cloud下Gateway的崭新网关定位</p>
  2. http://localhost/feign/pay/gateway/get/1<p>Spring Cloud下Gateway的崭新网关定位</p>
  3. http://localhost/feign/pay/gateway/info<p>Spring Cloud下Gateway的崭新网关定位</p>
(4)网关关闭状态
  1. 测试通过<p>Spring Cloud下Gateway的崭新网关定位</p>
  2. http://localhost/feign/pay/gateway/get/1<p>Spring Cloud下Gateway的崭新网关定位</p>
  3. http://localhost/feign/pay/gateway/info<p>Spring Cloud下Gateway的崭新网关定位</p>
(5)结论
  1. 9527 网关是否启动,没有影响,o (π﹏π) o
  2. 目前的配置来看,网关被绕开了……

5.4.2正确做法

(1)同一家公司内部,系统内环境,直接访问微服务

<p>Spring Cloud下Gateway的崭新网关定位</p>

(2)不同公司有外部访问,系统外访问,先经网关再到服务

<p>Spring Cloud下Gateway的崭新网关定位</p><p>Spring Cloud下Gateway的崭新网关定位</p>

  1. 刷新 feign 接口 jar 包
  2. 重启 80 订单微服务<p>Spring Cloud下Gateway的崭新网关定位</p>
  3. 有网关正常 success<p>Spring Cloud下Gateway的崭新网关定位</p>
  4. 无网关异常<p>Spring Cloud下Gateway的崭新网关定位</p><p>Spring Cloud下Gateway的崭新网关定位</p>

(5)存在问题

请查看网关 9527 的 yml
配置,映射写死问题,^_^<p>Spring Cloud下Gateway的崭新网关定位</p>

六、GateWay

高级特性<p>Spring Cloud下Gateway的崭新网关定位</p><p>Spring Cloud下Gateway的崭新网关定位</p>

(1)Route 以微服务名 - 动态获取服务 URI

6.1.1痛点分析

<p>Spring Cloud下Gateway的崭新网关定位</p>

6.1.2定义说明<p>Spring Cloud下Gateway的崭新网关定位</p>

6.1.3解决 uri 地址写死问题
  1. 9527 修改前 YML<p>Spring Cloud下Gateway的崭新网关定位</p>
  2. 9527 修改后 YML<p>Spring Cloud下Gateway的崭新网关定位</p>
6.1.4测试 1
  1. 重启网关 9527,80/8001 保持不变
  2. http://localhost/feign/pay/gateway/get/1<p>Spring Cloud下Gateway的崭新网关定位</p>
6.1.5测试 2
  1. 如果将 8001 微服务 yml 文件端口修改为 8007,照样可以访问
  2. 我实际启动的程序是 main8001 但是端口名改为 8007
  3. 我们依据微服务名字,匹配查找即可uri: lb://cloud-payment-service<p>Spring Cloud下Gateway的崭新网关定位</p>

(2)Predicate 断言 (谓词)

6.2.1定义阐释
  1. Spring Cloud Gateway
  2. Route Predicate Factories这是什么东西?<p>Spring Cloud下Gateway的崭新网关定位</p>
6.2.2启动微服务 gateway9527,查看 IDEA

后台的输出<p>Spring Cloud下Gateway的崭新网关定位</p>

文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/12630.html

(0)
LomuLomu
上一篇 2025 年 6 月 24 日
下一篇 2025 年 6 月 25 日

相关推荐

  • 2025年最新PyCharm激活码及永久破解教程(支持2099年)

    JetBrains全家桶(包括IDEA、PyCharm、DataGrip、Goland等)最新破解方法来了!本文将以PyCharm为例,手把手教你如何永久激活至2099年。先看成功破解后的效果图: 准备工作:下载PyCharm安装包 已安装用户可跳过此步骤 访问PyCharm官网:https://www.jetbrains.com/pycharm/downl…

    2025 年 5 月 8 日
    53700
  • MySQL环境下JDBC编程起步详解

    文章标题: MySQL环境下JDBC编程入门全解 文章内容: 个人主页:♡爱幻想的人 欢迎 👍点赞 ➕关注 ❤️收藏 💬评论 目录 🌟一、何为JDBC? 🌟二、JDBC编程的步骤 ✨使用流程 ✨DriverManger 💫定义 💫DriverManger的主要作用 💫DriverManger的关键方法 💫使用示例 ✨DataSource 💥定义 💥使用方式 …

    2025 年 9 月 19 日
    22800
  • datagrip破解图文教学附激活码下载

    本教程适用于IDEA、PyCharm、DataGrip、Goland等,支持Jetbrains全家桶! 话不多说,先给大家看看最新版破解成功的截图,一眼就能看到有效期直接干到 2099 年,爽到飞起! 下面我会用图文结合的方式,手把手教你把 DataGrip 激活到 2099 年。这套方法同样向下兼容老版本,无论 Windows、macOS 还是 Linux…

    DataGrip激活码 2025 年 11 月 6 日
    30000
  • Java刷题常见的集合类,各种函数的使用以及常见的类型转化等等

    目录 前言 集合类 ArrayList 1. 创建和初始化 ArrayList 2.添加元素 add 3.获取元素 get 4.删除元素 remove 5.检查元素 6.遍历 ArrayList LinkedList Stack 1. 创建Stack对象 2. 压入元素 (push) 3. 弹出元素 (pop) 4. 查看栈顶元素 (peek) 5. 检查栈…

    2025 年 1 月 6 日
    55100
  • 亲测最新intellij idea激活码和全新破解教程

    本文同样适用于 PyCharm、DataGrip、GoLand 等 JetBrains 全家桶产品,步骤完全一致。 废话少说,先放一张成功激活到 2099 年的截图镇楼,爽就完事儿! 下面我会用图文一步步带你把 IDEA 激活到 2099 年,老版本同样适用,Windows / macOS / Linux 全平台都给你安排好了。 第一步:获取 IDEA 安装…

    IDEA破解教程 2025 年 10 月 21 日
    24000

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信