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 日

相关推荐

  • 2024 DataGrip最新激活码,DataGrip永久免费激活码2025-02-05 更新

    DataGrip 2024最新激活码 以下是最新的DataGrip激活码,更新时间:2025-02-05 🔑 激活码使用说明 1️⃣ 复制下方激活码 2️⃣ 打开 DataGrip 软件 3️⃣ 在菜单栏中选择 Help -> Register 4️⃣ 选择 Activation Code 5️⃣ 粘贴激活码,点击 Activate ⚠️ 必看!必看! 🔥 …

    2025 年 2 月 5 日
    43700
  • 🚀 2025年最新IDEA激活码分享:永久破解JetBrains全家桶教程(附破解补丁)

    💻 教程简介 本教程适用于JetBrains全家桶所有产品,包括IDEA、PyCharm、DataGrip、Goland等开发工具!✨ 先给大家看看最新IDEA版本破解成功的实锤截图👇,可以看到已经完美破解到2099年啦!🎉 接下来就手把手教你如何激活IDEA至2099年,这个方法同样适用于旧版本哦!不管是Windows、Mac还是Linux系统,统统搞定!…

    2025 年 5 月 19 日
    68300
  • 华为OD机试E卷 –游戏分组–24年OD统一考试(Java & JS & Python & C & C++)

    文章目录 题目描述 输入描述 输出描述 用例 题目解析 Js算法源码 python算法源码 java算法源码 c++算法源码 c算法源码 题目描述 部门准备举办一场王者荣耀表演赛,有 10 名游戏爱好者参与,分为两队,每队 5 人。每位参与者都有一个评分,代表着他的游戏水平。为了表演赛尽可能精彩,我们需要把 10 名参赛者分为示例尽量相近的两队。一队的实力可…

    未分类 2025 年 1 月 5 日
    51200
  • 2025年最新IDEA激活码及永久破解教程(支持Jetbrains全家桶)

    前言 本教程适用于IntelliJ IDEA、PyCharm、DataGrip、GoLand等Jetbrains系列开发工具,提供完整的破解方案。先展示最新IDEA版本破解成果,有效期至2099年! 下面将详细介绍如何实现IDEA永久激活,该方法同样适用于旧版本,兼容所有操作系统。 第一步:获取IDEA安装包 若尚未安装,请先下载官方安装包:1. 访问Jet…

    IDEA破解教程 2025 年 8 月 4 日
    5300
  • [JAVA] 第十一章:深入浅出解析”继承”核心概念(新手入门指南)

    目录导航1. 类继承机制1.1 继承原理剖析1.2 继承语法规范1.3 访问父类成员 1.3.1 子类访问父类字段①子父类字段无冲突②子父类字段同名super关键字的妙用 1.3.2 子类调用父类方法①方法名不同②方法名相同1.4 子类构造器特性🔍构造器特点精要1.5 super与this对比✔共同特征✖差异分析1.6 初始化流程详解1.7 protecte…

    2025 年 5 月 12 日
    8900

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信