Spring Cloud Gateway - 学习记录

Spring Cloud 学习记录 - Gateway

一、路由规则说明

id:路由规则名称

uri:真实目标地址

predicates:请求地址匹配规则

filters:请求地址过滤规则,负责将请求地址映射到真实目标地址

 

示例一:直接映射

    routes:
        # 用户服务路由规则
        - id: user-service-route #路由规则名称
          uri: http://xx.xxx.xx.xx:pppp  #真实目标uri host
          predicates:  # 此处用来匹配请求地址
            - Path=/{s1}/{s2} # 路径匹配  #输入:http://localhost:9999/user/20  实际:http://xx.xxx.xx.xx:pppp/user/20

示例二:添加前缀映射

      routes:
        # 用户服务路由规则
        - id: user-service-route #路由规则名称
          uri: http://xx.xxx.xx.xx:pppp  #真实目标uri host
          predicates:  # 此处用来匹配请求地址
            # 路径匹配
            - Path=/{s1}
          filters: # 此处用来映射到真是请求地址
            - PrefixPath=/user #给请求地址添加前缀   #输入:http://localhost:9999/20  实际:http://xx.xxx.xx.xx:pppp/user/20

示例三:裁剪前缀映射

routes:
        # 用户服务路由规则
        - id: user-service-route #路由规则名称
          uri: http://xx.xxx.xx.xx:pppp  #真实目标uri host
          predicates:  # 此处用来匹配请求地址
            # 路径匹配
            - Path=/us/**
          filters: # 此处用来映射到真是请求地址
            - StripPrefix=1 #给请求地址裁剪前缀  #输入:http://localhost:9999/us/user/20  实际:http://xx.xxx.xx.xx:pppp/user/20

示例四:重定向映射

routes:
        # 用户服务路由规则
        - id: user-service-route #路由规则名称
          uri: http://xx.xxx.xx.xx:pppp  #真实目标uri host
          predicates:  # 此处用来匹配请求地址
            # 路径匹配
            - Path=/{s1}
          filters: # 此处用来映射到真是请求地址
            - RewritePath=(?<oldPath>^/), /user$\{oldPath} #输入:http://localhost:9999/20  实际:http://xx.xxx.xx.xx:pppp:9001/user/20

示例五:setpath映射

routes:
        # 用户服务路由规则
        - id: user-service-route #路由规则名称
          uri: http://xx.xxx.xx.xx:pppp  #真实目标uri host
          predicates:  # 此处用来匹配请求地址
            # 路径匹配
            - Path=/client/{s1}/{s2}
          filters: # 此处用来映射到真是请求地址
            - SetPath=/{s1}/{s2} #输入:http://localhost:9999/client/user/20  实际:http://xx.xxx.xx.xx:pppp/user/20

示例六:根据服务名映射

routes:
        # 用户服务路由规则
        - id: user-service-route #路由规则名称
          uri: lb://user-service  #真实目标uri service
          predicates:  # 此处用来匹配请求地址
            - Path=/us/**
          filters: # 此处用来映射到真是请求地址
            - StripPrefix=1 #给请求地址裁剪前缀

 

二、跨域配置

spring:
  cloud:
      gateway:
        globalcors:
           cors-configurations:
               '[/**]':
                  allowCredentials: true
                  allowedOrigins: "*"
                  allowedMethods: "*"
                  allowedHeaders: "*"

 

三、问题

nested exception is java.lang.NoClassDefFoundError: javax/validation/ValidationException

原因:项目中没有引用 spring-boot-starter-validation 包

As of #19550, 
Web and WebFlux starters do not depend on the validation starter by default anymore.
If your application is using validation features, you’ll need to manually add back a dependency on spring-boot-starter-validation in your build file.

解决方案:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

 

 

 

 

 

参考资料:

Spring Cloud Gateway

Spring Cloud Gateway -- 关于Path的配置

SpringCloud Gateway跨域配置

跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探

微服务网关实战——Spring Cloud Gateway

Spring Cloud Alibaba | Gateway基于Nacos动态网关路由

基于Nacos实现Spring Cloud Gateway实现动态路由

Spring Cloud Alibaba实战(七) - Gateway搭配Nacos实现动态路由

Spring Cloud Gateway跨域配置 - 配置后无效解决办法

Spring cloud gateway 详解和配置使用(文章较长)

SpringCloud gateway (史上最全)

聊聊spring cloud gateway的PrefixPath及StripPrefix功能

聊聊spring cloud的DiscoveryClientRouteDefinitionLocator

 

使用spring-boot-starter-validation做参数校验

 

一文带你了解 Spring 5.0 WebFlux 应用场景

SpringCloud实战十一:Gateway之 Spring Cloud Gateway

Spring Cloud gateway 网关服务 一

【开发必备】Spring Cloud Gateway

spring-cloud-gateway负载普通web项目

SpringCloud gateway (史上最全)

Spring Cloud GateWay 路由转发规则介绍

Spring Cloud gateway 网关服务 一 

体验SpringCloud Gateway

微服务网关实战——Spring Cloud Gateway

微服务网关 Spring Cloud Gateway

 

限流

Spring Cloud Gateway 之 限流

聊聊spring cloud的RequestRateLimiterGatewayFilter

spring cloud gateway之filter篇

Spring Cloud 微服务五:Spring cloud gateway限流

spring cloud gateway 实现接口限流(可以收藏保存)

 

熔断降级

spring cloud gateway 实现熔断降级攻略(原来gateway还集成Netty)

posted @ 2020-08-10 22:00  风过无痕521  阅读(245)  评论(0编辑  收藏  举报