Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了所有其他微服务的实例信息,这样的设计非常巧妙的将服务治理体系中维护的实例信息利用起来,使得维护服务实例的工作交给了服务治理框架自动完成,而对路由规则的维护,默认会将通过以服务名作为 ContextPath 的方式来创建路由映射,也可以做一些特别的配置,对于签名校验、登录校验等在微服务架构中的冗余问题,逻辑上来说,本质上和微服务应用自身的业务并没有多大的关系,所以他们完全可以独立成一个单独的服务存在,只是他们被剥离和独立出来之后,是在 API 网关统一调用来对微服务接口做前置过滤,以实现对微服务接口的拦截和校验。Spring Cloud Zuul 提供了一套过滤机制,可以很好的支持这样的任务,开发者可以通过使用 Zuul 来创建各种校验过滤器,然后指定哪些规则的请求需要执行校验逻辑,只有通过校验的才会被路由到具体的微服务接口,使得我们的微服务应用可以更专注与业务逻辑的开发,同时微服务的自动化测试也变得更容易实现。

快速入门

  • 创建一个基础的 Spring Boot 工程,命名为 gateway-zuul,并在 pom.xml 中引入 spring-cloud-starter-zuul 依赖,具体如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.lixue</groupId>

    <artifactId>gateway-zuul</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>gateway-zuul</name>

<parent>

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

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

<version>1.5.6.RELEASE</version>

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

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

<spring-cloud.version>Dalston.SR3</spring-cloud.version>

</properties>

<dependencies>

<dependency>

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

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

</dependency>

<dependency>

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

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

<scope>test</scope>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

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

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

<version>${spring-cloud.version}</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

</project>

对于 Spring-Cloud-starter-zuul 依赖,可以通过查看他的依赖内容了解到,该模块不仅包含了 zuul-core 核心依赖,还包括了一下重要依赖:

  • spring-cloud-starter-hystrix:该依赖用来在网关服务中实现对微服务转发时候的保护机制,通过线程隔离和断路器,防止微服务的故障引发 API 网关资源无法释放,从而影响其他应用的对外服务
  • spring-cloud-starter-ribbon:该依赖用来实现在网关服务进行路由转发时候,客户端负载均衡以及请求重试。
  • spring-boot-starter-actuator:该依赖用来提供常规的微服务管理端点,在 Spring Cloud Zuul 中还提供了 /routes 端点来返回当前的所有路由规则
  • 创建应用主类 GatewayZuulApplication ,使用 @EnableZuulProxy 注解开启Zuul的API网关服务功能,代码如下:

    @EnableZuulProxy

    @SpringBootApplication

    public class GatewayZuulApplication {

    public static void main(String[] args) {

    SpringApplication.run(GatewayZuulApplication.class, args);

    }

    }

  • 在 application.yml 中配置 Zuul 应用的基础信息,比如应用名称、端口号等,具体内容如下:

    server:

    port: 9300

    spring:

    application:

    name: gateway-zuul

    完成上面的步骤,基本的 Zuul 实现的 API 网关服务就构建完成了,下面将增加请求路由相关配置。

请求路由

传统路由方式

使用 Spring Cloud Zuul 实现路由功能非常简单,只需要增加一些关于路由的配置,就能实现传统的路由转发功能,比如:

zuul:

routes:

api:

