Spring Cloud Gateway-简介及基础应用

一、Spring Cloud Gateway简介

Spring Cloud Gateway是基于Spring WebFlux实现的网关组件,它的目标是提供一个简单、高效的API路由方式,并提供相应的安全、监控和故障恢复能力。

上图是官方给出的Spring Cloud Gateway的流程架构图,由图可以知道基本的请求转发过程:

请求到达网关后Gateway Handler Mapping根据请求参数匹配配置的路由规则;

  1. 匹配到路由规则后,请求与相应路由规则由Gateway Web Handler发到一系列Filter进行处理;
  2. 代理Filter将前边Filter处理过的请求转发到实际接口服务;
  3. 实际接口服务返回响应数据,再次经过一系列Filter的处理;
  4. 经过处理的响应数据返回到接口调用方。

Spring Cloud Gateway包含三个基本概念:

  1. Filter(过滤器):对请求或响应进行拦截处理,可以对请求头、请求体、响应头、响应体进行修改,可进行日志记录,可以进行请求状态告警等。
  2. Route(路由):用于配置路由规则,当断言为真时本条规则生效。
  3. Predicate(断言):用于确定生效的路由规则。

由于本文只介绍基本使用,因此不再展开说明(将在下一篇文章中详细介绍)。

二、基础应用示例

1、代码开发

创建一个Spring Boot项目,并添加相关依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>tech.ihai</groupId>
    <artifactId>ihai-gateway</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.cloud.gateway.version>3.0.3</spring.cloud.gateway.version>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>${spring.cloud.gateway.version}</version>
        </dependency>
    </dependencies>
</project>

创建启动类:

@SpringBootApplication
public class IHaiGateway {
    /**
     * 此方法是为了方便应用管理,非必须
     * @return
     */
    @Bean
    public ApplicationPidFileWriter pidFileWriter(){
        return new ApplicationPidFileWriter();
    }

    public static void main(String[] args) {
        SpringApplication.run(IHaiGateway.class, args);
    }
}

接着配置路由规则后启动就可以了,在此介绍通过文件和简单的代码两种配置方式。(动态配置等复杂使用方式将在后边的文章中向大家介绍)

2、路由配置(文件方式)

在application.yml中进行配置即可:

spring:
  cloud:
    gateway:
      routes:
        - id: baidu
          uri: https://www.baidu.com
          predicates:
            - Path=/s #访问http://127.0.0.1:8080/s会转发到http://www.baidu.com/s
        - id: csdn
          uri: https://www.csdn.net
          predicates:
            - Path=/csdn/**
          filters:
            - RewritePath=/csdn/?(?<segment>.*), /$\{segment} #访问http://127.0.0.1:8080/csdn会转发到http://www.csdn.net

3、路由配置(代码方式)

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
            .route("path_route", r -> r.path("/minibooks") //访问http://127.0.0.1:8080/minibooks会转发到https://www.infoq.cn/minibooks
                    .uri("https://www.infoq.cn"))
            .build();
}

以上就是Spring Cloud Gateway的基本使用,后续将通过系列文章深度开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EngineZhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值