vertx源码分析(二)---------------vertx-web router如何部署到vertx中

本文主要分析vertx-web中的route如何部署到vertx中,通过createHttpServer设置requestHandler,将route配置注入,并在listen后,由handleHttp1进行处理。路由器作为Handler,当请求到达时,会按顺序调用handler方法,通过next方法和iterateNext逐个处理上下文。
摘要由CSDN通过智能技术生成

在分析一中,我们已经知道了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:

<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值