# platform-gateway **Repository Path**: matieli/platform-gateway ## Basic Information - **Project Name**: platform-gateway - **Description**: 为避免Eureka的侵入性设计,本项目是基于zuul开发的独立路由器(相当于Nginx),比较适合java程序拓展路由需求,比喻流量监控、接口验签、ip黑名单、登陆校验等等,在业务拓展的时候比较方便,支持域名动态路由,无需重启即使生效! - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 3 - **Created**: 2018-01-31 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # platform-gateway 为避免Eureka的侵入性设计,本项目是基于zuul开发的独立路由器(相当于Nginx),比较适合java程序拓展路由需求,比喻流量监控、接口验签、ip黑名单、登陆校验等等,在业务拓展的时候比较方便,支持域名动态路由,无需重启即使生效! 采取的方式修改路由定位器,借鉴DiscoveryClientRouteLocator去改造SimpleRouteLocator使其具备刷新能力。 DiscoveryClientRouteLocator比SimpleRouteLocator多了两个功能,第一是从DiscoveryClient(如Eureka)发现路由信息,我们不想使用eureka这种侵入式的网关模块,所以忽略它,第二是实现了RefreshableRouteLocator接口,能够实现动态刷新。 在zuul中有这样一个实现了ApplicationListener的监听器ZuulRefreshListener private static class ZuulRefreshListener implements ApplicationListener { @Autowired private ZuulHandlerMapping zuulHandlerMapping; private HeartbeatMonitor heartbeatMonitor = new HeartbeatMonitor(); @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent || event instanceof RefreshScopeRefreshedEvent || event instanceof RoutesRefreshedEvent) { this.zuulHandlerMapping.setDirty(true); } else if (event instanceof HeartbeatEvent) { if (this.heartbeatMonitor.update(((HeartbeatEvent) event).getValue())) { this.zuulHandlerMapping.setDirty(true); } } } } 由此可知在发生ContextRefreshedEvent和RoutesRefreshedEvent事件时会执行this.zuulHandlerMapping.setDirty(true); public void setDirty(boolean dirty) { this.dirty = dirty; if (this.routeLocator instanceof RefreshableRouteLocator) { ((RefreshableRouteLocator) this.routeLocator).refresh(); } } 这样在spring容器启动完成后就刷新了路由规则。因此我们如果要主动刷新路由规则,只需要发布一个RoutesRefreshedEvent事件即可,此方法可以公布成接口,以便通过http命令实现刷新! public void refreshRoute() { RoutesRefreshedEvent routesRefreshedEvent = new RoutesRefreshedEvent(routeLocator); this.publisher.publishEvent(routesRefreshedEvent); logger.info("刷新了路由规则......"); }