微信公众号:bugstack虫洞栈 | bugstack.cn 沉淀、分享、成长,专注于原创专题案例,以最易学习编程的方式分享知识,让自己和他人都能有所收获。目前已完成的专题有;Netty4.x实战专题案例、用Java实现JVM、基于JavaAgent的全链路监控、手写RPC框架、架构设计专题案例[Ing]等。
在实际的业务开发中不只是将路由配置放到文件中,而是需要进行动态管理并且可以在变化时不用重启系统就可以更新。与此同时还需要在接口访问的时候,可以增加一些权限验证以防止恶意访问。
itstack-demo-springcloud-08 ├── itstack-demo-springcloud-eureka-client │ └── src │ └── main │ ├── java │ │ └── org.itstack.demo │ │ ├── web │ │ │ └── EurekaClientController.java │ │ └── EurekaClientApplication.java │ └── resources │ └── application.yml ├── itstack-demo-springcloud-eureka-server │ └── src │ └── main │ ├── java │ │ └── org.itstack.demo │ │ └── EurekaServerApplication.java │ └── resources │ └── application.yml ├── itstack-demo-springcloud-hystrix-feign │ └── src │ └── main │ ├── java │ │ └── org.itstack.demo │ │ ├── service │ │ │ ├── hystrix │ │ │ │ └── FeignServiceHystrix.java │ │ │ └── FeignService.java │ │ ├── web │ │ │ └── FeignController.java │ │ └── FeignApplication.java │ └── resources │ └── application.yml ├── itstack-demo-springcloud-hystrix-ribbon │ └── src │ └── main │ ├── java │ │ └── org.itstack.demo │ │ ├── service │ │ │ └── RibbonService.java │ │ ├── web │ │ │ └── RibbonController.java │ │ └── RibbonApplication.java │ └── resources │ └── application.yml └── itstack-demo-springcloud-zuul └── src └── main ├── java │ └── org.itstack.demo │ ├── config │ │ └── ZuulConfig.java │ ├── filter │ │ └── TokenFilter.java │ ├── router │ │ └── RouteLocator.java │ ├── service │ │ └── RefreshRouteService.java │ └── ZuulApplication.java └── resources └── application.yml 复制代码
itstack-demo-springcloud-zuul & 动态路由与权限过滤
通过RouteLocator实现自己的动态路由配置,其实就是把配置文件内容转移到这里用代码类实现,并且可以根据需要修改为从数据库里获取。
TokenFilter提供了权限验证功能,当用户访问时候会带上token否则拦截
此外还提供了自动刷新的接口,用于外部调用刷新配置
最后我们需要修改application配置,zuul中还需要排除不做路由的接口[刷新权限接口]
config/ZuulConfig.java & 路由配置类
/** * 路由配置 * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */ @Configuration public class ZuulConfig { @Autowired private ZuulProperties zuulProperties; @Autowired private ServerProperties server; @Bean public RouteLocator routeLocator() { return new RouteLocator(this.server.getServlet().getPath(), this.zuulProperties); } } 复制代码
filter/TokenFilter.java & 权限校验类
/** * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */ public class TokenFilter extends ZuulFilter { /** * 过滤器的类型,它决定过滤器在请求的哪个生命周期中执行。 * FilterConstants.PRE_TYPE:代表会在请求被路由之前执行。 * PRE、ROUTING、POST、ERROR */ public String filterType() { return FilterConstants.PRE_TYPE; } /** * filter执行顺序,通过数字指定。[数字越大,优先级越低] */ public int filterOrder() { return 0; } /** * 判断该过滤器是否需要被执行。这里我们直接返回了true,因此该过滤器对所有请求都会生效。 * 实际运用中我们可以利用该函数来指定过滤器的有效范围。 */ public boolean shouldFilter() { return true; } /* * 具体执行逻辑 */ public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); String token = request.getParameter("token"); if (token == null || token.isEmpty()) { ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); ctx.setResponseBody("refuse! token is empty"); } return null; } } 复制代码
router/RouteLocator.java & 路由类
/** * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */ public class RouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator { private ZuulProperties properties; public RouteLocator(String servletPath, ZuulProperties properties) { super(servletPath, properties); this.properties = properties; } @Override public void refresh() { doRefresh(); } @Override protected Map<String, ZuulRoute> locateRoutes() { LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<String, ZuulRoute>(); //从application.properties中加载路由信息 routesMap.putAll(super.locateRoutes()); //从db中加载路由信息 routesMap.putAll(routesConfigGroup()); //优化一下配置 LinkedHashMap<String, ZuulRoute> values = new LinkedHashMap<>(); for (Map.Entry<String, ZuulRoute> entry : routesMap.entrySet()) { String path = entry.getKey(); // Prepend with slash if not already present. if (!path.startsWith("/")) { path = "/" + path; } if (StringUtils.hasText(this.properties.getPrefix())) { path = this.properties.getPrefix() + path; if (!path.startsWith("/")) { path = "/" + path; } } values.put(path, entry.getValue()); } return values; } /** * 路由配置组,可以从数据库中读取 * 基本配置与写在文件中配置类似,如下; * # routes: * # api-a: * # path: /route-a/** * # serviceId: itstack-demo-springcloud-feign * # api-b: * # path: /route-b/** * # serviceId: itstack-demo-springcloud-ribbon * @return 配置组内容 */ private Map<String, ZuulRoute> routesConfigGroup() { Map<String, ZuulRoute> routes = new LinkedHashMap<>(); ZuulRoute zuulRoute = new ZuulRoute(); zuulRoute.setId("route-a"); zuulRoute.setPath("/route-a/**"); zuulRoute.setServiceId("itstack-demo-springcloud-feign"); // 如果使用了注册中心,那么可以根据serviceId进行访问。 // zuulRoute.setUrl("http://localhost:9001"); zuulRoute.setRetryable(false); zuulRoute.setStripPrefix(true); zuulRoute.setSensitiveHeaders(new HashSet<>()); routes.put(zuulRoute.getPath(), zuulRoute); return routes; } } 复制代码
service/RefreshRouteService.java & 路由刷新服务
/** * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */ @Service public class RefreshRouteService { @Autowired private ApplicationEventPublisher publisher; @Autowired private RouteLocator routeLocator; public void refreshRoute() { RoutesRefreshedEvent routesRefreshedEvent = new RoutesRefreshedEvent(routeLocator); publisher.publishEvent(routesRefreshedEvent); } } 复制代码
ZuulApplication.java & 启动服务注意注解,另外提供了服务接口
/** * 微信公众号:bugstack虫洞栈 | 专注原创技术专题案例 * 论坛:http://bugstack.cn * Create by 付政委 on @2019 */ @SpringBootApplication @EnableZuulProxy @EnableEurekaClient @EnableDiscoveryClient @RestController public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } @Bean public TokenFilter tokenFilter() { return new TokenFilter(); } @Autowired private RefreshRouteService refreshRouteService; @Autowired private ZuulHandlerMapping zuulHandlerMapping; @RequestMapping("api/refresh") public String refresh(){ refreshRouteService.refreshRoute(); return "success"; } @RequestMapping("api/queryRouteInfo") @ResponseBody public Map<String, Object> queryRouteInfo(){ return zuulHandlerMapping.getHandlerMap(); } } 复制代码
application.yml & 配置文件修改,路由过滤
server: port: 10001 spring: application: name: itstack-demo-ddd-zuul eureka: client: serviceUrl: defaultZone: http://localhost:7397/eureka/ # 动态路由,以下配置注释; # http://localhost:10001/route-a/api/queryUserInfo?userId=111 # http://localhost:10001/route-b/api/queryUserInfo?userId=111 zuul: ignoredPatterns: /api/** # routes: # api-a: # path: /route-a/** # serviceId: itstack-demo-springcloud-feign # api-b: # path: /route-b/** # serviceId: itstack-demo-springcloud-ribbon 复制代码
Hi 微信公众号:bugstack虫洞栈 | 111 >: from eureka client port: 8001 From Feign 复制代码