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}
​
相关推荐
Hello.Reader11 分钟前
算法基础(十三)——随机算法为什么有时主动引入随机性
java·数据库·算法
likerhood15 分钟前
ConcurrentHashMap底层数据结构和面试常见问题
java·数据结构·面试·hashmap
茉莉玫瑰花茶18 分钟前
LangGraph 拓展核心知识点
开发语言·windows·python
云烟成雨TD25 分钟前
Spring AI Alibaba 1.x 系列【52】Interrupts 中断机制:案例演示
java·人工智能·spring
老鱼说AI30 分钟前
现代 LangChain 开发指南:从 LCEL 原理到企业级 RAG 与 Agent 实战
java·开发语言·人工智能·深度学习·神经网络·算法·机器学习
Michelle802330 分钟前
25大数据 11-1 函数
开发语言·python
aini_lovee37 分钟前
C#与倍福PLC(通过ADS协议)通信上位机源程序实现
开发语言·c#
fie888941 分钟前
基于 MATLAB 的前景背景分割系统
开发语言·matlab
郝学胜-神的一滴1 小时前
Qt 入门 01-02: 开发环境搭建指南
开发语言·c++·qt·客户端
云烟成雨TD1 小时前
Spring AI Alibaba 1.x 系列【51】Graph 整体运行全流程
java·人工智能·spring