Spring Cloud Zuul 快速入门
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/**
该配置定义了发往 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 快速入门的更多相关文章
- 笔记:Spring Cloud Zuul 快速入门
Spring Cloud Zuul 实现了路由规则与实例的维护问题,通过 Spring Cloud Eureka 进行整合,将自身注册为 Eureka 服务治理下的应用,同时从 Eureka 中获取了 ...
- 官方文档中文版!Spring Cloud Stream 快速入门
本文内容翻译自官方文档,spring-cloud-stream docs,对 Spring Cloud Stream的应用入门介绍. 一.Spring Cloud Stream 简介 官方定义 Spr ...
- Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门
服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...
- Spring Cloud(六)服务网关 zuul 快速入门
服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...
- SpringCloud---API网关服务---Spring Cloud Zuul
1.概述 1.1 微服务架构出现的问题 及 解决: 1.1.1 前言 每个微服务应用都提供对外的Restful API服务,它通过F5.Nginx等网络设备或工具软件实现对各个微服务的路由与负载 ...
- Spring Cloud Zuul API服务网关之请求路由
目录 一.Zuul 介绍 二.构建Spring Cloud Zuul网关 构建网关 请求路由 请求过滤 三.路由详解 一.Zuul 介绍 通过前几篇文章的介绍,我们了解了Spring Cloud ...
- Spring Cloud Zuul 那些你不知道的功能点
本文摘自于 <Spring Cloud微服务 入门 实战与进阶> 一书. 1. /routes 端点 当@EnableZuulProxy与Spring Boot Actuator配合使用时 ...
- Spring Cloud Zuul记录接口响应数据
系统在生产环境出现问题时,排查问题最好的方式就是查看日志了,日志的记录尽量详细,这样你才能快速定位问题. 如果需要在Zuul中进行详细的日志记录,这两种日志必不可少. API请求信息 API响应信息 ...
- Spring MVC 教程,快速入门,深入分析
http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门 资源下载: ...
随机推荐
- Access denied for user 'root'@'IP' (using password:YES)解决方法
在MySql的使用过程中,碰到“Access denied for user 'root'@'IP' (using password:YES)”的问题,使用以下语句修改后还是不行. GRANT ALL ...
- 鼠标滑过元素,div显示,并根据scrollTop向下移动
如上图所示,通道有很多个,表格只有一个. 注意:滑过通道时鼠标如果停留在上面,那么表格才显示,鼠标滑过表格时,表格不消失 <div id="lineContent"> ...
- AMAZON数据集
http://snap.stanford.edu/data/amazon/productGraph/categoryFiles/
- centos7 firewalld基本使用
firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disable ...
- github如何删除新建仓库(致新手)
github作为开发人员的必备用具.那么,作为一个新手如何删除github中建立的仓库呢? 1.以删除My test为例
- mysql对后空格不敏感 mysql数据库对空格的查询处理
mysql对后空格不敏感 https://blog.csdn.net/lzupb/article/details/73530589 结论:查询条件中建议对字符串做trim处理,在数据入库的时候最好也做 ...
- LeetCode - Minimum Area Rectangle
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these p ...
- 有关vuex的问题
在引入mapMutations时报错,解决方法: 1:npm install --save-dev babel-plugin-transform-object-rest-spread 2:在packa ...
- curl提示不支持https协议解决方法
根据网上的资料,这个问题的原因是因为在安装curl时使用默认安装,但是默认安装并不支持https协议 简单粗暴的办法就是,卸载重新安装curl(有一种方法是重新编译就可以了,然后使用编译后的可执行文件 ...
- malloc,calloc,alloca和free函数
void *malloc(size_t size)因为返回类型为空,所以可以赋值到任何类型指针,其分配的空间大小为size,返回新分配内存地址的起始处的指针,其所分配的内存未经初始化,若分配失败返回N ...