摘要SpringBoot实战电商项目mall(20k+star)地址:https://github.com/macrozheng/mall
Spring Cloud Zuul 是Spring Cloud Netflix 子项目的核心组件之一,可以作为微服务架构中的API网关使用,支持动态路由与过滤功能,本文将对其用法进行详细介绍。
Zuul简介API网关为微服务架构中的服务提供了统一的访问入口,客户端通过API网关访问相关服务。API网关的定义类似于设计模式中的门面模式,它相当于整个微服务架构中的门面,所有客户端的访问都通过它来进行路由及过滤。它实现了请求路由、负载均衡、校验过滤、服务容错、服务聚合等功能。
创建一个zuul-proxy模块在pom.xml中添加相关依赖这里我们创建一个zuul-proxy模块来演示zuul的常用功能。
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
在application.yml中进行配置
server:
port: 8801
spring:
application:
name: zuul-proxy
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8001/eureka/
在启动类上添加@EnableZuulProxy注解来启用Zuul的API网关功能
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulProxyApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulProxyApplication.class, args);
}
}
常用功能
启动相关服务
这里我们通过启动eureka-server,两个user-service,feign-service和zuul-proxy来演示Zuul的常用功能,启动后注册中心显示如下。
配置路由规则- 我们可以通过修改application.yml中的配置来配置路由规则,这里我们将匹配/userService
@Component
public class PreLogFilter extends ZuulFilter {
private Logger LOGGER = LoggerFactory.getLogger(this.getClass());
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
String host = request.getRemoteHost();
String method = request.getMethod();
String uri = request.getRequestURI();
LOGGER.info("Remote host:{},method:{},uri:{}", host, method, uri);
return null;
}
}
过滤器功能演示
添加过滤器后,我们访问http://localhost:8801/user-service/user/1测试下,会打印如下日志。
核心过滤器2019-10-05 15:13:10.232 INFO 11040 --- [nio-8801-exec-7] com.macro.cloud.filter.PreLogFilter : Remote host:0:0:0:0:0:0:0:1,method:GET,uri:/user-service/user/1
禁用过滤器过滤器名称 过滤类型 优先级 过滤器的作用 ServletDetectionFilter pre -3 检测当前请求是通过DispatcherServlet处理运行的还是ZuulServlet运行处理的。 Servlet30WrapperFilter pre -2 对原始的HttpServletRequest进行包装。 FormBodyWrapperFilter pre -1 将Content-Type为application/x-www-form-urlencoded或multipart/form-data的请求包装成FormBodyRequestWrapper对象。 DebugFilter route 1 根据zuul.debug.request的配置来决定是否打印debug日志。 PreDecorationFilter route 5 对当前请求进行预处理以便执行后续操作。 RibbonRoutingFilter route 10 通过Ribbon和Hystrix来向服务实例发起请求,并将请求结果进行返回。 SimpleHostRoutingFilter route 100 只对请求上下文中有routeHost参数的进行处理,直接使用HttpClient向routeHost对应的物理地址进行转发。 SendForwardFilter route 500 只对请求上下文中有forward.to参数的进行处理,进行本地跳转。 SendErrorFilter post 0 当其他过滤器内部发生异常时的会由它来进行处理,产生错误响应。 SendResponseFilter post 1000 利用请求上下文的响应信息来组织请求成功的响应内容。 - 我们可以对过滤器进行禁用的配置,配置格式如下:
zuul: filterClassName: filter: disable: true- 以下是禁用PreLogFilter的示例配置:
Ribbon和Hystrix的支持zuul: PreLogFilter: pre: disable: true由于Zuul自动集成了Ribbon和Hystrix,所以Zuul天生就有负载均衡和服务容错能力,我们可以通过Ribbon和Hystrix的配置来配置Zuul中的相应功能。
- 可以使用Hystrix的配置来设置路由转发时HystrixCommand的执行超时时间:
hystrix: command: #用于控制HystrixCommand的行为 default: execution: isolation: thread: timeoutInMilliseconds: 1000 #配置HystrixCommand执行的超时时间,执行超过该时间会进行服务降级处理- 可以使用Ribbon的配置来设置路由转发时请求连接及处理的超时时间:
常用配置ribbon: #全局配置 ConnectTimeout: 1000 #服务请求连接超时时间(毫秒) ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
使用到的模块zuul: routes: #给服务配置路由 user-service: path: /userService/** feign-service: path: /feignService/** ignored-services: user-service,feign-service #关闭默认路由配置 prefix: /proxy #给网关路由添加前缀 sensitive-headers: cookie,Set-cookie,Authorization #配置过滤敏感的请求头信息,设置为空就不会过滤 add-host-header: true #设置为true重定向是会添加host请求头 retryable: true # 关闭重试机制 PreLogFilter: pre: disable: false #控制是否启用过滤器
项目源码地址springcloud-learning ├── eureka-server -- eureka注册中心 ├── user-service -- 提供User对象CRUD接口的服务 ├── feign-service -- feign服务调用测试服务 └── zuul-proxy -- zuul作为网关的测试服务https://github.com/macrozheng/springcloud-learning
关于作者macrozheng 【id:macrozheng】
专注Java技术分享,mall项目全套学习教程连载中,作者Github项目mall(20k+star)



