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 日

相关推荐

  • PyCharm破解工具合集|支持2025全年版本激活!

    免责声明:以下破解补丁与激活码均源自互联网公开渠道,仅供学习交流,禁止商业用途。若条件允许,请支持正版:https://panghu.hicxy.com/shop/?id=18 PyCharm 是 JetBrains 出品的一款跨平台 IDE,支持 Windows、macOS 与 Linux。本文将手把手教你用破解补丁永久解锁全部高级功能,无论你安装的是哪个…

    PyCharm激活码 2025 年 9 月 12 日
    11200
  • IDEA破解教程限时公开,0基础也能学会!

    声明:以下激活补丁与序列号均源自网络公开分享,仅供个人学习研究,禁止商用。若经济条件允许,请支持正版 JetBrains 全家桶!https://panghu.hicxy.com/shop/?id=18 先放张图镇楼:IDEA 2025.2.1 已顺利激活至 2099 年,稳! 下面用图文方式手把手演示最新版 IntelliJ IDEA 的完整激活流程。 前…

    IDEA破解教程 2025 年 9 月 25 日
    10800
  • 【2025年保姆级教程】IDEA 2025.1永久激活指南 – 一键破解无忧使用

    IntelliJ IDEA作为当今最受欢迎的Java开发工具,其强大的代码分析能力、智能提示和丰富的插件生态系统使其成为众多开发者的首选。然而,正版IDEA的价格对于很多个人开发者和学生来说确实不低。今天,我就为大家带来2025.1版本的永久激活方法,实测有效! 我发现很多小伙伴在网上找了很多教程却屡次失败,因此特意整理了这份详细的图文步骤,手把手教你如何激…

    IDEA破解教程 2025 年 4 月 28 日
    1.2K00
  • 全渠道申领官方idea激活码,权威破解教程合集

    声明:下文所述 IntelliJ IDEA 破解补丁与激活码均源自互联网公开分享,仅供个人学习与研究,禁止任何商业用途。若条件允许,请支持正版! JetBrains 出品的 IntelliJ IDEA 是一款跨平台(Windows / macOS / Linux)的旗舰级 IDE,功能全面、插件丰富。本文将手把手演示如何利用破解补丁实现“永久激活”,一次性解…

    IDEA破解教程 3天前
    1100
  • MySQL

    阿里云社区https://developer.aliyun.com/mirror 目录 一:数据库 1.1 二: MySQL数据库基本操作 2.1 创建数据库: 2.2 使用某个数据库: 2.3 删除数据库: 2.4 查询支持的存储引擎 2.5 创建表: 2.6 查看表结构: 2.7 查看表结构详细信息: 2.8 删除表: 三:表的操作 3.1 修改表名字:…

    未分类 2025 年 1 月 11 日
    33700

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信