# SpringCloud 服务网关(ZuulGateWay)

Posted MarlonBrando1998

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了# SpringCloud 服务网关(ZuulGateWay)相关的知识,希望对你有一定的参考价值。

Zuul

概览

简单来说就是路由代理、路由转发,通过拦截Url请求,对Url请求地址代理。Zuul是从设备和网站到Netflix流媒体应用程序后端的所有请求的前门。作为边缘服务应用程序,Zuul旨在实现动态路由,监视,弹性和安全性。

功能

Zuul使用了各种不同类型的过滤器,这使我们能够快速,灵活地将功能应用于边缘服务。这些过滤器帮助我们执行以下功能:

  • 身份验证和安全性-识别每种资源的身份验证要求,并拒绝不满足要求的请求。
  • 洞察和监控-在边缘跟踪有意义的数据和统计信息,以便为我们提供准确的生产视图。
  • 动态路由-根据需要将请求动态路由到不同的后端群集。
  • 压力测试-逐渐增加到群集的流量以评估性能。
  • 减载-为每种类型的请求分配容量,并丢弃超出限制的请求。
  • 静态响应处理-直接在边缘构建一些响应,而不是将其转发到内部集群
  • 多区域弹性-在AWS区域之间路由请求,以多样化我们的ELB使用并将我们的优势拉近与我们的会员之间的距离

简单使用教程

Zuul服务端配置
  • 引入依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- 导入Spring Cloud的依赖管理 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- 整合zuul网关 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

    <!-- Eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

</dependencies>
  • zuul启动类
@EnableZuulProxy
@SpringBootApplication
@EnableEurekaClient
public class ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class,args);
    }
}
  • zuul 服务端配置
spring:
  application:
    name: zuul-server
server:
  port: 5003
#  Eureka 注册地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5001/eureka/
# Zuul的路由配置      
zuul:
  routes:
    # 路由1
    springboot-clientone:
      # 拦截 /springboot-clientone 下的所有url 到Eureka,中找到服务名为springboot-clientone 的服务进行访问地址
      path: /springboot-clientone/**
      serviceId: springboot-clientone

测试模块 springboot-clientone:已经注册到Eureka 中

在这里插入图片描述

Zuul 服务的完整配置代码见:https://gitee.com/Marlon_Brando/back/tree/master/springcloud-zuul


GateWay

概述

​ Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到API,并为它们提供跨领域关注,例如:安全性,监视/指标和弹性。

功能

Spring Cloud Gateway功能:

  • 建立在Spring Framework 5,Project Reactor和Spring Boot 2.0之上
  • 能够匹配任何请求属性上的路由。
  • 谓词和过滤器特定于路由。
  • 断路器集成。
  • Spring Cloud DiscoveryClient集成
  • 易于编写的谓词和过滤器
  • 请求速率限制
  • 路径改写

简单使用教程

GateWay服务端配置
  • 引入依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- 导入Spring Cloud的依赖管理 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- gateway -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!-- Eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
  • 启动类
@SpringBootApplication
@EnableEurekaClient
public class GateWayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }
}
  • 配置信息
server:
  port: 5004

spring:
  application:
    name: gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
          enabled: true
      routes:
        # 路由id
        - id: springboot-clientone_routh
          # 匹配后提供服务路由地址
          uri: lb://springboot-clientone
          # 开启过滤
          filters:
            - StripPrefix=1
          # 断言
          predicates:
            - Path=/clientone/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5001/eureka/
  instance:
    prefer-ip-address: true
测试

完整代码见地址:https://gitee.com/Marlon_Brando/back/tree/master/springcloud-gateway


Zuul 和 GateWay 对比

  • 摘自简书:https://www.jianshu.com/p/8d82c6c2e5ee

    ​ 两者均是web网关,处理的是http请求gateway对比zuul多依赖了spring-webflux,在spring的支持下,功能更强大,内部实现了限流、负载均衡等,扩展性也更强,但同时也限制了仅适合于Spring Cloud套件。

    ​ zuul则可以扩展至其他微服务框架中,其内部没有实现限流、负载均衡等gateway很好的支持异步,而zuul仅支持同步,那么理论上gateway则更适合于提高系统吞吐量(但不一定能有更好的性能),最终性能还需要通过严密的压测来决定从框架设计的角度看,gateway具有更好的扩展性,并且其已经发布了2.0.0的RELESE版本,稳定性也是非常好的编码上看,zuul更加简洁易懂

    ​ 总的来说,在微服务架构,如果使用了Spring Cloud生态的基础组件,则Spring Cloud Gateway相比而言更加具备优势,单从流式编程+支持异步上就足以让开发者选择它了。

  • 完整代码配置见地址:https://gitee.com/Marlon_Brando/back

以上是关于# SpringCloud 服务网关(ZuulGateWay)的主要内容,如果未能解决你的问题,请参考以下文章

搭建SpringCloud微服务框架:SpringCloud-Gateway 服务网关处理

springcloud学习之路: springcloud集成Zuul网关

SpringCloud服务网关Zuul分析①分发

SpringCloud:服务网关zuul-API网关

SpringCloud之网关服务(gateway)

SpringCloud(Hoxton.SR3)基础篇:第八章SpringCloud之Zuul网关原理及其配置