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 教程快速入门 资源下载: ...
随机推荐
- 在Intellij IDEA中修改模板中user变量名称
在Intellij IDEA中的注释模板中的${user}名称是根据当前操作系统的登录名来取的,有时候登录名称和我们实际的user名称并不相同. 修改方法如下: 方法一:可以在settings的fil ...
- 关于Q-LEARNING的优化
Q-LEARNING 最后得到的一个图寻路最佳路径:---直接转化为图关于多顶点深度遍历热度传递 V(level+1) = 0.8 * Max(Vi(level)) 这个方法可以在O时间收敛 原方 ...
- http协议与websocket协议(转)
一.WebSocket是HTML5中的协议,支持持久连接:而Http协议不支持持久连接. 首先HTMl5指的是一系列新的API,或者说新规范,新技术.WebSocket是HTML5中新协议.新API. ...
- 【leetcode】290. Word Pattern
problem 290. Word Pattern 多理解理解题意!!! 不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致.疑惑!知道的可以分享一下下哈- 之前理解有误,应该 ...
- python 网络数据采集1
python3 网络数据采集1 第一部分: 一.可靠的网络连接: 使用库: python标准库: urllib python第三方库:BeautifulSoup 安装:pip3 install be ...
- Linux 针对nginx日志文件做ip防刷限制
针对nginx日志做ip访问限制 1.cat /var/log/server/nginx/access.log| awk -F '?' '/optionid/{print $1}'|awk '{pri ...
- [LeetCode&Python] Problem 404. Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- ISCC的 Web——WP
比赛已经结束了,自己做出来的题也不是很多,跟大家分享一下 第一题:比较数字大小 打开连接 在里面随意输入一个值,他会提示数字太小了 那么我们输入他允许的最大值试试 他还是提示太小了 我们知道做web‘ ...
- Python 3 运行 shell 命令
#python 3.5 , win10 引入包 #os.chdir('path') import osimport subprocess #https://docs.python.org/3.5/li ...
- java 彻底理解 byte char short int float long double
遇到过很多关于 数值类型范围的问题了,在这做一个总结,我们可以从多方面理解不同数值类型的所能表示的数值范围 在这里我们只谈论 java中的数值类型 首先说byte: 这段是摘自jdk中 Byte.ja ...