Hello World
歡迎來到Vert.x的世界,相信您在接觸Vert.x的同時,迫不及待想動手試一試,如您在學習計算機其它知識一樣,總是從Hello World開始,下面我們將引導您制作一個最基本簡單的Hello World例子,但在此之前,我們需要您具備有以下基礎知識:
-
Java基礎知識,您不需要了解Java EE或者是Java ME的知識,但是需要您對Java有所了解,在此文檔中,我們不會介紹任何關於Java SE又稱Core Java的知識點。請注意:Vert.x 3以上版本需要Java 8以上版本方能運行;
-
Maven相關知識,您需要知道什么Maven是做什么用的,以及如何使用Maven;
-
互聯網的基礎知識,知道什么是網絡協議,尤其是TCP,HTTP協議。
本文將會建立一個基本的HTTP服務器,並監聽8080端口,對於任何發往該服務器以及端口的請求,服務器會返回一個Hello World字符串。
首先新建一個Maven項目,一個基本的Maven項目目錄結構如下所示:
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ └── resources
│ └── test
│ └── java
隨后在pom.xml
中加入相關的依賴和插件,如下所示:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.example</groupId> <artifactId>vertx-example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <vertx.version>3.4.2</vertx.version> <main.class>io.example.Main</main.class> </properties> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>${vertx.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>${main.class}</Main-Class> </manifestEntries> </transformer> </transformers> <artifactSet /> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
跟其它Maven項目一樣,我們首先定義了項目的GroupId,ArtifactId以及版本號,隨后我們定義了兩個屬性,分別是:vertx.version
,也就是Vert.x的版本號,此處我們使用最新的Vert.x版本,也就是3.4.2;以及main.class
,也就是我們要使用的包含有main函數的主類。之后我們引入了兩個Maven插件,分別是maven-compiler-plugin
和maven-shade-plugin
,前者用來將.java的源文件編譯成.class的字節碼文件,后者可將編譯后的.class字節碼文件打包成可執行的jar文件,俗稱fat-jar
。
然后我們在src/main/java/io/example
目錄下新建兩個java文件,分別是Main.java
和MyFirstVerticle.java
,代碼如下:
Main.java
package io.example; import io.vertx.core.Vertx; /** * Created by chengen on 26/04/2017. */ public class Main { public static void main(String[] args){ Vertx vertx = Vertx.vertx(); vertx.deployVerticle(MyFirstVerticle.class.getName()); } }
MyFirstVerticle.java
package io.example; import io.vertx.core.AbstractVerticle; /** * Created by chengen on 26/04/2017. */ public class MyFirstVerticle extends AbstractVerticle { public void start() { vertx.createHttpServer().requestHandler(req -> { req.response() .putHeader("content-type", "text/plain") .end("Hello World!"); }).listen(8080); } }
然后用Maven的mvn package
命令打包,隨后在src的同級目錄下會出現target目錄,進入之后,會出現vert-example-1.0-SNAPSHOT.jar
和vert-example-1.0-SNAPSHOT-prod.jar
兩個jar文件,后者是可執行文件,在有圖形界面的操作系統中,您可雙擊執行,或者用以下命令:java -jar vert-example-1.0-SNAPSHOT-prod.jar
執行。
隨后打開瀏覽器,在瀏覽器的地址欄中輸入:http://localhost:8080/ 便可看到熟悉的Hello World!啦。
啟動器
我們也可以使用Launcher
來替代Main
類,這也是官方推薦的方式,在pom.xml
中加入main.verticle
屬性,並將該屬性值設置為maven-shade-plugin
插件的manifestEntries
的Main-Verticle
對應的值,最后修改main.class
為io.vertx.core.Launcher
,修改后的pom.xml
如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.example</groupId> <artifactId>vertx-example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <vertx.version>3.4.2</vertx.version> <main.class>io.vertx.core.Launcher</main.class> <main.verticle>io.example.MainVerticle</main.verticle> </properties> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>${vertx.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>${main.class}</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer> </transformers> <artifactSet /> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
然后在src/main/java/io/example
目錄下新增MainVerticle.java
文件,代碼如下:
package io.example; import io.vertx.core.AbstractVerticle; /** * Created by chengen on 26/04/2017. */ public class MainVerticle extends AbstractVerticle { public void start() { vertx.deployVerticle(MyFirstVerticle.class.getName()); } }
然后重新打包后執行,便可再次看到Hello World!。
請注意:重新打包之前,您可能需要清除之前編譯后留下的文件,用mvn clean package命令打包。
測試
下面我們將會介紹測試部分,首先引入兩個新的測試依賴,修改后的pom.xml
如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.example</groupId> <artifactId>vertx-example</artifactId> <version>1.0-SNAPSHOT</version> <properties> <vertx.version>3.4.2</vertx.version> <main.class>io.vertx.core.Launcher</main.class> <main.verticle>io.example.MainVerticle</main.verticle> </properties> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>${vertx.version}</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-unit</artifactId> <version>${vertx.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>${main.class}</Main-Class> <Main-Verticle>${main.verticle}</Main-Verticle> </manifestEntries> </transformer> <!--多語言支持在打包時需加入以下轉換器--> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource> </transformer> </transformers> <artifactSet /> <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-prod.jar</outputFile> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
隨后在src/test/java/io/example
目錄下新增MyFirstVerticleTest.java
文件:
package io.example; import io.vertx.core.Vertx; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; /** * Created by chengen on 26/04/2017. */ @RunWith(VertxUnitRunner.class) public class MyFirstVerticleTest { private Vertx vertx; @Before public void setUp(TestContext context) { vertx = Vertx.vertx(); vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess()); } @After public void tearDown(TestContext context) { vertx.close(context.asyncAssertSuccess()); } @Test public void testApplication(TestContext context) { final Async async = context.async(); vertx.createHttpClient().getNow(8080, "localhost", "/", response -> { response.handler(body -> { context.assertTrue(body.toString().contains("Hello")); async.complete(); }); }); } }
package io.example; import io.vertx.core.Vertx; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; /** * Created by chengen on 26/04/2017. */ @RunWith(VertxUnitRunner.class) public class MyFirstVerticleTest { private Vertx vertx; @Before public void setUp(TestContext context) { vertx = Vertx.vertx(); vertx.deployVerticle(MyFirstVerticle.class.getName(), context.asyncAssertSuccess()); } @After public void tearDown(TestContext context) { vertx.close(context.asyncAssertSuccess()); } @Test public void testApplication(TestContext context) { final Async async = context.async(); vertx.createHttpClient().getNow(8080, "localhost", "/", response -> { response.handler(body -> { context.assertTrue(body.toString().contains("Hello")); async.complete(); }); }); } }
執行該測試案例便可得到期望的結果,理解測試代碼並不難,留給讀者作為練習。
至此,大功告成,歡迎來到Vert.x的世界。