springCloud-2021.0.9 之 GateWay 示例

ops/2025/2/18 19:02:33/

文章目录

  • 前言
    • springCloud-2021.0.9 之 GateWay 示例
      • 1. GateWay 官网
      • 2. GateWay 三个关键名称
      • 3. GateWay 工作原理的高级概述
      • 4. 示例
        • 4.1. POM
        • 4.2. 启动类
        • 4.3. 过滤器
        • 4.4. 配置
      • 5. 启动/测试

前言

  如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
  而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!


springCloud202109__GateWay__7">springCloud-2021.0.9 之 GateWay 示例

这里我介绍一下,我用到的springCloud 是2021.0.9版本。
Maven 依赖管理配置引入 Spring Cloud 的 BOM(Bill of Materials)文件,主要作用如下:

  • 版本统一:通过引入 BOM 文件,您可以统一管理项目中所有 Spring Cloud 相关依赖的版本,避免版本冲突和不一致。

  • 简化依赖声明:在子模块中,您无需显式指定 Spring Cloud 相关依赖的版本号,Maven 会自动从 BOM 中获取版本信息。

与以前的引入方式相比,这种方法提供了更好的版本控制和依赖管理,特别是在处理多个子模块和复杂的依赖关系时,能够有效减少手动管理版本的复杂性。

1. GateWay 官网

下面是我用的版本官网api:

https://docs.spring.io/spring-cloud-gateway/docs/3.1.9/reference/html/

2. GateWay 三个关键名称

Route:网关的基本构建块。它由 ID、目标 URI、谓词集合和过滤器集合定义。如果聚合谓词为真,则路由匹配。

Predicate:这是一个Java 8 函数谓词。输入类型是Spring 框架ServerWebExchange。这允许您匹配 HTTP 请求中的任何内容,例如标头或参数。

Filter:这些是GatewayFilter使用特定工厂构建的实例。在这里,您可以在发送下游请求之前或之后修改请求和响应。

3. GateWay 工作原理的高级概述

下图提供了 Spring Cloud Gateway 工作原理的高级概述:

在这里插入图片描述
客户端向 Spring Cloud Gateway 发出请求。如果 Gateway Handler Mapping 确定请求与路由匹配,
则将其发送到 Gateway Web Handler。此处理程序通过特定于请求的过滤器链运行请求。过滤器被虚线分开的原因是过滤器可以在发送代理请求之前和之后运行逻辑。所有“前”过滤器逻辑都​​会执行。然后发出代理请求。发出代理请求后,运行“后”过滤器逻辑。

4. 示例

4.1. 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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.15</version></parent><groupId>org.example</groupId><artifactId>springCloud-Gateway</artifactId><version>1.0-SNAPSHOT</version><name>springCloud-Gateway</name><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><spring-cloud.version>2021.0.9</spring-cloud.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--作用: Spring Cloud Gateway 是一个基于 Spring WebFlux 和 Project Reactor 实现的 API 网关服务,它可以作为微服务架构的入口,处理路由请求、负载均衡、身份认证等功能。用途: 主要用于处理微服务的路由请求,你可以定义路由规则,决定哪些请求转发到哪些微服务。--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!--作用: 为你的应用程序提供 Eureka 客户端的支持,能够与 Eureka 服务发现注册中心交互。客户端可以向 Eureka 服务器注册自身,并从中获取其他服务实例的注册信息,实现服务发现和负载均衡。用途: 当应用启动时,它会自动注册到 Eureka Server,并能够根据其他服务的注册信息进行服务发现。--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--a作用: 提供了一组用于监控和管理应用程序的端点,比如健康检查、性能指标、日志级别调整等。它能够暴露很多应用的内部状态,并支持通过 HTTP、JMX 或其他方式访问这些信息。用途: 通过 Actuator,你可以轻松地监控和管理 Spring Boot 应用,结合 Spring Boot Admin 还能进行图形化管理。--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--作用: Sleuth 提供了分布式追踪的功能,可以在微服务架构中追踪请求的流转路径,帮助诊断和调试系统性能问题。Sleuth 会为每个请求生成追踪 ID 和 span ID,记录请求在不同微服务中的处理过程。用途: 在微服务架构中,用于监控和分析请求的处理流程,帮助你追踪请求跨多个服务的链路。--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-sleuth</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.6.15</version></plugin></plugins></build>
</project>
4.2. 启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class GateWayApplication {public static void main(String[] args) {SpringApplication.run(GateWayApplication.class, args);}}
4.3. 过滤器
package org.example.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.sleuth.CurrentTraceContext;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;@Component
public class ResponseFilter implements GlobalFilter , Ordered {private static final String CORRELATION_ID = "yzy-correlation-id";@Autowiredprivate CurrentTraceContext currentTraceContext;@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {return chain.filter(exchange).then(Mono.fromRunnable(() -> {ServerHttpResponse response = exchange.getResponse();response.getHeaders().add(CORRELATION_ID, currentTraceContext.context().traceId());}));}@Overridepublic int getOrder() {return -1;}
}
4.4. 配置
server:port: 8809netty:idle-timeout: 10m # Netty 服务器的空闲超时时间为 10 分钟,这决定了在没有网络活动时,连接会被关闭。
eureka:instance:prefer-ip-address: true # 优先ip注册client:service-url:defaultZone: http://127.0.0.1:8761/eureka # 注册中心地址
spring:application:name: springCloud-Gatewaycloud:gateway:httpclient:connect-timeout: 1000 # 配置全局 http 超时response-timeout: 5s # 配置全局 http 超时defaultFilters:- StripPrefix=1 # 默认移除前1个路径段routes:- id: springCloudServiceAuri: lb://SPRINGCLOUDSERVICEApredicates:- Path=/springCloudServiceA/**- id: springCloudServiceBuri: lb://SPRINGCLOUDSERVICEBpredicates:- Path=/springCloudServiceB/**

