【SpringCloud框架之Gateway新一代网关】 您所在的位置:网站首页 spring框架的七个模块 【SpringCloud框架之Gateway新一代网关】

【SpringCloud框架之Gateway新一代网关】

2023-03-18 05:13| 来源: 网络整理| 查看: 265

本笔记内容为尚硅谷SpringCloud框架开发Gateway部分

目录

一、概述简介

1、概述 

2、作用

3、具有的特性

4、Spring Cloud Gateway 与 Zuul的区别

二、三大核心概念

1、Route(路由)

2、Predicate(断言)

3、Filter(过滤)

总体

三、Gateway工作流程

官网总结

核心逻辑

四、入门配置 

1、新建案例

1.新建Module

2.POM

3.YML

4.主启动类

5.测试

2、Gateway网关路由有两种配置方式

1.在配置文件yml中配置

2.代码中注入RouteLocator的Bean

五、通过微服务名实现动态路由

六、Predicate的使用

1、RoutePredicateFactories是什么

2、常用的Route Predicate

七、Filter的使用

1、Filter是什么

2、Spring Cloud Gateway的Filter 

1.生命周期

2.种类

3、常用的GatewayFilter

4、自定义过滤器

自定义全局GlobalFilter

一、概述简介

官网:Spring Cloud Gateway

1、概述 

Cloud全家桶中有个很重要的组件就是网关,在1.x版本中都是采用的Zuul网关;

但在2.x版本中,zuul的升级一直跳票,SpringCloud最后自己研发了一个网关替代Zuul,

那就是SpringCloud Gateway一句话:gateway是原zuul1.x版的替代

 Gateway是在Spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot 2和 Project Reactor等技术。

Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能, 例如:熔断、限流、重试等

SpringCloud Gateway 是 Spring Cloud 的一个全新项目,基于 Spring 5.0+Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。 SpringCloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。 Spring Cloud Gateway的目标提供统一的路由方式且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

SpringCloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。

2、作用 反向代理鉴权流量控制熔断日志监控

​ 

为什么选择Gateway?

3、具有的特性

Spring Cloud Gateway 具有如下特性:

基于Spring Framework 5, Project Reactor 和 Spring Boot 2.0 进行构建;

动态路由:能够匹配任何请求属性;

可以对路由指定 Predicate(断言)和 Filter(过滤器);

集成Hystrix的断路器功能;

集成 Spring Cloud 服务发现功能;

易于编写的 Predicate(断言)和 Filter(过滤器);

请求限流功能;

支持路径重写。

4、Spring Cloud Gateway 与 Zuul的区别

在SpringCloud Finchley 正式版之前,Spring Cloud 推荐的网关是 Netflix 提供的Zuul:

 

1、Zuul 1.x,是一个基于阻塞 I/ O 的 API Gateway

2、Zuul 1.x 基于Servlet 2. 5使用阻塞架构它不支持任何长连接(如 WebSocket) Zuul 的设计模式和Nginx较像,每次 I/ O 操作都是从工作线程中选择一个执行,请求线程被阻塞到工作线程完成,但是差别是Nginx 用C++ 实现,Zuul 用 Java 实现,而 JVM 本身会有第一次加载较慢的情况,使得Zuul 的性能相对较差。

3、Zuul 2.x理念更先进,想基于Netty非阻塞和支持长连接,但SpringCloud目前还没有整合。 Zuul 2.x的性能较 Zuul 1.x 有较大提升。在性能方面,根据官方提供的基准测试, Spring Cloud Gateway 的 RPS(每秒请求数)是Zuul 的 1. 6 倍。

4、Spring Cloud Gateway 建立 在 Spring Framework 5、 Project Reactor 和 Spring Boot 2 之上, 使用非阻塞 API。

5、Spring Cloud Gateway 还 支持 WebSocket, 并且与Spring紧密集成拥有更好的开发体验

 WebFlux是什么?   :文档 Web on Reactive Stack

传统的Web框架,比如说:struts2,springmvc等都是基于Servlet API与Servlet容器基础之上运行的。

但是

在Servlet3.1之后有了异步非阻塞的支持。而WebFlux是一个典型非阻塞异步的框架,它的核心是基于Reactor的相关API实现的。相对于传统的web框架来说,它可以运行在诸如Netty,Undertow及支持Servlet3.1的容器上。非阻塞式+函数式编程(Spring5必须让你使用java8)

 

Spring WebFlux 是 Spring 5.0 引入的新的响应式框架,区别于 Spring MVC,它不需要依赖Servlet API,它是完全异步非阻塞的,并且基于 Reactor 来实现响应式流规范。

二、三大核心概念 1、Route(路由)

    路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由。

2、Predicate(断言)

    参考的是Java8的java.util.function.Predicate。开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由。

3、Filter(过滤)

    指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。

总体

    web请求,通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。

    predicate就是我们的匹配条件;

    而filter,就可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了

三、Gateway工作流程 官网总结

核心逻辑

        路由转发+执行过滤器链

四、入门配置  1、新建案例 1.新建Module

名为:cloud-gateway-gateway9527

2.POM

mscloud03 com.atguigu.springcloud 1.0-SNAPSHOT 4.0.0 cloud-gateway-gateway9527 org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client com.atguigu.springcloud cloud-api-commons ${project.version} org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test 3.YML

我们目前不想暴露8001端口,希望在8001外面套一层9527

