Spring Cloud Gateway 教程
2024-08-07 19:47:56作者:裴锟轩Denise
1. 项目介绍
Spring Cloud Gateway 是一款由 Pivotal 开发的微服务网关组件,它旨在成为 Spring Cloud 生态系统中的新一代 API 网关。该项目基于 Spring Framework 5 和 Project Reactor 提供了一个高度可配置的框架,用于构建和管理 API 路由。Spring Cloud Gateway 提供了以下核心功能:
- 动态路由:能够将请求映射到不同的微服务。
- 断言过滤器链:允许在请求经过路由之前添加预处理步骤,如认证、限流等。
- 自动负载均衡:通过与服务发现组件(如 Eureka、Consul 或 Nacos)集成,能够自动发现和路由到可用的微服务实例。
2. 项目快速启动
2.1 添加依赖
在 pom.xml 文件中添加 Spring Cloud Gateway 和相关依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.x</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2 启动配置
创建一个名为 application.yml 的配置文件,添加以下内容以配置路由:
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: example-route
uri: lb://example-service # 使用服务发现
predicates:
- Path=/example/**
2.3 主类
创建主类 Application.java,启用 Spring Boot 和 Gateway:
package com.example.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.4 启动应用
运行 Application 类,Spring Cloud Gateway 将启动并在指定端口监听,并根据配置将 /example/* 的请求路由到名为 example-service 的微服务。
3. 应用案例和最佳实践
3.1 身份验证
利用过滤器添加 JWT 认证:
@Bean
GlobalFilter jwtAuthenticationFilter(JwtUtils jwtUtils) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
if (!request.getHeaders().containsKey("Authorization")) {
return chain.filter(exchange);
}
// 验证并处理 JWT token...
return chain.filter(exchange.mutate().request(request).build());
};
}
3.2 日志监控
使用 Zipkin 或 Sleuth 追踪请求:
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedMethods: "*"
allowedHeaders: "*"
sleuth:
trace-id-generator:
name: random
zipkin:
base-url: http://localhost:9411/
3.3 限流
整合 Sentinel 进行流量控制:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
在配置文件中定义限流策略,并使用 @SentinelResource 注解标记业务方法。
4. 典型生态项目
- Spring Cloud Netflix Eureka: 服务发现组件,用于定位集群中的微服务实例。
- Spring Cloud OpenFeign: 用于声明式的 Web 服务客户端。
- Spring Cloud Consul: 另一种服务发现解决方案。
- Spring Cloud Config: 提供配置管理工具。
- Spring Cloud Bus: 实现事件和消息总线,通常与配置中心一起使用以实时刷新配置。
- Spring Cloud LoadBalancer: 提供了一种负载均衡机制,与 Spring Cloud Gateway 结合使用可以实现更灵活的服务调用。
欲了解更多详情,可以参考官方文档:https://github.com/spring-cloud/spring-cloud-gateway/blob/master/README.adoc
完成这些步骤后,您将拥有一个基础的 Spring Cloud Gateway 实例,可以继续根据项目需求进行定制和扩展。
登录后查看全文
热门项目推荐
相关项目推荐
Kimi-K2.5Kimi K2.5 是一款开源的原生多模态智能体模型,它在 Kimi-K2-Base 的基础上,通过对约 15 万亿混合视觉和文本 tokens 进行持续预训练构建而成。该模型将视觉与语言理解、高级智能体能力、即时模式与思考模式,以及对话式与智能体范式无缝融合。Python00- QQwen3-Coder-Next2026年2月4日,正式发布的Qwen3-Coder-Next,一款专为编码智能体和本地开发场景设计的开源语言模型。Python00
xw-cli实现国产算力大模型零门槛部署,一键跑通 Qwen、GLM-4.7、Minimax-2.1、DeepSeek-OCR 等模型Go06
PaddleOCR-VL-1.5PaddleOCR-VL-1.5 是 PaddleOCR-VL 的新一代进阶模型,在 OmniDocBench v1.5 上实现了 94.5% 的全新 state-of-the-art 准确率。 为了严格评估模型在真实物理畸变下的鲁棒性——包括扫描伪影、倾斜、扭曲、屏幕拍摄和光照变化——我们提出了 Real5-OmniDocBench 基准测试集。实验结果表明,该增强模型在新构建的基准测试集上达到了 SOTA 性能。此外,我们通过整合印章识别和文本检测识别(text spotting)任务扩展了模型的能力,同时保持 0.9B 的超紧凑 VLM 规模,具备高效率特性。Python00
Baichuan-M3-235BBaichuan-M3 是百川智能推出的新一代医疗增强型大型语言模型,是继 Baichuan-M2 之后的又一重要里程碑。Python00
VLOOKVLOOK™ 是优雅好用的 Typora/Markdown 主题包和增强插件。 VLOOK™ is an elegant and practical THEME PACKAGE × ENHANCEMENT PLUGIN for Typora/Markdown.Less00
项目优选
收起
deepin linux kernel
C
27
11
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
539
3.76 K
Ascend Extension for PyTorch
Python
348
414
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
889
609
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
338
185
openJiuwen agent-studio提供零码、低码可视化开发和工作流编排,模型、知识库、插件等各资源管理能力
TSX
986
252
openGauss kernel ~ openGauss is an open source relational database management system
C++
169
233
暂无简介
Dart
778
193
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.34 K
758
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
114
140