基本概念
1.springCloud网关Zuul和GateWay区别
相同点:
1、底层都是servlet
2、两者均是web网关,处理的是http请求
不同点:
1、内部实现:
gateway对比zuul多依赖了spring-webflux,在spring的支持下,功能更强大,内部实现了限流、负载均衡等,扩展性也更强,但同时也限制了仅适合于Spring Cloud套件
zuul则可以扩展至其他微服务框架中,其内部没有实现限流、负载均衡等。
2、是否支持异步
zuul仅支持同步
gateway支持异步。理论上gateway则更适合于提高系统吞吐量(但不一定能有更好的性能),最终性能还需要通过严密的压测来决定
3、框架设计的角度
gateway具有更好的扩展性,并且其已经发布了2.0.0的RELESE版本,稳定性也是非常好的
4、性能
WebFlux 模块的名称是 spring-webflux,名称中的 Flux 来源于 Reactor 中的类 Flux。Spring webflux 有一个全新的非堵塞的函数式 Reactive Web 框架,可以用来构建异步的、非堵塞的、事件驱动的服务,在伸缩性方面表现非常好。使用非阻塞API。 Websockets得到支持,并且由于它与Spring紧密集成,所以将会是一个更好的 开发 体验。
Zuul 1.x,是一个基于阻塞io的API Gateway。Zuul已经发布了Zuul 2.x,基于Netty,也是非阻塞的,支持长连接,但Spring Cloud暂时还没有整合计划。
2.Spring Cloud Gataway的核心概念
Spring Cloud Gataway有几个核心组成:
-
Filter(过滤器):
Spring Cloud Gateway的Filter和Zuul的过滤器类似
,可以在请求发出前后进行一些业务上的处理 ,这里分为两种类型的Filter,分别是Gateway Filter网关filter和Global Filter全局Filter,
他们的区别在后续会讲到。
-
Route(路由):
网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。说白了就是把url请求路由到对应的资源(服务),或者说是一个请求过来Gateway应该怎么把这个请求转发给下游的微服务,转发给谁。
-
Predicate(断言/url判断):
这是一个 Java 8 的 Predicate,可以使用它来匹配来自 HTTP 请求的任何内容
,例如 headers 或参数。断言的输入类型是一个 ServerWebExchange。简单理解就是处理HTTP请求的匹配规则,在什么样的请情况下才能命中资源继续访问。
3.Spring Cloud Gateway的工作方式
客户端向Spring Cloud Gateway发出请求。然后在Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到GatewayWeb Handler。
Handler再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。
过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post")执行业务逻辑。
Filter在“pre”类型的过滤器可以做参数校验、权限校验、流量监控、日志输出、协议转换等,在“post”类型的过滤器中可以做响应内容、响应头的修改,日志的输出,流量监控等有着非常重要的作用。
Spring Cloud Gataway入门
第一步:创建工程springcloud-gateway-server
第二步:导入依赖
<!--服务注册与发现-->
<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>
第三步:主配置类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApp {
public static void main(String[] args) {
SpringApplication.run(GatewayApp.class, args);
}
}
第四步:配置yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1010/eureka/
instance:
instance-id: gateway-server
spring:
application:
name: gateway-server
cloud:
gateway:
discovery:
locator:
enabled: false
lower-case-service-id: true
routes:
- id: user-server
uri: lb://user-server
predicates:
- Path=/user/**
filters:
- StripPrefix=2
server:
port: 1060
这里除了要注册到Eureak以外,还需要配置Gataway的路由
-
spring.cloud.gateway.discovery.locator.enabled=false
: 不开放服务名访问方式 -
spring.cloud.gateway.discovery.locator.lower-case-service-id: true
忽略服务名大小写,大写小写都可以匹配 -
spring.cloud.gateway.routes.id
: 指定了路由的服务名,可以自己定义 -
spring.cloud.gateway.routes.uri=lb://user-server
: 去注册中心找服务,采用负载均衡的方式请求。其实就是找要调用的服务。 -
spring.cloud.gateway.routes.predicates
: 断言,这里使用的Path=/user/**,即匹配访问的路径如果匹配/user/就可以将请求路由(分发)到user-server这个服务上。 -
spring.cloud.gateway.routes.filters
:这里使用StripPrefix=1主要是处理前缀 /user ,访问目标服务的时候会去掉前缀访问。这个需要根据url情况来定义。
第五步:访问测试
启动网关访问:http://localhost:1060/user/xxx/user/33