Vert.x 与Springboot集成

众所知周,Vert.x是一个异步无阻塞的网络框架,其参照物是node.js。基本上node.js能干的事情,Vert.x都能干。Vert.x利用Netty4的EventLoop来做单线程的事件循环,所以跑在Vert.x上的业务不能做CPU密集型的运算,这样会导致整个线程被阻塞。

Springboot是应用非常广泛的java快速开发框架,它提供了与数据库,websocket,消息系统等等各种集成。
业务中已经使用了springboot,如果又有需要使用vert.x,最好是将这两者集成,而不是Vert.x单独作为一个程序。

如何集成Vert.x与Springboot? 非常简单,我们只需要创建一个Vert.x Server,让它在springboot启动的时候自动启动即刻。

第一步, 设置Vert.x server端口,
创建一个简单的配置类,它从环境变量获取我们Vert.x Server的端口,如果环境变量没有设置,就使用默认的8081.

package com.yq.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * A configuration bean.
 * @author <a href="http://escoffier.me">Clement Escoffier</a>
 */
@Configuration
public class AppConfiguration {

    @Autowired
    Environment environment;

    public int httpPort() {
        return environment.getProperty("http.port", Integer.class, 8081);
    }

}

第二步,创建Vert.x server
我们需要创建一个简单的WebServer, 这里直接使用Vert.x自带的StaticHandler,也可以自己创建新的Handler,这里只是演示,就直接使用Vert.x自带的StaticHandler。

package com.yq.springboot;

import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class StaticServer extends AbstractVerticle {

    @Autowired
    AppConfiguration configuration;

    @Override
    public void start() throws Exception {
        Router router = Router.router(vertx);

        // Serve the static pages
        router.route().handler(StaticHandler.create());

        vertx.createHttpServer().requestHandler(router::accept).listen(configuration.httpPort());
    }
}

第三步,设置静态页面
我们使用了StaticHandler,可以查看源码,发现只需要创建webroot目录,创建index.html页面即刻,


@VertxGen
public interface StaticHandler extends Handler<RoutingContext> {
    String DEFAULT_WEB_ROOT = "webroot";
    boolean DEFAULT_FILES_READ_ONLY = true;
    long DEFAULT_MAX_AGE_SECONDS = 86400L;
    boolean DEFAULT_CACHING_ENABLED = true;
    boolean DEFAULT_DIRECTORY_LISTING = false;
    String DEFAULT_DIRECTORY_TEMPLATE = "vertx-web-directory.html";
    boolean DEFAULT_INCLUDE_HIDDEN = true;
    long DEFAULT_CACHE_ENTRY_TIMEOUT = 30000L;
    String DEFAULT_INDEX_PAGE = "/index.html";

我们在\vertxDemo\src\main\resources\webroot下面创建index.html, 详细路径可参看这里的源代码。
index.html

<html>
<head>
</head>
<body>
<h1>Static web server. Click on some links below</h1>

<br>
<br>
<a href="page1.html">Static Page 1</a>
<a href="page2.html">Static Page 2</a>

</body>
</html>

第四步 在springboot,添加postConstruct
在启动类中加上当我们推出时关闭Vertx staticServer的代码。


package com.yq;

import com.yq.springboot.StaticServer;
import io.vertx.core.Vertx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;


@SpringBootApplication
public class VertxApplication {

    @Autowired
    private StaticServer staticServer;

    public static void main(String[] args) {
        SpringApplication.run(VertxApplication.class, args);
    }

    @PostConstruct
    public void deployVerticle() {
        Vertx.vertx().deployVerticle(staticServer);
    }
}

启动程序后再网页打开http://127.0.0.1:8081
效果图
这里写图片描述

代码放在这里,欢迎加星。

猜你喜欢

转载自blog.csdn.net/russle/article/details/80466215