server: port: 9527 spring: application: name: cloud-gateway cloud: gateway: routes: - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: http://localhost:8001 #匹配后提供服务的路由地址 predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: http://localhost:8001 #匹配后提供服务的路由地址 predicates: - Path=/payment/lb/** # 断言,路径相匹配的进行路由 eureka: instance: hostname: cloud-gateway-service client: #服务提供者provider注册进eureka服务列表内 service-url: register-with-eureka: true fetch-registry: true defaultZone: http://eureka7001.com:7001/eureka

没有业务类

4.主启动类 package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class GateWayMain9527 { public static void main(String[] args) { SpringApplication.run(GateWayMain9527.class,args); } } 5.测试 启动7001启动8001:cloud-provider-payment8001启动9527网关

访问说明

添加网关前-http://localhost:8001/payment/get/31

添加网关后-http://localhost:9527/payment/get/31

2、Gateway网关路由有两种配置方式 1.在配置文件yml中配置

见前面的步骤

2.代码中注入RouteLocator的Bean

官网案例:

 

自己写一个通过9527网关访问到外网的百度新闻网址http://news.baidu.com/guonei 

当访问地址 http://localhost:9527/guonei时会自动转发到地址:http://news.baidu.com/guonei

cloud-gateway-gateway9527的config包下创建一个GateWayConfig.java

package com.atguigu.springcloud.config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class GateWayConfig { /** * 配置了一个id为route-name的路由规则, * 当访问地址 http://localhost:9527/guonei时会自动转发到地址:http://news.baidu.com/guonei * @param builder * @return */ @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { RouteLocatorBuilder.Builder routes = builder.routes(); routes.route("path_route_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guonei")).build(); return routes.build(); } @Bean public RouteLocator customRouteLocator2(RouteLocatorBuilder builder) { RouteLocatorBuilder.Builder routes = builder.routes(); routes.route("path_route_atguigu2", r -> r.path("/guoji").uri("http://news.baidu.com/guoji")).build(); return routes.build(); } } 五、通过微服务名实现动态路由

默认情况下Gateway会根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能。

启动:一个eureka7001 + 两个服务提供者8001/8002

1.POM:

org.springframework.cloud spring-cloud-starter-netflix-eureka-client

2.YML:

需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。

lb://serviceName是spring cloud​gateway在微服务中自动为我们创建的负载均衡uri

server: port: 9527 spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由 routes: - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8001 #匹配后提供服务的路由地址 uri: lb://cloud-payment-service #匹配后提供服务的路由地址 predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8001 #匹配后提供服务的路由地址 uri: lb://cloud-payment-service #匹配后提供服务的路由地址 predicates: - Path=/payment/lb/** # 断言,路径相匹配的进行路由 eureka: instance: hostname: cloud-gateway-service client: #服务提供者provider注册进eureka服务列表内 service-url: register-with-eureka: true fetch-registry: true defaultZone: http://eureka7001.com:7001/eureka

3.测试

访问:fhttp://localhost:9527/payment/lb     8001/8002两个端口切换

六、Predicate的使用

http://localhost:9527/payment/lb8001/8002两个端口切换启动我们的gateway9527发现

1、RoutePredicateFactories是什么

Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。

Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个Route Predicate工厂可以进行组合

 

Spring Cloud Gateway 创建 Route 对象时, 使用 RoutePredicateFactory 创建 Predicate 对象,Predicate 对象可以赋值给 Route。 Spring Cloud Gateway 包含许多内置的Route Predicate Factories。

 

所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。

2、常用的Route Predicate

具体见:【SpringCloud框架Gateway网关常用的Route Predicate】_蛋饼吧的博客-CSDN博客

 Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理。

七、Filter的使用 1、Filter是什么

路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。

Spring Cloud Gateway 内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生

2、Spring Cloud Gateway的Filter  1.生命周期 prepost 2.种类

GatewayFilter​

官网:Spring Cloud Gateway

 

GlobalFilter

3、常用的GatewayFilter

AddRequestParameter

YML

server: port: 9588 spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true #开启从注册中心动态创建路由的功能 lower-case-service-id: true #使用小写服务名,默认是大写 routes: - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 uri: lb://cloud-provider-payment #匹配后的目标服务地址,供服务的路由地址 #uri: http://localhost:8001 #匹配后提供服务的路由地址 filters: - AddRequestParameter=X-Request-Id,1024 #过滤器工厂会在匹配的请求头加上一对请求头,名称为X-Request-Id值为1024 predicates: - Path=/paymentInfo/** # 断言,路径相匹配的进行路由 - Method=GET,POST eureka: instance: hostname: cloud-gateway-service client: #服务提供者provider注册进eureka服务列表内 service-url: register-with-eureka: true fetch-registry: true defaultZone: http://eureka7001.com:7001/eureka 4、自定义过滤器 自定义全局GlobalFilter

两个主要接口介绍  implements GlobalFilter,Ordered

作用:全局日志记录、统一网关鉴权

案例代码:filter包下MyLogGateWayFilter.java

ackage com.atguigu.springcloud.filter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.Date; @Component //必须加,必须加,必须加 public class MyLogGateWayFilter implements GlobalFilter,Ordered { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { System.out.println("time:"+new Date()+"\t 执行了自定义的全局过滤器: "+"MyLogGateWayFilter"+"hello"); String uname = exchange.getRequest().getQueryParams().getFirst("uname"); if (uname == null) { System.out.println("****用户名为null,无法登录"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } @Override public int getOrder() { return 0; } }

测试

启动

正确访问测试:http://localhost:9527/payment/lb?uname=z3

错误访问测试:http://localhost:9527/payment/lb (没有参数uname、无法正常使用转发)

结束!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有