1、Zuul简介
Zuul作为微服务系统的网关组件,用于构建边界服务(Edge Service),致力于动态路由、过滤、监控、弹性伸缩和安全。其在微服务架构中有着重要的作用,主要体现在以下六个方面:
- Zull、Ribbon以及Eureka相结合可以实现智能路由和负载均衡的功能,Zull可以按照某种策略将请求分发到不同的实例上
- 网关作为边界服务,将内部服务的API接口进行聚合并统一对外暴露接口。保护内部服务的API接口,防止内部服务被外界调用泄露敏感信息;
- 网关可以对用户的身份权限进行认证,防止非法请求API接口;
- 网关可以实现监控功能,实时日志输出,对请求进行记录;
- 网关可以用来实现流量监控,在高流量的情况下,对服务进行降级;
- API接口从内部服务分离出来,便于测试
2、创建一个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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
application.yml,这里配置了两种路由,以ribbon开头的请求都会转发到实例service-ribbon上,以feign开头的请求会转发到实例service-feign上
server:
port: 8005
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
spring:
application:
name: service-zuul
zuul:
routes:
api-a:
path: /ribbon/**
serviceId: service-ribbon
api-b:
path: /feign/**
serviceId: service-feign
启动类上添加@EnableZuulProxy开启zuul服务
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ServiceZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceZuulApplication.class, args);
}
}
3、测试
访问:http://localhost:8005/ribbon/test/hello,可以看到8001和8002在轮询打印,说明路由已经起了作用
Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002
访问:http://localhost:8005/feign/test/hello,同样8001和8002在轮询打印
Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002
4、服务过滤
这里可以过滤用户的请求,并可以对用户的请求做一些校验操作,比如Token校验
/**
* @author XuJD
* @create 2019-11-28 15:15
**/
@Component
public class RouteFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@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();
Object accessToken = request.getParameter("token");
if(accessToken == null) {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
}catch (Exception e){}
return null;
}
return null;
}
filterType:过滤类型,在zuul种提供了4种不同生命周期的过滤类型
pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
filterOrder:过滤顺序
shouldFilter:是否需要过滤
run:过滤器的具体逻辑
这时候再访问:http://localhost:8005/feign/test/hello
token is empty
带上token则可以正常访问
http://localhost:8005/ribbon/test/hello?token=123
Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002