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}
​
相关推荐
皮皮林5514 小时前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot
顺风尿一寸7 小时前
从 Java NIO poll 到 Linux 内核 poll:一次系统调用的完整旅程
java
程途知微8 小时前
JVM运行时数据区各区域作用与溢出原理
java
华仔啊10 小时前
为啥不用 MP 的 saveOrUpdateBatch?MySQL 一条 SQL 批量增改才是最优解
java·后端
xiaoye201812 小时前
Lettuce连接模型、命令执行、Pipeline 浅析
java
beata15 小时前
Java基础-18:Java开发中的常用设计模式:深入解析与实战应用
java·后端
Seven9716 小时前
剑指offer-81、⼆叉搜索树的最近公共祖先
java
雨中飘荡的记忆1 天前
保证金系统入门到实战
java·后端
Nyarlathotep01131 天前
Java内存模型
java