Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway

作者:媒体转发 时间:2018-12-19 21:09

字号

Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway

Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使用阻塞 API,它不支持任何长连接,如 WebSockets,Spring Cloud Gateway 使用非阻塞 API,支持 WebSockets,支持限流等新特性。

Spring Cloud Gateway

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

Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

相关概念:

Route(路由):这是网关的基本构建块。它由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配。

Predicate(断言):这是一个 Java 8 的 Predicate。输入类型是一个 ServerWebExchange。我们可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。

Filter(过滤器):这是org.springframework.cloud.gateway.filter.GatewayFilter的实例,我们可以使用它修改请求和响应。

工作流程:

Spring Cloud 终于按捺不住推出了自己的服务网关 Gateway

客户端向 Spring Cloud Gateway 发出请求。如果 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。

过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。

Spring Cloud Gateway 的特征:

基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0

动态路由

Predicates 和 Filters 作用于特定路由

集成 Hystrix 断路器

集成 Spring Cloud DiscoveryClient

易于编写的 Predicates 和 Filters

限流

路径重写

快速上手

Spring Cloud Gateway 网关路由有两种配置方式:

在配置文件 yml 中配置

通过@Bean自定义 RouteLocator,在启动主类 Application 中配置

这两种方式是等价的,建议使用 yml 方式进配置。

使用 Spring Cloud Finchley 版本,Finchley 版本依赖于 Spring Boot 2.0.6.RELEASE。

<parent> 

    <groupId>org.springframework.boot</groupId> 

    <artifactId>spring-boot-starter-parent</artifactId> 

    <version>2.0.6.RELEASE</version> 

    <relativePath/> <!-- lookup parent from repository --> 

</parent> 

 

<dependencyManagement> 

    <dependencies> 

        <dependency> 

            <groupId>org.springframework.cloud</groupId> 

            <artifactId>spring-cloud-dependencies</artifactId> 

            <version>Finchley.SR2</version> 

            <type>pom</type> 

            <scope>import</scope> 

        </dependency> 

    </dependencies> 

</dependencyManagement> 

经测试 Finchley.RELEASE 有 bug 多次请求会报空指针异常,SR2 是 Spring Cloud 的最新版本。

添加项目需要使用的依赖包

<dependency> 

    <groupId>org.springframework.cloud</groupId> 

    <artifactId>spring-cloud-starter-gateway</artifactId> 

</dependency> 

Spring Cloud Gateway 是使用 netty+webflux 实现因此不需要再引入 web 模块。

我们先来测试一个最简单的请求转发。

server: 

  port: 8080 

spring: 

  cloud: 

    gateway: 

      routes: 

      - id: neo_route 

        uri:  

        predicates: 

        - Path=http://zhuanlan.51cto.com/spring-cloud 

各字段含义如下:

id:我们自定义的路由 ID,保持唯一

uri:目标服务地址

责任编辑:CQITer新闻报料:400-888-8888   本站原创,未经授权不得转载
继续阅读
热新闻
推荐
关于我们联系我们免责声明隐私政策 友情链接