SpringCloud 第五期网关 GateWay

本文介绍了Spring Cloud Gateway作为Spring Cloud生态中的API网关,其基于WebFlux和Netty实现非阻塞模型。内容包括Gateway的核心概念,如路由、断言和过滤器,以及如何通过配置实现路由到服务实例。文中通过实例展示了如何创建路由,利用Path断言和CookieRoutePredicate,并实现全局过滤器进行权限校验。最后,讨论了动态路由和负载均衡的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gateway是什么?

springCloud GateWay是基于WebFlux框架实现的,底层是Netty。

SpringCloud GatewWay提供统一路由方式 基于Filter的方式提供了网关的基本功能。例如安全,性能,监控等。

Zuul是基于Sevelet2.5阻塞模型,GateWay是非阻塞模型

属于spring家族,能更好的与springCloud进行整合

路由三大核心概念 路由 断言 过滤器

路由 由id 目标url 一系列断言和过滤器组成,断言为true则路由匹配

断言 开发人员可以匹配HTTP请求中的所有内容(请求头,请求参数等),断言匹配则进行路由

过滤器 指的是spring框架中GateWayFilter实例,可以在请求路由之前或者之后队请求进行修改

过滤器 preFilter可以做参数校验 流量控制 权限校验 日志输出 协议转换等

postFilter可以做响应内容,响应头的修改,流量监控等

下面进入编码

新建 CloudGateWay9527模块

POM

<?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">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.liuxu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-gateway-9527</artifactId>
    <dependencies>
        <!--引入comm工程-->
        <dependency>
            <groupId>com.liuxu</groupId>
            <artifactId>cloud-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--eurekaClient-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值