SpringCloud Gateway 的使用(一)(建议收藏)|牛气冲天新年征文

简介: 一. Springboot 对应的 Springcloud 版本如下~官网地址 : spring.io/projects/sp…各位大佬 请注意下~ ,最后一句英文还说 不再支持 Dalston, Edgware, Finchley,Greenwich 这四个版本啦二. 使用pom 文件<properties> <spring.cloud-version>Hoxton.SR8</spring.cloud-version></properties><dependencyManagement> <dependencies> <dependency

一. Springboot 对应的 Springcloud 版本如下~


官网地址 : spring.io/projects/sp…


各位大佬 请注意下~  ,最后一句英文还说 不再支持 Dalston, Edgware,

Finchley,Greenwich 这四个版本啦


网络异常,图片无法展示
|


二. 使用


pom 文件


<properties>
    <spring.cloud-version>Hoxton.SR8</spring.cloud-version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码


yaml 文件


spring:
  cloud:
    gateway:
      routes:
        - id: weight_high
          uri: http://localhost:8080/
          predicates:
            - Path=/gateway/a
          filters:
#           去掉请求路径中的 第一个  如 /gateway/a 变成/a
            - StripPrefix=1
#           重新设置这个路由地址
            - SetPath=/debug
复制代码


三. 组成


官方文档


Route


The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.


对应该类(可对比上面 yaml 文件):


网络异常,图片无法展示
|


Predicate


This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.


当匹配时会拦截该请求


Filter


These are instances of Spring Framework GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.


可以配置添加更多过滤器~


四. 工作原理


由于 gateway 包中 使用了 WEBFLUX , 不能和这个 WEBMVC   同时存在,所以要移除 spring-boot-starter-web


网络异常,图片无法展示
|


工作原理图(来自官网):就一堆过滤器


网络异常,图片无法展示
|


五. Route Predicate Factories(★★★★★)


Route Predicate Factoriesdocs.spring.io/spring-clou…

可以看到这里有 11 种匹配模式~


网络异常,图片无法展示
|


匹配模式大全(重点!)


这里的匹配满足正则表达式


spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
#           匹配在这个时间之后的
            - After=2017-01-20T17:42:47.789-07:00[America/Denver]
#           匹配在这个时间之后的
            - Before=2017-01-20T17:42:47.789-07:00[America/Denver]
#           匹配这个时间中间的
            - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
#           匹配 cookie 为 chocolate=ch.p 时拦截该请求
            - Cookie=chocolate, ch.p
#           当 header 中有 X-Request-Id 该请求头时,且它的值匹配该正则时 拦截
            - Header=X-Request-Id, \d+
#           匹配该 host 地址~
            - Host=**.somehost.org,**.anotherhost.org
#           匹配该 请求方法
            - Method=GET,POST
#           匹配该路径
            - Path=/gateway/a
#           匹配该请求参数~  当请求参数为red 且值为 gree开头的~
            - Query=red, gree.
#           匹配 网址为  192.168.1.1 - 192.168.1.254
            - RemoteAddr=192.168.1.1/24
复制代码


根据权重匹配


spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2
复制代码


六. Gateway Filter Factories(★★★★★)


`Gateway Filter Factories : docs.spring.io/spring-clou…


数了下~ 官网居然多达 30filter 给你使用


网络异常,图片无法展示
|


网络异常,图片无法展示
|


可能在另外一篇博文中再写写~  ,  这里可以先简单参考下 4ye 在上面的 yaml 文件中的配置


七. Global Filters(★★★★★)


Global Filters: docs.spring.io/spring-clou…


全局过滤器~😄


网络异常,图片无法展示
|


自定义过滤器


@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("custom global filter");
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return -1;
    }



目录
相关文章
|
8天前
|
负载均衡 Java Nacos
SpringCloud基础2——Nacos配置、Feign、Gateway
nacos配置管理、Feign远程调用、Gateway服务网关
SpringCloud基础2——Nacos配置、Feign、Gateway
|
19天前
|
安全 Java 开发者
强大!Spring Cloud Gateway新特性及高级开发技巧
在微服务架构日益盛行的今天,网关作为微服务架构中的关键组件,承担着路由、安全、监控、限流等多重职责。Spring Cloud Gateway作为新一代的微服务网关,凭借其基于Spring Framework 5、Project Reactor和Spring Boot 2.0的强大技术栈,正逐步成为业界的主流选择。本文将深入探讨Spring Cloud Gateway的新特性及高级开发技巧,助力开发者更好地掌握这一强大的网关工具。
72 6
|
2月前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
290 15
|
2月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
61 3
|
2月前
|
Java 微服务 Spring
SpringCloud gateway自定义请求的 httpClient
SpringCloud gateway自定义请求的 httpClient
112 3
|
2月前
|
JSON 前端开发 Java
SpringCloud怎么搭建GateWay网关&统一登录模块
本文来分享一下,最近我在自己的项目中实现的认证服务,目前比较简单,就是可以提供一个公共的服务,专门来处理登录请求,然后我还在API网关处实现了登录拦截的效果,因为在一个博客系统中,有一些地址是可以不登录的,比方说首页;也有一些是必须登录的,比如发布文章、评论等。所以,在网关处可以支持自定义一些不需要登录的地址,一些需要登录的地址,也可以在网关处进行校验,如果未登录,可以返回JSON格式的出参,前端可以进行相关处理,比如跳转到登录页面等。
|
2月前
|
缓存 监控 Java
通用快照方案问题之Spring Boot Admin的定义如何解决
通用快照方案问题之Spring Boot Admin的定义如何解决
45 0
|
3月前
|
Java API 网络架构
Spring Cloud Gateway的高级配置与实践
Spring Cloud Gateway的高级配置与实践
|
2月前
|
Java API 网络架构
Spring Boot与Spring Cloud Gateway的集成
Spring Boot与Spring Cloud Gateway的集成
|
3月前
|
Java API 开发者
Spring Cloud Gateway中的GlobalFilter:构建强大的API网关过滤器
Spring Cloud Gateway中的GlobalFilter:构建强大的API网关过滤器
123 0