在分析一中,我们已经知道了verticle是如何部署的
而route就是实现了AbstractVerticle的一种
route
package cn.ctyun.moho.router.verticle;
import cn.ctyun.moho.router.config.AppConfig;
import cn.ctyun.moho.router.handler.*;
import cn.ctyun.moho.router.model.PluginType;
import cn.ctyun.moho.router.plugin.IPluginHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.Comparator;
import java.util.List;
/**
* 负责路由转发的核心Vertical
* @author Codi
*/
@Slf4j
@Component
@Scope("prototype")
public class RouterVerticle extends AbstractVerticle {
@Autowired
private RouteHandler routeHandler;
@Autowired
private BlockerHandler blockerHandler;
@Autowired
private RequestHandler requestHandler;
@Autowired
private ResponseHandler responseHandler;
@Autowired
private HttpClientHandler httpClientHandler;
@Autowired
private List<IPluginHandler> pluginHandlers;
@Autowired
private ReturnHandler returnHandler;
@Autowired
private GlobalErrorHandler errorHandler;
@Autowired
private AppConfig appConfig;
@Autowired
private StopHandler stopHandler;
//从之前的分析可以知道,vertx在初始化完成后,会运行注册的AbstractVerticle
//中的start方法
@Override
public void start() {
Router router = Router.router(vertx);
//处理请求body
//TODO 对异常body过滤
router.route().handler(BodyHandler.create());
//优雅关闭handler
router.route().handler(stopHandler);
//整理HttpRequest
router.route().handler(requestHandler);
//处理路由拦截
router.route().handler(blockerHandler);
//处理路由匹配
router.route().handler(routeHandler);
//处理前置插件
initPreHandlers(router);
//处理转发 整理response
router.route().handler(httpClientHandler);
//处理后置插件
initPostHandlers(router);
//处理返回 这里调用last方法会改变route中order的值,让后边的handler放到最后
router.route().last().handler(returnHandler);
//集中异常处理
router.route().failureHandler(errorHandler);
//启动http服务 这个才是启动route的关键
startHttpServer(router);
}
private void initPreHandlers(Router router) {
pluginHandlers.stream()
.filter(handler -> handler.getPluginType() == PluginType.PRE)
.sorted(Comparator.comparing(IPluginHandler::priority))
.forEach(handler -> {
router.route().handler(handler);
log.info("Add pre plugin <" + handler.getClass().getSimpleName() + ">");
});
log.info("PreHandlers init complete");
}
private void initPostHandlers(Router router) {
pluginHandlers.stream()
.filter(handler -> handler.getPluginType() == PluginType.POST)
.sorted(Comparator.comparing(IPluginHandler::priority))
.forEach(handler -> {
router.route().handler(handler);
log.info("Add post plugin <" + handler.getClass().getSimpleName() + ">");
});
log.info("PostHandlers init complete");
}
private void startHttpServer(Router router) {
int port= appConfig.getPort();
HttpServerOptions options = new HttpServerOptions();
//增加配置初始长度,规避可能存在的uri过长被服务器拒绝的问题
options.setMaxInitialLineLength(appConfig.getMaxInitialLineLength());
vertx.createHttpServer(options).requestHandler(router).listen(port, http -> {
if (http.succeeded()) {
log.info("Moho-Router HTTP server started on port " + port);
} else {
log.error("Moho-Router HTTP server start error!", http.cause());
}
});
}
}
createHttpServer:
<