本文共 11561 字,大约阅读时间需要 38 分钟。
Gateway新一代网关
https://github.com/Netflix/zuul/wiki
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
Cloud全家桶中有个很重要的组件就是网关,在1.x版本中都是采用的ZuuI网关;
但在2.x版本中,zuul的升级一 直跳票, SpringCloud最后自己研发了一个网关替代Zuul,那就是SpringCloud Gateway一句话: gateway是 原zuul1.x版的替代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通讯框架
源码架构
一方面因为Zuul1.0已经进 入了维护阶段,而且Gateway是 SpringCloud团队研发的,是亲儿子产品,值得信赖。而且很多功能Zuul都没有用起来也非常的简单便捷。
Gateway是基于异步非阻塞模型上进行开发的,性能方面不需要担心。虽然Netflix早就发布了最新的Zuul 2.x,但Spring Cloud貌似没有整合计划。而且Netflix相关组件都宣布进入维护期;不知前景如何?
多方面综合考虑Gateway是很理想的网关选择。
Spring Cloud Gateway具有如下特性:
Spring Cloud Gateway与Zuul的区别
在SpringCloud Finchley正式版之前,Spring Cloud推荐的网关是Netflix 提供的Zuul:Springcloud中所集成的Zuμ版本,采用的是Tomcat容器,使用的是传统的Servlet IO处理模型。
学过尚硅谷web中期课程都知道一个题目,Servlet的生命周期?servlet由servlet container进行生命周期管理。
container启动时构造servlet对象并调用servlet init()进行初始化;container运行时接受请求,并为每个请求分配一个线程 (一般从线程池中获取空闲线程) 然后调用service()。container关闭时调用servlet destory()销毁servlet;上述模式的缺点:
servlet是一个简单的网络IO模型,当请求进入servlet container时, servlet container就会为其绑定一个线程, 在并发不高的场景下这种模型是适用的。但是一旦高并发(此如抽风用jemeter压),线程数量就会上涨,而线程资源代价是昂贵的(. 上线文切换,内存消耗大)严重影响请求的处理时间。在一些简单业务场景下,不希望为每个request分配一个线程,只需要1个或几个线程就能应对极大并发的请求,这种业务场景下servlet模型没有优势所以Zuul 1.X是基于servlet之上的一个阻塞式处理模型,即spring实现了 处理所有request请求的一个servlet (DispatcherServlet) 并由该servlet阻塞式处理处理。所以Springcloud Zuul无法摆脱servlet模型的弊端
https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#spring-webflux
说明:
传统的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来实现响应式流规范。
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如断言为true则匹配该路由
参考的是Java8的java.util.function.Predicate 开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改.
客户端向Spring Cloud Gateway发出请求。然后在Gateway Handler Mapping中找到与请求相匹配的路由,将其发送到Gateway Web Handler。
Handler再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前( "pre” )或之后( "post" )执行业务逻辑。Filter在"pre" 类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,
在"post"类型的过滤器中可以做响应内容、响应头的修改,日志的输出,流量监控等有着非常重要的作用。我的理解:
gateway先根据断言条件找到指定的微服务,再路由转发,过滤可以在路由转发前或转发后加一些自己的东西。我认为网关是整合所有模块的不同端口,让他们对外暴露一个统一的端口来进行映射调用使用
uri就是整合的模块端口地址predicated是模块里的方法映射地址通过端口,我们可以直接选取方法映射地址来进行调用,而不用到其模块的信息。
路由转发+执行过滤器链
cloud-gateway-gateway9527
com.eiletxie.springcloud cloud-api-commons ${project.version} org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
server: port: 9527spring: application: name: cloud-gatewayeureka: instance: hostname: cloud-gateway-service client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka/
无
@SpringBootApplication@EnableEurekaClientpublic class GatewayMain9527 { public static void main(String[] args) { SpringApplication.run(GatewayMain9527.class,args); }} server: port: 9527spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称j进行路由 routes: - id: payment_route # 路由的id,没有规定规则但要求唯一,建议配合服务名 #匹配后提供服务的路由地址 uri: http://localhost:8001 predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_route2 uri: http://localhost:8001 predicates: Path=/payment/lb/** #断言,路径相匹配的进行路由eureka: instance: hostname: cloud-gateway-service client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://eureka7001.com:7001/eureka/
启动7001
启动8001
cloud-provider-payment8001启动9527
访问说明
Gateway网关路由有两种配置方式:
https://news.baidu.com/guonei
通过9527网关访问到外网的百度新闻网址
cloud-gateway-gateway9527
业务实现
package com.eiletxie.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;/** * @Author EiletXie * @Since 2020/3/12 15:01 */@Configurationpublic 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_eiletxie", 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_eiletxie2", r -> r.path("/guoji") .uri("http://news.baidu.com/guoji")).build(); return routes.build(); }} config
默认情况下Gatway会根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能
一个eureka7001+两个服务提供者8001/8002
org.springframework.cloud
server: port: 9527spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true # 开启从注册中心动态创建路由的功能,利用微服务名称进行路由 routes: - id: payment_route # 路由的id,没有规定规则但要求唯一,建议配合服务名 #匹配后提供服务的路由地址 #uri: http://localhost:8001 uri: lb://cloud-payment-service predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_route2 #uri: http://localhost:8001 uri: lb://cloud-payment-service predicates: Path=/payment/lb/** #断言,路径相匹配的进行路由eureka: instance: hostname: cloud-gateway-service client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://eureka7001.com:7001/eureka/
需要注意的是uri的协议lb,表示启用Gateway的负载均衡功能.
lb://serverName是spring cloud#gatway在微服务中自动为我们创建的负载均衡uri
http://localhost:9527/payment/lb
8001/8002两个端口切换启动我们的gateway9527
Spring Cloud Gateway创建Route对象时,使用 RoutePredicateFactory创建Predicate 对象,Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories.
所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。
加入curl返回中文乱码
https://blog.csdn.net/leedee/article/details/82685636YML
ALL
说白了,Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gatewayfilter-factories
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#global-filters
YML
implments GlobalFilter,OrderId
package com.atguigu.springcloud.filter;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.http.server.reactive.ServerHttpRequest;import org.springframework.stereotype.Component;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;import java.util.Date;/** * 全局自定义过滤器 * * @author zzyy * @version 1.0 * @create 2020/03/06 */@Component@Slf4jpublic class MyLogGatewayFilter implements GlobalFilter, Ordered { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { log.info("come in global filter: {}", new Date()); ServerHttpRequest request = exchange.getRequest(); String uname = request.getQueryParams().getFirst("uname"); if (uname == null) { log.info("用户名为null,非法用户"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } // 放行 return chain.filter(exchange); } /** * 过滤器加载的顺序 越小,优先级别越高 * * @return */ @Override public int getOrder() { return 0; }}