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}
​
相关推荐
多加点辣也没关系7 小时前
JavaScript|第41章:性能优化实战
开发语言·javascript·性能优化
Resean02237 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设8 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
码途行者8 小时前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang
23图8 小时前
C 语言开散列表(拉链法)拆解:链挂得没错,key=-1 会去读 T[-1]
c语言·开发语言·散列表
feixing_fx9 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
小飞在努力9 小时前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
Flynt10 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
夜郎king12 小时前
Shapefile 一键生成 GeoServer 样式:GeoTools + Spring Boot 实现 YSLD 自动生成器
spring boot·gis·geotools·ysld
饮水桶_T13 小时前
C++ stack & queue复习笔记
开发语言·笔记·算法