Spring Cloud Gateway使用和配置

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

gateway的使用导入依赖
复制代码
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
 </dependency>

注意:一定要排除掉 spring-boot-starter-web 依赖,否则启动报错,这个依赖里面集成了 spring-boot-starter-web 会导致依赖冲入。

2.编写相关配置
复制代码
server:
  port: 8888 # 统一端口方便前段人员调用
spring:
  application:
    name: mygateway-server
    gateway:
      globalcors:
        corsConfigurations:  # 这里是解决跨域问题
          '[/**]': # 匹配所有请求
            allowedOrigins: "*" #跨域处理 允许所有的域
            allowedHeaders: "*" # 所有的请求头
            allowedMethods: # 支持的方法
              - GET
              - POST
              - PUT
              - DELETE 
      routes:
        - id: app-routes #唯一的标识 用户自定义
          uri: lb://app-server
          predicates:
            - Path=/api/app/**,/api/tr/**  #映射的web访问地址
          filters:
            - RewritePath=/api/(?<segment>.*), /$\{segment}
​
相关推荐
m0_587383001 小时前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
·云扬·1 小时前
Prompt 工程与安全:从 System 设计到注入防御
java·人工智能·安全·ai·prompt
专注仿真1 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
Best-Wishes2 小时前
科来网络分析系统(技术交流版)使用说明书
开发语言·junit·php
代码不停2 小时前
自动化测试(java)
java·开发语言·selenium·自动化
鬼手点金4 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
爱上网的小舟4 小时前
星际战士2启动错误怎么办?5种游戏报错解决方法
开发语言·php
paopaokaka_luck5 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
阿里嘎多学长5 小时前
2026-10-12 GitHub 热点项目精选
开发语言·程序员·github·代码托管
SL_staff5 小时前
JVS混合架构实践:能力引擎+动态模型如何支撑企业级可持续交付
spring boot·低代码·全栈