SpringCloud系列之API网关(Gateway)服务Zuul
1、什么是API网关
API网关是所有请求的入口,承载了所有的流量,API Gateway是一个门户一样,也可以说是进入系统的唯一节点。这跟面向对象设计模式中的Facet模式很像。API Gateway封装内部系统的架构,并且提供API给各个客户端。它还可能有其他功能,如授权、监控、负载均衡、缓存、请求分片和管理、静态响应处理等
API Gateway负责请求转发、合成和协议转换。所有来自客户端的请求都要先经过API Gateway,然后路由这些请求到对应的微服务。API Gateway将经常通过调用多个微服务来处理一个请求以及聚合多个服务的结果。它可以在web协议与内部使用的非Web友好型协议间进行转换,如 HTTP协议、WebSocket协议。
画图表示,没有网关的情况,客户端的请求会直接落到后端的各个服务中,无法集中统一管理。
画图表示,有网关的情况,所有的请求都先经过网关,然后进行分发到对应服务
2、API网关的重要性
API网关在微服务项目中是很重要的,网关提供一个统一的管理,服务间的调度变得有序
引用nginx官方的一篇优质博客,https://www.nginx.com/blog/building-microservices-using-an-api-gateway/,例子介绍了一个庞杂的电商系统,按照微服务理论进行设计,有如下各种服务:
- 购物车服务:购物车中的物品数量
- 订单服务:订单历史记录
- 目录服务:基本产品信息,例如其名称,图像和价格
- 审核服务:客户审核
- 库存服务:库存不足警告
- 运送服务:运送选项,期限和费用与运送提供商的API分开提取
- 推荐服务:建议项目
在不使用网关的情况,客户端直接调用各服务:
理想情况,各服务调用是可以正常使用的,但是随着业务拓展,服务之间的调用越来越复杂,到时候系统就会变成如图:
如果没有一个统一的管理,肯定是不合理的,所以可以引入网关,作为一个统一的门户,如图:
3、API Gateway的作用
ok,简单介绍网关之后,要说说网关的作用,在Spring cloud官网也有过归纳:
当然,我们可以自己挑几个重要的介绍
- 动态路由
网关可以做路由转发,假如服务信息变了,只要改网关配置既可,所以说网关有动态路由(Dynamic Routing)的作用,如图:
- 请求监控
请求监控可以对整个系统的请求进行监控,详细地记录请求响应日志,如图,可以将日志丢到消息队列,如果没有使用网关的话,记录请求信息需要在各个服务中去做
- 认证鉴权
认证鉴权可以对每一个访问请求做认证,拒绝非法请求,保护后端的服务,不需要每个服务都做鉴权,在项目中经常有加上OAuth2.0、JWT,Spring Security进行权限校验
- 压力测试
有网关的系统,如果要要对某个服务进行压力测试,可以如图所示,改下网关配置既可,测试请求路由到测试服务,测试服务会有单独的测试数据库,这样测试的请求就不会影响到正式的服务和数据库
4、什么是Netflix Zuul?
Netflix Zuul是Netflix公司的产品,是一款API网关中间件。Zuul是一个基于 JVM 路由和服务端的负载均衡器。提供了路由、监控、弹性、安全等服务。Zuul 能够与 Eureka、Ribbon、Hystrix 等组件配合使用,提供统一的API网关处理
5、Netflix Zuul工作原理
参考Zuul官网wiki,Zuul的核心如图其实就是过滤器,zuul基于Servlet实现。当一个请求进来时,会先进入 pre 过滤器,在 pre 过滤器执行完后,接着就到了 routing 过滤器中,开始路由到具体的服务中,错误的情况会被错误过滤器拦截
- 过滤器类型:
- 前置过滤器(PRE FILTER):在路由过滤器之前执行。功能可以包括请求身份验证,选择原始服务器以及记录调试信息。
- 路由过滤器(ROUTE FILTER):处理将请求路由到源的过程。这是使用Apache HttpClient或Netflix Ribbon构建和发送原始HTTP请求的地方。
- 后置过滤器(POST FILTER):在将请求路由过滤器之后执行。功能可以包括向响应中添加标准HTTP标头,收集统计信息和指标以及将响应从源流传输到客户端。
- 错误过滤器(ERR FILTER):在其他阶段之一发生错误时就会调用到错误过滤器
6、Zuul实验环境准备
环境准备:
- JDK 1.8
- SpringBoot2.2.3
- SpringCloud(Hoxton.SR6)
- Maven 3.2+
- 开发工具
- IntelliJ IDEA
- smartGit
创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程
maven配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
本博客的是基于spring-cloud-starter-netflix-eureka-client
进行试验,试验前要运行eureka服务端,eureka服务提供者,代码请参考上一章博客
项目创建成功后,先在启动类加上@EnableZuulProxy
:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class SpringcloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZuulApplication.class, args);
}
}
7、eureka、zuul配置
eureka客户端配置:
server:
port: 8082
# 指定application name,这个是微服务注册的serviceId
spring:
application:
name: zuul-api-gateway
eureka:
client:
# 服务注册中心url
service-url:
defaultZone: http://localhost:8761/eureka/
# 网关服务注册、发现都开放,所以 register-with-eureka、fetch-registry都是true
register-with-eureka: true
fetch-registry: true
instance:
status-page-url-path: http://localhost:8761/actuator/info
health-check-url-path: http://localhost:8761/actuator/health
prefer-ip-address: true
instance-id: zuul-api-gateway8082
Zuul 配置路由规则:
zuul:
routes:
provider: # 路由标识,可以自己定义
service-id: eureka-service-provider # 服务id(必须配置)
path: /provider/** # 映射的路径,一般和routes.provider一致
url: http://localhost:8083 # 路由到的url,可以不配置
Zuul配置访问前缀:访问时候需要加上前缀,eg:http://localhost:8082/api-gateway/provider/api/users/mojombo
zuul:
# 配置前缀
prefix: /api-gateway
Zuul配置Header过滤:
zuul:
# 配置过滤敏感的请求头信息,设置为空就不会过滤
sensitive-headers: Cookie,Set-Cookie,Authorization
Zuul配置重定向添加Host:
zuul:
# 重定向会添加host请求头
add-proxy-headers: true
Zuul超时设置:
zuul:
host:
# 配置连接超时时间
connect-timeout-millis: 15000
# socker发送超时时间
socket-timeout-millis: 60000
zuul所有配置参考,详情参考官网:
zuul:
# 配置前缀
prefix: /api-gateway
routes:
provider: # 路由标识,可以自己定义
service-id: eureka-service-provider # 服务id
path: /provider/** # 映射的路径,一般和routes.provider一致
url: http://localhost:8083 # 路由到的url
host:
# 配置连接超时时间
connect-timeout-millis: 15000
# socker发送超时时间
socket-timeout-millis: 60000
# 请求url编码
decode-url: true
# 查询字符串编码
force-original-query-string-encoding: false
# 配置过滤敏感的请求头信息,设置为空就不会过滤
sensitive-headers: Cookie,Set-Cookie,Authorization
# 重定向会添加host请求头
add-proxy-headers: true
访问:http://localhost:8082/api-gateway/provider/api/users/mojombo,要加上前缀,配置的path
可能遇到的错误,504错误:
经过排查,需要加上超时设置,因为调用服务超时,导致504错误
zuul:
host:
connect-timeout-millis: 15000
socket-timeout-millis: 60000
加上配置,调用服务成功
8、Zuul自定义过滤器
在前面的介绍中,已经介绍了几种过滤器,现在自定义实现这四种过滤器
ps:spring cloud官网也提供了zuul过滤器的例子,详情可以去github查看:https://github.com/spring-cloud-samples/sample-zuul-filters
项目结构:
过滤器类型参考org.springframework.cloud.netflix.zuul.filters.supportFilterConstants.java:
实现一个前置过滤器:拦截请求,必须带token过来,不然抛出提示信息等等
package com.example.springcloud.zuul.web.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORWARD_TO_KEY;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY;
/**
* <pre>
* API网关预过滤器
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/08/05 18:08 修改内容:
* </pre>
*/
@Slf4j
//@Component
public class ZuulApiGatewayPreFilter extends ZuulFilter {
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String accessToken = request.getParameter("token");
if (StringUtils.isEmpty(accessToken)) {
// zuul过滤该请求,不进行路由
ctx.setSendZuulResponse(false);
// 设置返回的错误码
ctx.setResponseStatusCode(403);
ctx.setResponseBody("AccessToken is Invalid ");
return null;
}
log.info("accessToken: {}",accessToken);
// 否则业务继续执行
return null;
}
}
后置过滤器,经常被用于打印日志等等操作,代码参考:https://www.baeldung.com/zuul-filter-modifying-response-body,实现效果时,路由过滤器执行之后,执行后置过滤器打印日志:
package com.example.springcloud.zuul.web.filter;
import com.google.common.io.CharStreams;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.protocol.RequestContent;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
/**
* <pre>
* API Gateway后置过滤器
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/08/06 10:05 修改内容:
* </pre>
*/
@Slf4j
//@Component
public class ZuulApiGatewayPostFilter extends ZuulFilter {
@Override
public String filterType() {
return POST_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
try (final InputStream responseDataStream = context.getResponseDataStream()) {
if(responseDataStream == null) {
log.warn("RESPONSE BODY: {}", "");
return null;
}
String responseData = CharStreams.toString(new InputStreamReader(responseDataStream, "UTF-8"));
log.info("RESPONSE BODY: {}", responseData);
context.setResponseBody(responseData);
}
catch (Exception e) {
throw new ZuulException(e, INTERNAL_SERVER_ERROR.value(), e.getMessage());
}
return null;
}
}
注册过滤器,将过滤器加载到Spring容器,也可以在过滤器类加上@Component
package com.example.springcloud.zuul;
import com.example.springcloud.zuul.web.filter.ZuulApiGatewayErrFilter;
import com.example.springcloud.zuul.web.filter.ZuulApiGatewayPostFilter;
import com.example.springcloud.zuul.web.filter.ZuulApiGatewayPreFilter;
import com.example.springcloud.zuul.web.filter.ZuulApiGatewayRouteFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableZuulProxy
public class SpringcloudZuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudZuulApplication.class, args);
}
@Bean
public ZuulApiGatewayPreFilter zuulApiGatewayPreFilter(){
return new ZuulApiGatewayPreFilter();
}
@Bean
public ZuulApiGatewayPostFilter zuulApiGatewayPostFilter(){
return new ZuulApiGatewayPostFilter();
}
@Bean
public ZuulApiGatewayRouteFilter zuulApiGatewayRouteFilter(){
return new ZuulApiGatewayRouteFilter();
}
@Bean
public ZuulApiGatewayErrFilter zuulApiGatewayErrFilter(){
return new ZuulApiGatewayErrFilter();
}
}
访问网关:http://localhost:8082/api-gateway/provider/api/users/mojombo,不带token的情况
http://localhost:8082/api-gateway/provider/api/users/mojombo?token=?,带上token调用成功
9、查看Zuul路由信息
加上spring-boot-starter-actuator,进行路由信息监控:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring-boot-starter-actuator配置:
management:
endpoints:
web:
exposure:
# 默认只支持info,health,开启对routes的监控
include: info,health,routes
# 开启健康检查详细信息
endpoint:
health:
show-details: always
查看路由详细,访问http://localhost:8082/actuator/routes,SpringBoot2.2.3版本要加上actuator前缀,调用成功,返回json数据:
{
"/api-gateway/provider/**":"eureka-service-provider",
"/api-gateway/eureka-service-provider/**":"eureka-service-provider"
}
查看路由详细信息,访问链接:http://localhost:8082/actuator/routes/details
{
"/api-gateway/provider/**":{
"id":"provider",
"fullPath":"/api-gateway/provider/**",
"location":"eureka-service-provider",
"path":"/**",
"prefix":"/api-gateway/provider",
"retryable":false,
"customSensitiveHeaders":false,
"prefixStripped":true
},
"/api-gateway/eureka-service-provider/**":{
"id":"eureka-service-provider",
"fullPath":"/api-gateway/eureka-service-provider/**",
"location":"eureka-service-provider",
"path":"/**",
"prefix":"/api-gateway/eureka-service-provider",
"retryable":false,
"customSensitiveHeaders":false,
"prefixStripped":true
}
}
本博客代码例子下载:code download
Zuul官网手册:
spring Cloud官网zuul资料:https://docs.spring.io/spring-cloud-netflix/docs/2.2.x-SNAPSHOT/reference/html/#router-and-filter-zuul
zuul github wiki:https://github.com/Netflix/zuul/wiki/How-it-Works
github zuul过滤器例子:https://github.com/spring-cloud-samples/sample-zuul-filters
优质学习资料参考:
Nginx官网对微服务网关的介绍:https://www.nginx.com/blog/building-microservices-using-an-api-gateway/
SpringCloud组件之网关Zuul(Hoxton版本):https://juejin.im/post/6847902220214763527
方志鹏大佬系列Spring Cloud博客:https://www.fangzhipeng.com/spring-cloud.html
使用Spring Cloud与Docker实战微服务:https://eacdy.gitbooks.io/spring-cloud-book/content/
程序员DD大佬系列Spring Cloud博客:http://blog.didispace.com/spring-cloud-learning/
SpringCloud系列之API网关(Gateway)服务Zuul的更多相关文章
- SpringCloud微服务项目实战 - API网关Gateway详解实现
前面讲过zuul的网关实现,那为什么今天又要讲Spring Cloud Gateway呢?原因很简单.就是Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用 ...
- springcloud中的API网关服务Zuul
到目前为止,我们Spring Cloud中的内容已经介绍了很多了,Ribbon.Hystrix.Feign这些知识点大家都耳熟能详了,我们在前文也提到过微服务就是把一个大的项目拆分成很多小的独立模块, ...
- Spring Cloud 微服务二:API网关spring cloud zuul
前言:本章将继续上一章Spring Cloud微服务,本章主要内容是API 网关,相关代码将延续上一章,如需了解请参考:Spring Cloud 微服务一:Consul注册中心 Spring clou ...
- SpringCloud之API网关与服务发现——Cloud核心组件实战入门及原理
微服务发展历史 单体模式——>服务治理(服务拆分)——>微服务(细分服务)——>Segments(服务网格) 微服务 VS SOA 微服务:模块化.独立部署.异构化 SOA:共同的治 ...
- SpringCloud系列三:将微服务注册到Eureka Server上
1. 回顾 通过上篇博客的讲解,我们知道硬编码提供者地址的方式有不少问题.要想解决这些问题,服务消费者需要一个强大的服务发现机制,服务消费者使用这种机制获取服务提供者的网络信息.不仅如此,即使服务提供 ...
- springcloud系列五 feign远程调用服务
一:Feign简介 Feign 是一种声明式.模板化的 HTTP 客户端,在 Spring Cloud 中使用 Feign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完 ...
- 再谈API GateWay服务网关
前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...
- 微服务系列(二):使用 API 网关构建微服务
编者的话|本文来自 Nginx 官方博客,是微服务系列文章的第二篇,本文将探讨:微服务架构是如何影响客户端到服务端的通信,并提出一种使用 API 网关的方法. 作者介绍:Chris Richardso ...
- API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd API 网关出现的原因
API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd http://www.infoq.com/cn/articles/compa ...
随机推荐
- 利用vue-i18n实现多语言切换
在vue项目中利用vue-i18n,我们可以实现多语言的切换,可以轻松搞定大部分的需求,包括中英文切换,以及词条的变更. vue-i18n基本的使用方法 一.安装vue-i18n npm instal ...
- 关于jquery.unobtrusive-ajax.js 回调函数无效 的解决办法
今天新项目的时候写MVC的时候使用到了Ajax.BeginForm,发现它的回调函数怎么都不响应,最后在网上查找了相关资料跟自己写的一些代码测试, 总算找到了原因:jquery.unobtrusive ...
- 当输入一个 URL,实际会发生什么?
从一个经典的面试题说起 从输入URL到页面展现的过程: 输入URL后,会先进行域名解析.优先查找本地host文件有无对应的IP地址,没有的话去本地DNS服务器查找,还不行的话,本地DNS服务器会去找根 ...
- 基于python的自动化测试简介【十年从业大佬】
一.自动化测试包括以下几个方面: 1. 常用测试工具: (1)QTP:主要用于回归测试和测试同一软件的新版本 (2)Robot Framwork:python编写的功能自动化测试框架,具有良好的可扩展 ...
- 题解 洛谷 P2287 [USACO07NOV]Sunscreen G
原题 传送门 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值(minSPFi and maxSPFi),太大就晒伤了,太小奶牛没 ...
- 拿下Netty这座城,从现在开始!
你好,我是彤哥,技术公号主"彤哥读源码"的运营者. 其实,我刚学习Netty的时候,也是很迷茫的,直到有一天,一个同事收到了阿里的offer,他要去阿里做中台了,临走前他偷偷地告诉 ...
- Spring bean初始化
- O、Θ、Ω、o、ω,别再傻傻分不清了!
前言 本篇文章收录于专辑:http://dwz.win/HjK,点击解锁更多数据结构与算法的知识. 你好,我是彤哥,一个每天爬二十六层楼还不忘读源码的硬核男人. 前面几节,我们一起学习了算法的复杂度如 ...
- 前端 /deep/ 深入样式(很深入的那种哦) 简单收藏
简单介绍:使用vue脚手架和elemen-ui开发的前端项目 遇到这样的场景: 对div下的el-select下拉组件 设置样式,直接在标签上用style属性是完全可以的,但我们的开发规范是前端样式 ...
- 关于简单的数据双向绑定原理,defineProperty 和Proxy演示
双向绑定,也就是说js中的数据传到页面,页面中的内容到js,实现同步更新,简单的演示可以直接复制下放HTML代码运行. 在这个例子中,我们使用defineProperty ,Object.define ...