Blade源码深入探索2--server

来源:互联网 发布:java调用rest api实例 编辑:程序博客网 时间:2024/06/03 15:34

接下来看看server服务如何启动,Blade框架中使用了netty作为web服务:

/**     * Start the blade web server     *     * @param bootClass Start the boot class, used to scan the class in all of the packages     * @param address   web server bind ip address     * @param port      web server bind port     * @param args      launch parameters     * @return blade     */    public Blade start(Class<?> bootClass, @NonNull String address, int port, String... args) {        try {            environment.set(ENV_KEY_SERVER_ADDRESS, address);            Assert.greaterThan(port, 0, "server port not is negative number.");            this.bootClass = bootClass;            eventManager.fireEvent(EventType.SERVER_STARTING, this);            Thread thread = new Thread(() -> {                try {                    server.start(Blade.this, args);                    latch.countDown();                    server.join();                } catch (Exception e) {                    startupExceptionHandler.accept(e);                }            });            String threadName = null != this.threadName ? this.threadName : environment.get(ENV_KEY_APP_THREAD_NAME, null);            threadName = null != threadName ? threadName : DEFAULT_THREAD_NAME;            thread.setName(threadName);            thread.start();            started = true;        } catch (Exception e) {            startupExceptionHandler.accept(e);        }        return this;    }


NettyServer

NettyServer的类图:


NettyServer中诸多的属性略过不提,看看几个启动方法:分析参数最多的那个就行了

public void start(Blade blade, String[] args) throws Exception {        this.blade = blade;        this.environment = blade.environment();        this.processors = blade.processors();        long initStart = System.currentTimeMillis();        log.info("Environment: jdk.version    => {}", System.getProperty("java.version"));        log.info("Environment: user.dir       => {}", System.getProperty("user.dir"));        log.info("Environment: java.io.tmpdir => {}", System.getProperty("java.io.tmpdir"));        log.info("Environment: user.timezone  => {}", System.getProperty("user.timezone"));        log.info("Environment: file.encoding  => {}", System.getProperty("file.encoding"));        log.info("Environment: classpath      => {}", CLASSPATH);        this.loadConfig(args);        this.initConfig();        WebContext.init(blade, "/");        this.initIoc();        this.shutdownHook();        this.startServer(initStart);    }
第二个参数args就是 main 方法中的字符串数组参数,当开发者在命令行中启动应用或者设置启动参数的时候,Blade 会读取到一些配置信息,当然如果你不用这个功能也可以不传递该参数。既然如此,我们略过不看。。。

this.loadConfig(args);this.initConfig();
两个方法都是加载一些参数,先跳过吧。。。
接下来的方法是web容器的初始化,即
WebContext.init(blade, "/");
也没什么特别的,可以过了

this.initIoc();
上面即是web服务的核心了,一点点看
    private void initIoc() {        RouteMatcher routeMatcher = blade.routeMatcher();        routeMatcher.initMiddleware(blade.middleware());        routeBuilder = new RouteBuilder(routeMatcher);        blade.scanPackages().stream()                .flatMap(DynamicContext::recursionFindClasses)                .map(ClassInfo::getClazz)                .filter(ReflectKit::isNormalClass)                .forEach(this::parseCls);        routeBuilder.register();        this.processors.stream().sorted(new OrderComparator<>()).forEach(b -> b.preHandle(blade));        Ioc ioc = blade.ioc();        if (BladeKit.isNotEmpty(ioc.getBeans())) {            log.info("⬢ Register bean: {}", ioc.getBeans());        }        List<BeanDefine> beanDefines = ioc.getBeanDefines();        if (BladeKit.isNotEmpty(beanDefines)) {            beanDefines.forEach(b -> BladeKit.injection(ioc, b));        }        this.processors.stream().sorted(new OrderComparator<>()).forEach(b -> b.processor(blade));    }
RouteMatcher是默认路由匹配器,

routeMatcher.initMiddleware(blade.middleware());
初始化一些中间件的配置


原创粉丝点击