5. 启动/测试

如下图,启动成功!!

在这里插入图片描述
在这里插入图片描述

测试:
http://127.0.0.1:8809/springCloudServiceA/api/test/scan
在这里插入图片描述
请求链路:
postmain -> SPRINGCLOUD-GATEWAY->SPRINGCLOUDSERVICEA


http://www.ppmy.cn/ops/158244.html

相关文章

从 0 到 1 搭建个人博客:技术选型与实现全解析

开篇&#xff1a;梦想中的个人技术天地 在技术的浩瀚海洋里遨游&#xff0c;每个人都渴望有一片属于自己的小天地&#xff0c;能记录探索的足迹、分享独到的见解。搭建个人博客&#xff0c;就像是亲手打造一座知识的花园&#xff0c;每一篇文章都是一朵娇艳的花。今天&#xf…

物联网(IoT)如何与人工智能(AI)的结合

物联网&#xff08;IoT&#xff09;与人工智能&#xff08;AI&#xff09;的结合是当前技术发展的重要趋势&#xff0c;通常被称为 AIoT&#xff08;人工智能物联网&#xff09;。这种结合通过将AI的计算能力和数据分析能力与物联网的海量设备连接能力相结合&#xff0c;实现了…

LeapMotion第2代 Unity示范代码(桌面开发)

一、官方地址&#xff1a; 官网&#xff1a;https://www.ultraleap.com/ 驱动下载&#xff1a;https://leap2.ultraleap.com/downloads/leap-motion-controller-2/ docs地址&#xff1a;https://docs.ultraleap.com/xr-and-tabletop/tabletop/unity/getting-started/index.html…

DevExpress WPF中文教程:Grid - 如何创建未绑定列?

DevExpress WPF拥有120个控件和库&#xff0c;将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序&#xff0c;这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 无论是Office办公软件…

左移架构 -- 从攒批,湖仓到使用数据流的实时数据产品

编辑导读: 这篇文章翻译自 Kai Waehner的 《The Shift Left Architecture – From Batch and Lakehouse to Real-Time Data Products with Data Streaming》。文章通过数据产品的概念引出了如何创建可重复使用的数据产品使企业能够从当前和未来的数据中获得价值。基于构建数据产…

vue2 多页面pdf预览

使用pdfjs-dist预览pdf&#xff0c;实现预加载&#xff0c;滚动条翻页。pdfjs的版本很重要&#xff0c;换了好多版本&#xff0c;终于有一个能用的 node 20.18.1 "pdfjs-dist": "^2.2.228", vue页面代码如下 <template><div v-loading"loa…

全面理解-什么是尾递归优化?

尾递归&#xff08;Tail Recursion&#xff09; 是一种特殊的递归形式&#xff0c;其特点是递归调用是函数的 最后一步操作。尾递归可以被编译器优化为迭代形式&#xff0c;从而避免递归调用带来的栈溢出问题&#xff0c;并提升性能。 以下是尾递归的详细说明和优化原理&#…

【进阶】JVM篇

为什么学习jvm 1、面试的需要 学过java的程序员对jvm应该不陌生&#xff0c;程序员为什么要学习jvm呢&#xff1f;其实不懂jvm也可以照样写出优质的代码&#xff0c;但是不懂jvm会被大厂的面试官虐的体无完肤。 2、高级程序员需要了解 jvm作用 jvm负责把编译后的字节码转换…