Vert.x是一个在JVM上构建响应式应用的工具集(toolkit),在性能上十分出众。本文探讨了如何在Eclipse上运行基于Vert.x框架的应用。
在本例中,Vert.x的版本为3.4.2,JDK版本1.8,Kotlin版本1.1.2。
1、创建Maven工程
点击完成时会发现pom.xml报错,这时右键该工程,选择Java EE tools-->Generate Deployment Descriptor Stub即可消除报错。
2、添加对Vert.x的支持
在maven中引入vert.x的core包
<dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>3.4.2</version> </dependency> </dependencies>
使用Kotlin编写一个简单的Vert.x服务
package com.example.vertx import io.vertx.core.AbstractVerticle class MyServer : AbstractVerticle() { override fun start() { vertx.createHttpServer() .requestHandler() { req -> req.response().putHeader("content-type", "text/plain") .end("My first Vert.x server.") } .listen(8082) } }
开始运行该服务,右键工程,Run As-->Java Application,在弹出提示框中选择io.vertx.core.Launcher左右工程的Main Class。
点击Ok后会发现并没有执行成功,提示信息是
Usage: java io.vertx.core.Launcher [COMMAND] [OPTIONS] [arg...] Commands: bare Creates a bare instance of vert.x. list List vert.x applications run Runs a verticle called <main-verticle> in its own instance of vert.x. start Start a vert.x application in background stop Stop a vert.x application version Displays the version. Run 'java io.vertx.core.Launcher COMMAND --help' for more information on a command.
还需要将入口类配置到运行的参数中。右键工程,Run As-->Run Configuration...,选择Arguments选项卡中添加Program Arguments。
加入的参数 run com.example.vertx.MyServer,即刚编写的Vert.x服务类。
再次运行,显示运行成功。
六月 23, 2017 4:44:54 下午 io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer
信息: Succeeded in deploying verticle
3、验证服务
在浏览器中打开http://localhost:8082/
服务运行正常。
本次介绍了使用Vert.x搭建最简单的WEB服务,更复杂的服务还需要引入vertx-web等jar包。