一、概述
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/client-a/转发到到a服务,/client-b/转发到到b服务。zuul默认和Ribbon结合实现了负载均衡的功能。
二、准备
将服务注册与发现这篇博客中的Eureka-Client-A工程,复制一份,名字修改为Service-Zuul。
三、工程修改
Gradle依赖修改为
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
compile('org.springframework.cloud:spring-cloud-starter-zuul:1.4.4.RELEASE')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
在启动类上添加@EnableZuulProxy注解,开启路由功能。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class EurekaZuulApplication
{
public static void main(String[] args) {
SpringApplication.run(EurekaZuulApplication.class, args);
}
}
修改application.properties配置文件,将/client-a/开头的请求路由到eureka-client-a服务器
spring.application.name=service-zuul
server.port=10010
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
zuul.routes.api-a.path: /client-a/**
zuul.routes.api-a.serviceId: eureka-client-a
四、测试
启动之前的Eureka-Server、Eureka-Client-A和当前的Service-Zuul工程,访问http://127.0.0.1:10001/
发现都已经注册到了注册中心,访问http://127.0.0.1:10010/client-a/client,会发现路由到了Eureka-Client-A
五、服务过滤
Zuul不仅有路由的功能,还有服务过滤的功能,做一些拦截验证。
新建MyFilter类
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;
/**
* Created by wzj on 2018/5/24.
*/
@Component
public class MyFilter extends ZuulFilter
{
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
/**
* to classify a filter by type. Standard types in Zuul are "pre" for pre-routing filtering,
* "route" for routing to an origin, "post" for post-routing filters, "error" for error handling.
* We also support a "static" type for static responses see StaticResponseFilter.
* Any filterType made be created or added and run by calling FilterProcessor.runFilters(type)
*
* @return A String representing that type
*/
@Override
public String filterType()
{
return "pre";
}
/**
* filterOrder() must also be defined for a filter. Filters may have the same filterOrder if precedence is not
* important for a filter. filterOrders do not need to be sequential.
*
* @return the int order of a filter
*/
@Override
public int filterOrder()
{
return 0;
}
/**
* a "true" return from this method means that the run() method should be invoked
*
* @return true if the run() method should be invoked. false will not invoke the run() method
*/
@Override
public boolean shouldFilter()
{
return true;
}
/**
* if shouldFilter() is true, this method will be invoked. this method is the core method of a ZuulFilter
*
* @return Some arbitrary artifact may be returned. Current implementation ignores it.
* @throws ZuulException if an error occurs during execution.
*/
@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("token");
if (accessToken == null)
{
log.warn("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try
{
ctx.getResponse().getWriter().write("token is empty");
}
catch (Exception e)
{
}
return null;
}
log.info("ok");
return null;
}
}
filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
- pre:路由之前
- routing:路由之时
- post: 路由之后
- error:发送错误调用
- filterOrder:过滤的顺序
- shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
- run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
服务重启,再次访问http://127.0.0.1:10010/client-a/client,界面显示如下
再次输入http://127.0.0.1:10010/client-a/client?token=1001