网站续费贵是重新做个好还是续费,房产网签流程图,移动互联网站开发,阿里云网站 模板建设GateWay的介绍与使用 文章目录 GateWay的介绍与使用一、GateWay介绍1. GateWay概述2. GateWay非阻塞异步模型3. Gateway工作流程 二、Gateway的使用三. GateWay常用的Predicate四. GateWay的Filter 一、GateWay介绍
1. GateWay概述
Gateway官网
Cloud全家桶中有个很重要的组…GateWay的介绍与使用 文章目录 GateWay的介绍与使用一、GateWay介绍1. GateWay概述2. GateWay非阻塞异步模型3. Gateway工作流程 二、Gateway的使用三. GateWay常用的Predicate四. GateWay的Filter 一、GateWay介绍
1. GateWay概述
Gateway官网
Cloud全家桶中有个很重要的组件就是网关在1.x版本中都是采用的Zuul网关;
但在2.x版本中zuul的升级一直跳票SpringCloud最后自己研发了一个网关替代Zuul那就是SpringCloud Gateway—句话gateway是原zuul1.x版的替代 示例pandas 是基于NumPy 的一种工具该工具是为了解决数据分析任务而创建的。 Gateway是在Spring生态系统之上构建的API网关服务基于Spring 5Spring Boot 2和Project Reactor等技术。
Gateway旨在提供一种简单而有效的方式来对API进行路由以及提供一些强大的过滤器功能例如:熔断、限流、重试等。
SpringCloud Gateway是Spring Cloud的一个全新项目基于Spring 5.0Spring Boot 2.0和Project Reactor等技术开发的网关它旨在为微服务架构提供—种简单有效的统一的API路由管理方式。
SpringCloud Gateway作为Spring Cloud 生态系统中的网关目标是替代Zuul在Spring Cloud 2.0以上版本中没有对新版本的Zul 2.0以上最新高性能版本进行集成仍然还是使用的Zuul 1.x非Reactor模式的老版本。而为了提升网关的性能SpringCloud Gateway是基于WebFlux框架实现的而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。
Spring Cloud Gateway的目标提供统一的路由方式且基于 Filter链的方式提供了网关基本的功能例如:安全监控/指标和限流。
作用
方向代理鉴权流量控制熔断日志监控…
微服务架构中网关的位置
2. GateWay非阻塞异步模型
有Zuull了怎么又出来Gateway我们为什么选择Gateway? netflix不太靠谱zuul2.0一直跳票迟迟不发布。 一方面因为Zuul1.0已经进入了维护阶段而且Gateway是SpringCloud团队研发的是亲儿子产品值得信赖。而且很多功能Zuul都没有用起来也非常的简单便捷。Gateway是基于异步非阻塞模型上进行开发的性能方面不需要担心。虽然Netflix早就发布了最新的Zuul 2.x但Spring Cloud貌似没有整合计划。而且Netflix相关组件都宣布进入维护期不知前景如何?多方面综合考虑Gateway是很理想的网关选择。 SpringCloud Gateway具有如下特性 基于Spring Framework 5Project Reactor和Spring Boot 2.0进行构建动态路由能够匹配任何请求属性可以对路由指定Predicate (断言)和Filter(过滤器)集成Hystrix的断路器功能集成Spring Cloud 服务发现功能易于编写的Predicate (断言)和Filter (过滤器)请求限流功能支持路径重写。 SpringCloud Gateway与Zuul的区别 在SpringCloud Finchley正式版之前Spring Cloud推荐的网关是Netflix提供的Zuul。Zuul 1.x是一个基于阻塞I/O的API Gateway。Zuul 1.x基于Servlet 2.5使用阻塞架构它不支持任何长连接(如WebSocket)Zuul的设计模式和Nginx较像每次I/О操作都是从工作线程中选择一个执行请求线程被阻塞到工作线程完成但是差别是Nginx用C实现Zuul用Java实现而JVM本身会有第-次加载较慢的情况使得Zuul的性能相对较差。Zuul 2.x理念更先进想基于Netty非阻塞和支持长连接但SpringCloud目前还没有整合。Zuul .x的性能较Zuul 1.x有较大提升。在性能方面根据官方提供的基准测试,Spring Cloud Gateway的RPS(每秒请求数)是Zuul的1.6倍。Spring Cloud Gateway建立在Spring Framework 5、Project Reactor和Spring Boot2之上使用非阻塞API。Spring Cloud Gateway还支持WebSocket并且与Spring紧密集成拥有更好的开发体验
Zuul1.x模型 Springcloud中所集成的Zuul版本采用的是Tomcat容器使用的是传统的Serviet IO处理模型。
Servlet的生命周期servlet由servlet container进行生命周期管理。
container启动时构造servlet对象并调用servlet init()进行初始化container运行时接受请求并为每个请求分配一个线程一般从线程池中获取空闲线程然后调用service)container关闭时调用servlet destory()销毁servlet。 上述模式的缺点 Servlet是一个简单的网络IO模型当请求进入Servlet container时Servlet container就会为其绑定一个线程在并发不高的场景下这种模型是适用的。但是一旦高并发(如抽风用Jmeter压)线程数量就会上涨而线程资源代价是昂贵的上线文切换内存消耗大严重影响请求的处理时间。在一些简单业务场景下不希望为每个request分配一个线程只需要1个或几个线程就能应对极大并发的请求这种业务场景下servlet模型没有优势。
所以Zuul 1.X是基于servlet之上的一个阻塞式处理模型即Spring实现了处理所有request请求的一个servlet (DispatcherServlet)并由该servlet阻塞式处理处理。所以SpringCloud Zuul无法摆脱servlet模型的弊端。
Gateway模型 WebFlux是什么官方文档 传统的Web框架比如说: Struts2SpringMVC等都是基于Servlet APl与Servlet容器基础之上运行的。
但是在Servlet3.1之后有了异步非阻塞的支持。而WebFlux是一个典型非阻塞异步的框架它的核心是基于Reactor的相关API实现的。相对于传统的web框架来说它可以运行在诸如NettyUndertow及支持Servlet3.1的容器上。非阻塞式函数式编程(Spring 5必须让你使用Java 8)。
Spring WebFlux是Spring 5.0 引入的新的响应式框架区别于Spring MVC它不需要依赖Servlet APl它是完全异步非阻塞的并且基于Reactor来实现响应式流规范。 Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or when built as a WAR.link 3. Gateway工作流程
三大核心概念
Route(路由) - 路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如断言为true则匹配该路由redicate(断言) - 参考的是Java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由Filter(过滤) - 指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。 web请求通过一些匹配条件定位到真正的服务节点。并在这个转发过程的前后进行一些精细化控制。
predicate就是我们的匹配条件而fliter就可以理解为一个无所不能的拦截器。有了这两个元素再加上目标uri就可以实现一个具体的路由了 管网总结 客户端向Spring Cloud Gateway发出请求。然后在Gateway Handler Mapping 中找到与请求相匹配的路由将其发送到GatewayWeb Handler。
Handler再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post执行业务逻辑。
Filter在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等在“post”类型的过滤器中可以做响应内容、响应头的修改日志的输出流量监控等有着非常重要的作用。
核心逻辑路由转发 执行过滤器链。 二、Gateway的使用
1.新建Module - cloud-gateway-gateway9527
2.POM
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdLearnCloud/artifactIdgroupIdcom.lun.springcloud/groupIdversion1.0.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdcloud-gateway-gateway9527/artifactIddependencies!--gateway--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependency!--eureka-client--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency!-- 引入自己定义的api通用包可以使用Payment支付Entity --dependencygroupIdcom.lun.springcloud/groupIdartifactIdcloud-api-commons/artifactIdversion${project.version}/version/dependency!--一般基础配置类--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies/project3.YML
server:port: 9527spring:application:name: cloud-gatewayeureka:instance:hostname: cloud-gateway-serviceclient: #服务提供者provider注册进eureka服务列表内service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka4.主启动类
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.9527网关如何做路由映射? cloud-provider-payment8001看看controller的访问地址
getlb
我们目前不想暴露8001端口希望在8001外面套一层9527
6.YML新增网关配置
server:port: 9527spring:application:name: cloud-gateway
#############################新增网关配置###########################cloud:gateway: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-serviceclient: #服务提供者provider注册进eureka服务列表内service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka
注在gateway中配置uri配置有三种方式
ws(websocket)方式: uri: ws://localhost:9000http方式: uri: http://localhost:8130/lb(注册中心中服务名字)方式: uri: lb://brilliance-consumer
7.测试 启动7001
启动8001-cloud-provider-payment8001
启动9527网关
访问说明
添加网关前 - http://localhost:8001/payment/get/1 添加网关后 - http://localhost:9527/payment/get/1 两者访问成功返回相同结果
Gateway配置路由的两种方式 在配置文件yml中配置见上一章节
代码中注入RouteLocator的Bean
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
{Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){RouteLocatorBuilder.Builder routes routeLocatorBuilder.routes();routes.route(path_route_atguigu,r - r.path(/guonei).uri(http://news.baidu.com/guonei)).build();return routes.build();}
}
测试
浏览器输入http://localhost:9527/guonei返回http://news.baidu.com/guonei相同的页面。
GateWay配置动态路由 默认情况下Gateway会根据注册中心注册的服务列表以注册中心上微服务名为路径创建动态路由进行转发从而实现动态路由的功在这里插入代码片能不写死一个地址。
POM
!--eureka-client--
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId
/dependency
YML 需要注意的是uri的协议为lb表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri。
server:port: 9527spring: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-serviceclient: #服务提供者provider注册进eureka服务列表内service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka
测试
浏览器输入 - http://localhost:9527/payment/lb
结果
不停刷新页面8001/8002两个端口切换。
三. GateWay常用的Predicate
官方文档
Route Predicate Factories这个是什么 Spring Cloud Gateway matches routes as part of the Spring WebFlux HandlerMapping infrastructure. Spring Cloud Gateway includes many built-in route predicate factories. All of these predicates match on different attributes of the HTTP request. You can combine multiple route predicate factories with logical and statements. link Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。
Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个RoutePredicate工厂可以进行组合。
Spring Cloud Gateway创建Route 对象时使用RoutePredicateFactory 创建 Predicate对象Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories。 所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合并通过逻辑and。
常用的Route Predicate Factory
The After Route Predicate FactoryThe Before Route Predicate FactoryThe Between Route Predicate FactoryThe Cookie Route Predicate FactoryThe Header Route Predicate FactoryThe Host Route Predicate FactoryThe Method Route Predicate FactoryThe Path Route Predicate FactoryThe Query Route Predicate FactoryThe RemoteAddr Route Predicate FactoryThe weight Route Predicate Factory
讨论几个Route Predicate Factory The After Route Predicate Factory
spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:# 这个时间后才能起效- After2017-01-20T17:42:47.789-07:00[America/Denver]
可以通过下述方法获得上述格式的时间戳字符串
import java.time.ZonedDateTime;public class T2
{public static void main(String[] args){ZonedDateTime zbj ZonedDateTime.now(); // 默认时区System.out.println(zbj);//2021-02-22T15:51:37.48508:00[Asia/Shanghai]}
}The Between Route Predicate Factory
spring:cloud:gateway:routes:- id: between_routeuri: https://example.org# 两个时间点之间predicates:- Between2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
The Cookie Route Predicate Factory
spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- Cookiechocolate, ch.p
The cookie route predicate factory takes two parameters, the cookie name and a regular expression.
This predicate matches cookies that have the given name and whose values match the regular expression.
测试
# 该命令相当于发get请求且没带cookie
curl http://localhost:9527/payment/lb# 带cookie的
curl http://localhost:9527/payment/lb --cookie chocolatechip
The Header Route Predicate Factory
spring:cloud:gateway:routes:- id: header_routeuri: https://example.orgpredicates:- HeaderX-Request-Id, \d
The header route predicate factory takes two parameters, the header name and a regular expression.
This predicate matches with a header that has the given name whose value matches the regular expression.
# 带指定请求头的参数的CURL命令
curl http://localhost:9527/payment/lb -H X-Request-Id:123
其它的举一反三。
小结
说白了Predicate就是为了实现一组匹配规则让请求过来找到对应的Route进行处理。
四. GateWay的Filter
官方文档 Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories. 路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应路由过滤器只能指定路由进行使用。Spring Cloud Gateway内置了多种路由过滤器他们都由GatewayFilter的工厂类来产生。
Spring Cloud Gateway的Filter:
生命周期 prepost 种类具体看官方文档 GatewayFilter - 有31种GlobalFilter - 有10种 常用的GatewayFilterAddRequestParameter GatewayFilter
自定义全局GlobalFilter
两个主要接口介绍
GlobalFilterOrdered
能干什么
全局日志记录统一网关鉴权…
代码案例
GateWay9527项目添加MyLogGateWayFilter类
import lombok.extern.slf4j.Slf4j;
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
Slf4j
public class MyLogGateWayFilter implements GlobalFilter,Ordered
{Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain){log.info(***********come in MyLogGateWayFilter: new Date());String uname exchange.getRequest().getQueryParams().getFirst(uname);if(uname null){log.info(*******用户名为null非法用户o(╥﹏╥)o);exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);return exchange.getResponse().setComplete();}return chain.filter(exchange);}Overridepublic int getOrder(){return 0;}
}
测试
浏览器输入
http://localhost:9527/payment/lb - 反问异常http://localhost:9527/payment/lb?unameabc - 正常反问