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全家桶破解指南(含PyCharm/IDEA/DataGrip) 先给大家展示最新PyCharm版本成功破解的截图,可以看到授权有效期已延长至2099年! 下面将详细介绍如何通过简单步骤完成PyCharm永久激活,本方法适用于所有主流操作系统和版本。 准备工作:获取PyCharm安装包 已安装用户可跳过此步骤 访问JetBrains官网下载页…

    PyCharm激活码 2025 年 7 月 23 日
    1.1K00
  • 掌控Python多版本:pyenv的安装与运用

    标题:掌控Python多版本管理:pyenv的安装与操作 一、pyenv基本介绍 pyenv是一款用于管理Python不同版本的工具,它能让用户在同一台设备上安装并切换多种Python版本。其通过对环境变量的调整来达成版本的隔离,适用于开发、测试以及有兼容性要求的场景。 官方地址:https://github.com/pyenv/pyenv Windows版…

    2025 年 9 月 18 日
    6800
  • WebStorm激活成功但提示未注册?可能是这个问题!

    申明:本教程 WebStorm破解补丁、激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除。若条件允许,希望大家购买正版 ! 废话不多说,先上 WebStorm 2025.2.1 版本破解成功的截图,如下图,可以看到已经成功破解到 2099 年辣,舒服的很! 接下来就给大家通过图文的方式分享一下如何破解最新的WebStorm。 如果觉得…

    2025 年 9 月 28 日
    8600
  • 2025年最新PyCharm激活码分享 | 永久破解教程+注册码一键获取

    本方法适用于JetBrains全系列产品,包括PyCharm、IDEA、DataGrip、Goland等开发工具! 先给大家看看最新PyCharm版本成功激活的截图,有效期直达2099年,完全无忧使用! 下面我将用详细的图文步骤,手把手教你如何将PyCharm激活至2099年。 这个方法不仅适用于最新版本,之前的旧版本同样有效! 支持Windows/Mac/…

    PyCharm激活码 2025 年 7 月 30 日
    57500
  • 🚀 2025最新PyCharm永久激活教程|破解到2099年(附激活码+破解补丁)

    适用于Jetbrains全家桶(IDEA/PyCharm/DataGrip/Goland等),亲测有效!✨ 先晒个最新版PyCharm破解成功的截图,有效期直接拉到2099年,简直不要太爽!😎 下面就用最详细的图文教程,手把手教你如何永久激活PyCharm。这个方法全版本通用,无论你是: 🖥️ Windows/Mac/Linux系统 🔢 2024.3.5或更…

    2025 年 6 月 15 日
    2.4K00

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信