小程序开发定制【Spring Cloud】教你十分钟学会Gateway~

  • 💂 个人主页: 
  • 🤟 版权: 本文由【小程序开发定制程序员爱摸鱼】原创、在CSDN首发、小程序开发定制需要转载请联系博主
  • 💬 小程序开发定制如果文章对你有帮助、欢迎关注+点赞+收藏(一键三连)哦
  • 💅 小程序开发定制想寻找共同成长的小伙伴,小程序开发定制可以互粉哦

💬文章目录

💅

💅

💅

💅

💅

💅

💅

💅

💅


1.1 概述

        Spring Cloud Gateway 是 Spring Cloud 小程序开发定制的一个全新项目,小程序开发定制该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor(小程序开发定制响应式编程) 等技术开发的,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

        Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 2.0之前的非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。


1.1.1 相关术语

Gateway 目标是替代 Zuul,所有基本特性差别不大,主要的区别,底层的通信框架。

术语描述
Route(路由)网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。
Predicate(断言)这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。
Filter(过滤器)和Zuul的过滤器在概念上类似,可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。

1.2 入门

        1.2.1 搭建环境

  • 创建项目:test-gateway-2.1

  • 修改pom,添加坐标
  1. <dependencies>
  2. <!-- 网关 -->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-gateway</artifactId>
  6. </dependency>
  7. <!-- nacos 服务发现 -->
  8. <dependency>
  9. <groupId>com.alibaba.cloud</groupId>
  10. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  11. </dependency>
  12. </dependencies>
  • 配置 yml文件:application.yml
  1. #端口号
  2. server:
  3. port: 10010
  4. spring:
  5. application:
  6. name: test-gateway
  7. cloud:
  8. nacos:
  9. discovery:
  10. server-addr: 127.0.0.1:8848 #nacos服务地址
  11. gateway:
  12. discovery:
  13. locator:
  14. enabled: true #开启服务注册和发现的功能,自动创建router以服务名开头的请求路径转发到对应的服务
  • 编写启动类
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class TestGatewayApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(TestGatewayApplication.class, args);
  6. }
  7. }

        1.2.2 测试

http://localhost:10010/service-consumer/feign/echo/abc

http://localhost:10010/service-provider/echo/abc

2.1 routes路由

        2.1.2 路由匹配规则

  • gateWay的主要功能之一是转发请求,转发规则的定义主要包含三个部分
组成描述
路由 Route路由是网关的基本单元, 由ID、URI、一组Predicate、一组Filter组成,根据Predicate进行匹配转发。
断言 Predicate路由转发的判断条件,目前SpringCloud Gateway支持多种方式, 常见如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式
过滤器 Filter过滤器是路由转发请求时所经过的过滤逻辑,可用于修改请求、响应内容

  • 修改yml文件,添加
    1. routes:
    2. - id: consumer #自定义
    3. uri: lb://service-consumer #访问路径
    4. predicates: #断言
    5. - Path=/consumer/**
    6. filters: #过滤器
    7. - StripPrefix=1

    2.2.1 predicates 断言

        在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件,来进行作为条件匹配到对应的路由。

简单来说, Predicate 就是一组匹配规则,方便请求过来时,匹配到对应的 Route 进行处理。

  • Spring Cloud GateWay 内置多种 Predicate ,如下:

规则实例
Path- Path=/gate/,/rule/
Before- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
After- After=2017-01-20T17:42:47.789-07:00[America/Denver]
Between- Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver]
Cookie- Cookie=chocolate, ch.p
Header- Header=X-Request-Id, \d+
Host- Host=
Method- Method=GET
Query- Query=baz
RemoteAddr- RemoteAddr=192.168.1.1/24

  • 实例1:通过请求参数匹配
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: query
  6. uri: http://www.czxy.com
  7. predicates:
  8. - Query=my,123
  9. #访问路径,有参数my将转发到www.czxy.com
  10. http://localhost:10010/?my=123
  • 实例2:通过请求路径匹配
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: path
  6. uri: http://www.czxy.com
  7. predicates:
  8. - Path=/czxy/{flag}
  9. #
  10. http://localhost:10010/czxy/666
  • 总结:

    • 各种 Predicates 同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路由匹配。

    • 一个请求满足多个路由的断言条件时,请求只会被首个成功匹配的路由转发

7.3.3 Filter 网关过滤器

  • 路由过滤器允许以某种方式修改传入的HTTP请求或传出HTTP响应。

过滤规则实例说明
PrefixPath- PrefixPath=/app对所有的请求路径添加前缀app
RedirectTo- RedirectTo=302, 重定向,配置包含重定向的返回码和地址
RemoveRequestHeader- RemoveRequestHeader=X-Request-Foo去掉某个请求头信息
RemoveResponseHeade- RemoveResponseHeader=X-Request-Foo去掉某个响应头信息
RemoveRequestParameter- RemoveRequestParameter=red去掉某个请求参数信息
RewritePath- RewritePath=/where(?<segment>/?.), /test(?<segment>/?.)改写路径 /where/... 改成 /test/...
SetPath- SetPath=/{segment}设置请求路径,与RewritePath类似
SetRequestHeader- SetRequestHeader=X-Request-Red, Blue设置请求头信息
SetStatus- SetStatus=401设置响应状态码
StripPrefix- StripPrefix=2跳过指定路径
RequestSize- name: RequestSize args: maxSize: 5000000请求大小

  • 实例1:跳过指定路径
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: consumer
  6. uri: lb://service-consumer
  7. predicates:
  8. - Path=/consumer/**
  9. filters:
  10. - StripPrefix=1

  • 实例2:添加前缀
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: PrefixPath
  6. uri: lb://service-consumer
  7. predicates:
  8. - Path=/consumer/**
  9. filters:
  10. - StripPrefix=1
  11. - PrefixPath=/feign
  • 实例3:改写路径
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: RewritePath
  6. uri: lb://service-consumer
  7. predicates:
  8. - Path=/consumer/**
  9. filters:
  10. - RewritePath=/consumer(?<segment>/?.*), $\{segment}

到这里说明你已经学会了哦,努力学习!学无止境!!!

                                                                        


想要了解更多吗?没时间解释了,快来点一点!!!

————————————————
版权声明:本文为CSDN博主「程序员爱摸鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发