本文目录一览:
springcloud执行流程
1.Servlet
zuul.servletPath默认配置为/zuul,故请求为/zuul开头的会跳过dispatcherServlet直接进入ZuulServlet,该配置可以自定义配置,例如用于大文件上传
2.ZuulServlet中service方法
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
try {
this.init((HttpServletRequest)servletRequest, (HttpServletResponse)servletResponse);
RequestContext context = RequestContext.getCurrentContext();
context.setZuulEngineRan();
try {
//运行pre过滤器
this.preRoute();
} catch (ZuulException var12) {
//有异常,执行errorFilter
this.error(var12);
//再执行postFilter
this.postRoute();
return;
}
try {
//运行rote过滤器
this.route();
} catch (ZuulException var13) {
//有异常,执行errorFilter
this.error(var13);
//再执行postFilter
this.postRoute();
return;
}
try {
//运行post过滤器
this.postRoute();
} catch (ZuulException var11) {
//有异常,执行errorFilter
this.error(var11);
}
} catch (Throwable var14) {
this.error(new ZuulException(var14, 500, "UNHANDLED_EXCEPTION_" + var14.getClass().getName()));
} finally {
RequestContext.getCurrentContext().unset();
}
}
3.FilterProcessor
其运行交由FilterProcessor中的方法runFilters,根据service中的顺序,取不同的filter类型,执行其中的run方法
public Object runFilters(String sType) throws Throwable {
if (RequestContext.getCurrentContext().debugRouting()) {
Debug.addRoutingDebug("Invoking {" + sType + "} type filters");
}
boolean bResult = false;
ListZuulFilter list = FilterLoader.getInstance().getFiltersByType(sType);
if (list != null) {
for(int i = 0; i list.size(); ++i) {
ZuulFilter zuulFilter = (ZuulFilter)list.get(i);
Object result = this.processZuulFilter(zuulFilter);//见下面zuulFilter的runFilter()
if (result != null result instanceof Boolean) {
bResult |= ((Boolean)result).booleanValue();
}
}
}
return bResult;
}
zuulFilter的runFilter方法,当filter的shouldFilter()返回true时才执行run()方法
public ZuulFilterResult runFilter() {
ZuulFilterResult zr = new ZuulFilterResult();
if (!this.isFilterDisabled()) {
if (this.shouldFilter()) {
Tracer t = TracerFactory.instance().startMicroTracer("ZUUL::" + this.getClass().getSimpleName());
try {
Object res = this.run();
zr = new ZuulFilterResult(res, ExecutionStatus.SUCCESS);
} catch (Throwable var7) {
t.setName("ZUUL::" + this.getClass().getSimpleName() + " failed");
zr = new ZuulFilterResult(ExecutionStatus.FAILED);
zr.setException(var7);
} finally {
t.stopAndLog();
}
} else {
zr = new ZuulFilterResult(ExecutionStatus.SKIPPED);
}
}
return zr;
}
4.获取过滤器FilterRegistry
其中的属性private final ConcurrentHashMapString, ZuulFilter filters = new ConcurrentHashMap();
保存所有的过滤器
例子中有12个(其中有两个为自定义的):
[org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter@3dc68586,
org.springframework.cloud.netflix.zuul.filters.pre.Servlet30WrapperFilter@4001d8c1,
org.springframework.cl
zuulfilter 怎么使用环境变量
路由是微服务架构的不可或缺的一部分。例如:”/” 可能映射到你应用主页,/api/users映射到用户服务,/api/shop映射到购物服务。Zuul。Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。
Netflix uses Zuul for the following:
* Authentication
* Insights
* Stress Testing
* Canary Testing
* Dynamic Routing
* Service Migration
* Load Shedding
* Security
* Static Response handling
* Active/Active traffic management
Zuul的规则和过滤器允许使用各种基于JVM的语言,支持基于Java和Groovy。
注意:zuul.max.host.connections已经被两个新的属性替代:zuul.host.maxTotalConnections 和 zuul.host.maxPerRouteConnections,默认分别为200和20.
注意:默认所有routes的Hystrix隔离模式(ExecutionIsolationStrategy)是SEMAPHORE zuul.ribbonIsolationStrategy可以改为THREAD,如果这个隔离模式更好。
Zuul配置项中sensitiveHeaders和ignoredHeaders的不同
路由是微服务架构的不可或缺的一部分。例如:”/” 可能映射到你应用主页,/api/users映射到用户服务,/api/shop映射到购物服务。Zuul。Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器。
Netflix uses Zuul for the following: * Authentication * Insights * Stress Testing * Canary Testing * Dynamic Routing * Service Migration * Load Shedding * Security * Static Response handling * Active/Active traffic management
Zuul的规则和过滤器允许使用各种基于JVM的语言,支持基于Java和Groovy。
注意:zuul.max.host.connections已经被两个新的属性替代:zuul.host.maxTotalConnections 和 zuul.host.maxPerRouteConnections,默认分别为200和20.
注意:默认所有routes的Hystrix隔离模式(ExecutionIsolationStrategy)是SEMAPHORE zuul.ribbonIsolationStrategy可以改为THREAD,如果这个隔离模式更好。
org.springframework.cloud and artifact id spring-cloud-starter-zuul。See the Spring Cloud Project page for details。
当一个UI应用想要代理调用一个或者多个后台服务的时候,Sping cloud创建了一个嵌入的Zuul proxy很方便的开发一个简单的案例。这个功能对于代理前端需要访问的后端服务非常有用,避免了所有后端服务需要关心管理CORS和认证的问题.
在Spring Boot主函数上通过注解 @EnableZuulProxy 来开启, 这样可以让本地的请求转发到适当的服务. 按照约定, 一个ID为”users”的服务会收到 /users 请求路径的代理请求(前缀会被剥离). Zuul使用Ribbon定位服务注册中的实例, 并且所有的请求都在hystrix的command中执行, 所以失败信息将会展现在Hystrix metrics中, 并且一旦断路器打开, 代理请求将不会尝试去链接服务.
注意:Zuul starter没有包含服务发现的客户端, 所以对于路由你需要在classpath中提供一个根据service IDs做服务发现的服务.(例如, eureka是一个不错的选择)