Vert.x - Hello World


Vert.x 是一个高性能的开发框架,临近春节假期正好了解一下。

官网:https://vertx.io/
官方文档:https://vertx.io/docs/
中文文档:https://vertxchina.github.io/vertx-translation-chinese/ (翻译滞后)

项目搭建

Vert.x提供了类似Spring.start的项目初始化页面:https://start.vertx.io/ 选择合适的版本直接下载即可。

下载后导入IDEA,如果使用的是JDK1.8需要调整一下pom.xml:


  maven-compiler-plugin
  ${maven-compiler-plugin.version}
  
    
    1.8
    1.8
    UTF-8
  

官方下载的demo,并没有提供类,而是通过maven插件运行:

mvn clean compile exec:java

不过在开发环境下,每次都要compile一下,非常不方便,所以不建议开发环境使用这种方式运行。

运行项目

上面提到,官方推荐的运行项目方式,在IDEA开发环境下非常不方便,所以可以自己构建main来启动项目,创建Starter,并部署Verticle:

public class Starter {

  public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(new MainVerticle());
  }
}

运行项目,正常情况下可以看到下面的日志:

HTTP server started on port 8888

说明应用正常运行。

访问应用

官方下载的demo,程序运行之后,默认监听的是8888端口,通过浏览器访问 http://localhost:8888


彩蛋:关注公众号、或小程序,阅读更多IT文章。