path: /api/**

url: http://localhost:8080

该配置定义了发往 API 网关服务的请求中,所有符合 /api/** 规则的访问都被路由转发到 http://localhost:8080 地址上,配置属性中的 api 部分为路由的名字,可以任意定义,但是一组 path 和 url 映射关系的路由名要相同。

面向服务的路由

传统的路由配置方式对于我们来说并不友好,需要运维人员花费大量的时间来维护各个路由 path 与 url 的关系,为了解决这个问题 Spring Cloud Zuul 实现了与 Spring Cloud Eureka 的无缝整合,我们可以让路由的 path 不是映射具体的url,而是让映射到具体的服务,而具体的 url 则交给 Eureka 的服务发现机制去自动维护。

  • 为了与 Eureka 整合,我们需要增加 spring-cloud-starter-eureka 依赖,具体如下:

    <dependency>

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

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

    </dependency>

    调整在 application.yml 中配置,增加 eureka 的注册中心的配置,并配置服务路由,具体如下:

    eureka:

    client:

    service-url:

    defaultZone: http://eurekaserver2:9002/eureka,http://eurekaserver1:9001/eureka

    zuul:

    routes:

    api:

    path: /api/**

    serviceId: org.lixue.helloworld

    consumer:

    path: /consumer/**

    serviceId: eureka-feign-consumer

    # 增加 zuul 超时相关

    host:

    connect-timeout-millis: 10000

    socket-timeout-millis: 5000

    # 增加断路器超时时间

    hystrix:

    command:

    default:

    execution:

    isolation:

    thread:

    timeoutInMilliseconds: 60000

通过面向服务的路由配置方式,我们不需要再为各个路由维护微服务应用的具体实例的位置,而是通过简单的path 与 serviceId 的映射组成,是的维护供桌变得非常简单。

Spring Cloud Zuul 快速入门的更多相关文章

  1. 笔记:Spring Cloud Zuul 快速入门

    Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...

  2. 官方文档中文版!Spring Cloud Stream 快速入门

    本文内容翻译自官方文档,spring-cloud-stream docs,对 Spring Cloud Stream的应用入门介绍. 一.Spring Cloud Stream 简介 官方定义 Spr ...

  3. Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门

    服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...

  4. Spring Cloud(六)服务网关 zuul 快速入门

    服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...

  5. SpringCloud---API网关服务---Spring Cloud Zuul

    1.概述 1.1 微服务架构出现的问题   及  解决: 1.1.1 前言 每个微服务应用都提供对外的Restful API服务,它通过F5.Nginx等网络设备或工具软件实现对各个微服务的路由与负载 ...

  6. Spring Cloud Zuul API服务网关之请求路由

    目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 ​ 通过前几篇文章的介绍,我们了解了Spring Cloud ...

  7. Spring Cloud Zuul 那些你不知道的功能点

    本文摘自于 <Spring Cloud微服务 入门 实战与进阶> 一书. 1. /routes 端点 当@EnableZuulProxy与Spring Boot Actuator配合使用时 ...

  8. Spring Cloud Zuul记录接口响应数据

    系统在生产环境出现问题时,排查问题最好的方式就是查看日志了,日志的记录尽量详细,这样你才能快速定位问题. 如果需要在Zuul中进行详细的日志记录,这两种日志必不可少. API请求信息 API响应信息 ...

  9. Spring MVC 教程,快速入门,深入分析

    http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: ...

随机推荐

  1. django本身提供了runserver,为什么不能用来部署(runserver与uWSGI的区别)

    runserver方法是调试django时经常用到的运行方式,它使用django自带的. WSGI Server 运行,主要在测试和开发使用,并且runserver 开启的方式也是单线程. uWSGI ...

  2. 16 多校8 Rikka with Parenthesis II

    As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some mat ...

  3. C 语言经典面试题 —— 宏

    1. 运算符优先级与括号 #define Cube(a) a*a*a 无法解决 Cube(1+1) ⇒ 1+1*1+1*1+1 ⇒ 4,期待的应当是 8,故将其改造为 #define Cube(a) ...

  4. sqlplus/rman登录报权限错误ORA-01031/ORA-04005/0RA-00554

    安装Weblogic误操作对Oracle用户属组进行了修改 --本地sqlplus登录报错权限问题??? [oracle@enmo admin]$ sqlplus / as sysdba SQL*Pl ...

  5. Python之路,第六篇:Python入门与基础6

    python 列表 序列类型简介(sequence) 字符串str                         列表list                         元祖tuple 概念: ...

  6. lesson2-cnn-fastai

    %mkdir的做法glob('.jpg')np.random.permutation(图片)np.random.rename(,)#save_array:utils中,连接每个btch得到的数组#ke ...

  7. cell-augmented

    https://zhuanlan.zhihu.com/p/33147353 https://blog.csdn.net/thm225679/article/details/79689008 https ...

  8. linux 用户配制文件 用户增加及删除 以及用户属于的更改

    1.用户密码文件 /etc/passwd root  :   x   :    0    :       0    :          root      :     /root    :    / ...

  9. djjango cookie和session 的几种常用需求使用方法

    ------https://www.cnblogs.com/liuqingzheng/articles/8990027.html 需求情形一:正常设置cookie set_cookie(key,val ...

  10. java第一节感受

    第一节java课考试,感觉自从小学期和实习过了以后就等这个测试了,测试过了以后就是中秋节了,下周再上一节java又放国庆节了. 当时报软工的时候就早早地做好了心理准备,但是当亲身经历一遍后真的有了一种 ...