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}
​
相关推荐
小bo波8 小时前
使用Thread子类创建线程 VS 使用Runnable接口创建线程的区别
java·多线程·thread·并发编程·runnable
SamDeepThinking8 小时前
高并发场景下,CompletableFuture与ForkJoinPool该如何取舍?
java·后端·面试
张不才11 小时前
CPU 100% 了怎么办?Java 性能排障的标准化操作
java·后端
shepherd11112 小时前
吞吐量提升 10 倍:高并发大批量数据处理任务的架构演进与性能调优
java·后端·架构
plainGeekDev15 小时前
单例模式 → object 声明
android·java·kotlin
用户2986985301416 小时前
Java 实现 Word 文档文本与图片提取的方法
java·后端
SimonKing17 小时前
铁子,IntelliJ IDEA 2026.1.3来了,升不升?
java·后端·程序员
咖啡八杯1 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
java小白小1 天前
SpringBoot(01): 初识SpringBoot,从Spring的痛点说起
spring boot