zuul是被spring集成在spring-cloud项目下的,一般都会配合eureak使用
eureka-client包中,在DiscoveryClient初始化的时候,默认会开启一个定时器,每隔30秒发布一个HeartbeatEvent事件,而ZuulRefreshListener会监听这个事件,然后调用ZuulHandlerMapping.setDirty(true);
所以,RouteLocator的refresh时间不能太长,不然后果很严重
如果refresh结束之前,dirty值已经被改成false,当时确实更新的route不会生效,但是会在下一个HeartbeatEvent事件发布后生效
如果,需要在refresh之后,立刻生效,也可以通过aop+反射来解决这个问题
private Logger logger = LoggerFactory.getLogger(CompositeRouteAop.class);
@Resource
private ZuulHandlerMapping handlerMapping;
@Pointcut("execution(* org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator.refresh(..))")
public void pointCut() {
}
@Around("pointCut()")
public Object resetDirty(ProceedingJoinPoint joinPoint) throws Throwable {
Object proceed = joinPoint.proceed(joinPoint.getArgs());
try {
Field dirty = ZuulHandlerMapping.class.getDeclaredField("dirty");
dirty.setAccessible(true);
dirty.setBoolean(handlerMapping, true);
} catch (Throwable e) {
logger.error("reset zuulHandlerMapping dirty value error after refresh route", e);
}
return proceed;
}