Spring Cloud 之 路由网关 Zuul组件
Netflix又一开源Spring Cloud组件,路由网关Zuul。
微服务架构下服务众多,Zuul能将所有服务的API接口统一聚合,向外暴露,用户看起来就像在访问一个服务一样。隐藏了内部服务跳转的过程。还可以和Ribbon组合实现智能路由转发和负载均衡的功能。Zuul还可以用来做身份认证,权限过滤等等。实现流量监控,日志记录等等功能。
Zuul通过自己的ZuulServlet(类似于Spring MVCde DispatcherServlet)来对请求进行转发控制。
Zuul的4中过滤器:
- PRE:在请求路由到具体服务之前执行,用来做安全验证
- ROUTING:将请求路由到具体服务
- POST:在请求已被路由到具体服务后执行,收集数据,监控信息
- ERROR:在其它过滤器发生错误时执行
当一个Request请求进入Zuul网关服务,先进入PRE,进行验证操作,然后进入ROUTING进行路由转发。转发到具体服务实例进行逻辑处理,处理完后,然后进入POST,做数据统计(性能指标等等),最后将Response返回给客户端。
Zuul官方文档
1、搭建Zuul
在前面几篇博客的基础上新增一个model:server-zuul
*注意主工程依赖
pom.xml
<?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>com.service</groupId>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server-zuul</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.cloud</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
</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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
应用主类添加注解:@EnableZuulProxy(或者@EnableZuulServe),区别参考官方文档18.16.3节
@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:7777/eureka/
server:
port: 8888
spring:
application:
name: service-zuul
zuul:
routes:
#配置对应路径转发到具体服务
api-a:
path: /api-ribbon/**
serviceId: service-ribbon
api-b:
path: /api-feign/**
serviceId: service-feign
实现一个简单的过滤器
MyFilter 验证一个name参数是否存在。
package com.service.zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* Author: RoronoaZoro丶WangRui
* Time: 2018/7/17/003
* Describe:
*/
@Component
public class MyFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
/**
* filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
* pre:路由之前
* routing:路由之时
* post: 路由之后
* error:发送错误调用
* filterOrder:过滤的顺序
* shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
* run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
* 访问测试:http://localhost:8888/api-feign/hi?name=forezp||http://localhost:8888/api-feign/hi?name=forezp&token=22
* @return
* @throws ZuulException
*/
@Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("name");
if (accessToken == null) {
log.warn("name is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("name is empty");
} catch (Exception e) {
}
return null;
}
log.info("ok");
return null;
}
}
2、测试
依次启动eureka-server,eureka-client,service-ribbon,service-feign,server-zuul。其中eureka-client启动两个实例,为了测试负载均衡。
访问:http://localhost:8888/api-feign/hi?name=mistra或者http://localhost:8888/api-ribbon/hi?name=mistra
浏览器交替输出:hi mistra,i am from port:8003和hi mistra,i am from port:8002
当访问不带name参数时:http://localhost:8888/api-ribbon/hi
浏览器输出:name is empty
源码
推荐阅读
-
spring cloud 使用Zuul 实现API网关服务问题
-
Spring Cloud入门教程之Zuul实现API网关与请求过滤
-
详解Spring Cloud Zuul 服务网关
-
spring cloud 使用Zuul 实现API网关服务问题
-
Spring Cloud入门教程之Zuul实现API网关与请求过滤
-
Spring Cloud系列-Zuul网关集成JWT身份验证
-
微服务架构下使用Spring Cloud Zuul作为网关将多个微服务整合到一个Swagger服务上
-
Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】
-
Spring Cloud 之 Config与动态路由.
-
Spring Cloud实战之初级入门(六)— 服务网关zuul