Spring Cloud(四) API网关Zuul
前文回顾:
Spring Cloud(一)Eureka Server-单体及集群搭建
Spring Cloud(二) 配置Eureka Client
Spring Cloud(三) 熔断器Hystrix
一.API网关
API网关旨在用一套单一且统一的API入口点,来组合一个或多个内部API。
API网关定位为应用系统服务接口的网关,区别于网络技术的网关,但是原理是一样的。API网关统一服务入口,可方便实现对平台众多服务接口进行管控,如对访问服务的身份认证、防报文重放与防数据篡改、功能调用的业务鉴权,以及相应数据的脱敏、流量与并发控制,甚至基于API调用的计量或计费等。
API网关常用于以下场景:
黑白名单:实现通过IP地址控制禁止访问网关功能
日志:实现访问日志的记录,可用于分析访问,处理性能指标,同时将分析结果支持其它模块功能应用
协议适配:实现通信协议校验、适配转换的功能
身份认证:负责网关访问身份认证验证
计流限流:实现微服务访问流量计算,基于流量计算分析进行限流,可以定义多种限流规则
路由:是API网关很核心的模块功能,此模块实现根据请求锁定目标微服务,并将请求进行转发
API网关所带来的好处:
避免将内部信息泄露给外部
为微服务添加额外的安全层
支持混合通信协议
降低构建微服务的复杂性
微服务模拟与虚拟化
二.Zuul
Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。
Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。
Zuul可以通过加载动态过滤机制,从而实现以下各项功能:
验证与安全保障: 识别面向各类资源的验证要求并拒绝那些与要求不符的请求。
审查与监控: 在边缘位置追踪有意义数据及统计结果,从而为我们带来准确的生产状态结论。
动态路由: 以动态方式根据需要将请求路由至不同后端集群处。
压力测试: 逐渐增加指向集群的负载流量,从而计算性能水平。
负载分配: 为每一种负载类型分配对应容量,并弃用超出限定值的请求。
静态响应处理: 在边缘位置直接建立部分响应,从而避免其流入内部集群。
多区域弹性: 跨越AWS区域进行请求路由,旨在实现ELB使用多样化并保证边缘位置与使用者尽可能接近。
三.代码示例
1.pom中添加依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/></parent>
<properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR1</spring-cloud.version></properties>
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId><version>2.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-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>
2.Application启动类中添加注解
@EnableZuulProxy:支持网关路由
@SpringBootApplication
@EnableZuulProxy
public class Study05Application {public static void main(String[] args) {SpringApplication.run(Study05Application.class, args);}
}
3.配置文件
spring.application.name=gateway-service-zuul
server.port=8888
zuul.routes.api-a.path=/producer/**
zuul.routes.api-a.serviceId=spring-cloud-producer
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
4.测试
依次启动 spring-cloud-eureka
、 spring-cloud-producer
、gateway-service-zuul-eureka
,访问:http://localhost:8888/producer/hello?name=yfy
返回:hello yfy,welcome to Spring Cloud
测试集群,修改端口号,启动spring-cloud-producer
@RestController
public class HelloController {
@RequestMapping("/hello")public String index(@RequestParam String name) {return "hello " + name + ",welcome to Spring Cloud:product2";}
}
修改完成后启动spring-cloud-producer-2
,重启gateway-service-zuul-eureka
。测试多次访问http://localhost:8888/producer/hello?name=yfy
,依次返回:
hello yfy,welcome to Spring Cloud
hello yfy,welcome to Spring Cloud:product2
hello yfy,welcome to Spring Cloud
hello yfy,welcome to Spring Cloud:product2
...
说明通过zuul成功调用了producer服务并且做了均衡负载。
5.网关的默认路由规则
如果后端服务多达十几个的时候,每一个都这样配置也挺麻烦的,spring cloud zuul已经帮我们做了默认配置。默认情况下,Zuul会代理所有注册到Eureka Server的微服务,并且Zuul的路由规则如下:
http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/**
会被转发到serviceId对应的微服务。
我们注销掉gateway-service-zuul-eureka
项目中关于路由的配置:
#zuul.routes.api-a.path=/producer/**
#zuul.routes.api-a.serviceId=spring-cloud-producer
重新启动后,访问http://localhost:8888/spring-cloud-producer/hello?name=yfy
,测试返回结果和上述示例相同,说明Spring cloud zuul默认已经提供了转发功能。
Spring Cloud(四) API网关Zuul相关推荐
- .NET Core + Spring Cloud:API 网关
API 网关是系统的唯一入口,调用任何服务的请求都需要经过网关层,最终才可能到达目标服务,既然是必经之路,那我们可以在网关层进行一些通用的操作,如:认证.鉴权.限流.智能路由.缓存.日志.监控.超时. ...
- Spring Cloud微服务网关Zuul过滤链和整合OAuth2+JWT入门实战
一.Spring Cloud Zuul 过滤链 1.1 工作原理 Zuul的核心逻辑是由一系列的Filter来实现的,他们能够在进行HTTP请求或者相应的时候执行相关操作.Zuul Filter的主要 ...
- Spring Cloud(六) 服务网关GateWay 入门
前文回顾: Spring Cloud(一)Eureka Server-单体及集群搭建 Spring Cloud(二) 配置Eureka Client Spring Cloud(三) 熔断器Hystri ...
- Spring Cloud 系列之 Netflix Zuul 服务网关(三)
本篇文章为系列文章,未读前几集的同学请猛戳这里: Spring Cloud 系列之 Netflix Zuul 服务网关(一) Spring Cloud 系列之 Netflix Zuul 服务网关(二) ...
- 6、API网关 Zuul
该栏目讲叙微服务概念.注册中心.负载均衡.配置中心.服务熔断.服务消费等知识 文章目录 简介 1.概述 2.场景 3.优点 Nginx 实现 API网关 Zuul 实现 API网关 1.路由配置规则 ...
- Spring cloud Gateway 服务网关 实战
Spring cloud Gateway 服务网关 一.简介 优点: 特性: 总结: 二.核心概念 三.路由规则 1.Path 2.Query 3.Method 4.Datetime 5.Romote ...
- Spring Cloud(10)——新一代网关Spring Cloud Gateway
文章目录 Spring Cloud(10)--新一代网关Spring Cloud Gateway 1.背景知识--API网关 2.Spring Cloud Gateway 详细概述 3.Spring ...
- Spring Cloud :Gateway 网关限流(五)
目录 一.概述 1. 为什么需要限流 二.限流算法 1. 计数器算法 2. 漏桶算法 3. 令牌桶算法 四.Gateway 限流 1. 添加依赖 2. 配置文件 3. 限流规则配置类 Spring C ...
- Spring Cloud学习笔记—网关Spring Cloud Gateway官网教程实操练习
Spring Cloud学习笔记-网关Spring Cloud Gateway官网教程实操练习 1.Spring Cloud Gateway介绍 2.在Spring Tool Suite4或者IDEA ...
最新文章
- iis下 ActiveSync插件无法访问(下)
- jQuery通过name获取值
- centosx64 6.3安装视频组件
- 比特币可视化工具_比特币再破1.2万大关 你还要做打工人吗?
- 传感器为什么在低量程偏差大_传感器复习
- python声音分类_Python音频信号分类MFCC特征神经网络
- 网页最少要有一个html标签,PHP_网页开发人员必须知道的10个不常用HTML标签, 网页开发人员常常希望能 - phpStudy...
- Java和poi导出excel报表
- linux suse 共享目录_SUSE环境下YAST源(连接共享目录)
- Redis 6.0 正式版终于发布了!除了多线程还有什么新功能?
- python距离向量路由算法_python算法练习——动态规划与字符串的编辑距离
- linux 共享内存为分配,Linux在x86_64上共享内存分配
- 毫米和像素怎么换算_自己计算出来的关于像素和厘米单位的换算
- Neural Networks and Deep Learing笔记:一个简单的识别手写数字的神经网络
- java大转盘抽奖概率算法_微信小程序大转盘抽奖概率算法实现
- AutoJs学习-自动添加微信群好友
- VBA-save as xlsm
- 山东大学软件学院项目实训-创新实训-山大软院网络攻防靶场实验平台(十)-Java反序列化漏洞(2)
- 白领的一天 场景7:薪水与福利
- CALayer创建图层(转)