Vert.X

Eclipse Vert.x is a tool-kit for building reactive applications on the JVM.

优点很多,不赘述。

Hello World

创建一个 Java maven 项目。

  • Project Struct
  [plaintext]
1
2
3
4
5
6
7
8
9
10
11
12
13
│pom.xml │ └─src ├─main │ ├─java │ │ └─com │ │ └─ryo │ │ └─vertx │ │ HelloWorldEmbedded.java │ │ │ └─resources └─test └─java
  • pom.xml

主要是引入核心包,指定 JDK 版本。

  [xml]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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>com.ryo</groupId> <artifactId>vertx</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>3.4.2</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
  • HelloWorldEmbedded.java
  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ryo.vertx; import io.vertx.core.Vertx; /** * Created by bbhou on 2017/9/30. */ public class HelloWorldEmbedded { public static void main(String[] args) { // Create an HTTP server which simply returns "Hello World!" to each request. Vertx.vertx().createHttpServer().requestHandler(req -> req.response().end("Hello World!")).listen(8080); } }
  • Run & Visit

直接运行 main() 方法,浏览器访问

  [plaintext]
1
localhost:8080

即可看到对应结果。