Hello World

欢迎来到Vert.x的世界,相信您在接触Vert.x的同时,迫不及待想动手试一试,如您在学习计算机其它知识一样,总是从Hello World开始,下面我们将引导您制作一个最基本简单的Hello World例子,但在此之前,我们需要您具备有以下基础知识:

  1. Java基础知识,您不需要了解Java EE或者是Java ME的知识,但是需要您对Java有所了解,在此文档中,我们不会介绍任何关于Java SE又称Core Java的知识点。请注意:Vert.x 3以上版本需要Java 8以上版本方能运行;

  2. Maven相关知识,您需要知道什么Maven是做什么用的,以及如何使用Maven;

  3. 互联网的基础知识,知道什么是网络协议,尤其是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-pluginmaven-shade-plugin,前者用来将.java的源文件编译成.class的字节码文件,后者可将编译后的.class字节码文件打包成可执行的jar文件,俗称fat-jar

然后我们在src/main/java/io/example目录下新建两个java文件,分别是Main.javaMyFirstVerticle.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.jarvert-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插件的manifestEntriesMain-Verticle对应的值,最后修改main.classio.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的世界。

Vert.x 之 HelloWorld的更多相关文章

  1. vert.x学习(八),用JDBCClient配合c3p0操作数据库

    今天学习了下vert.x的JDBCClient,我这里将今天的学习笔记记录下来.这次学习中使用了c3p0. 用使用JDBCClient和c3p0得现在pom.xml文件里面导入对应的依赖,下面贴出xm ...

  2. vert.x学习(七),使用表单获取用户提交的数据

    在web开发中,用的最多的就是表单了,用户通过表单提交数据到系统后台,系统又可以通过表单传递的数据做业务分析.那么这章就学习在vert.x中怎么使用表单,获取表单的参数值. 编写一个表单模板代码res ...

  3. vert.x学习(五),用StaticHandler来处理静态文件

    做web开发,css.js.图片等静态资源是必不可少的,那么vert.x又是怎么来加载这些静态资源呢.请看StaticHandler 编写HelloStaticResource.java packag ...

  4. vert.x学习(四),使用模板解析器ClassLoaderTemplateResolver

    在vert.x中使用模板解析,可以为我们带来很多方便.我这里学习了一下ClassLoaderTemplateResolver的简单使用.这次工程配置与上篇一样,不需要做任何多的配置.直接编写代码就可以 ...

  5. vert.x学习(三),Web开发之Thymeleaf模板的使用

    在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖.pom.xml文件如下 <?xml version="1.0" e ...

  6. vert.x学习(一),开篇之hello world

    今天决定学习下vert.x这个框架,记录下学习笔记. 下面列下我的开发环境: Java版本 1.8 maven版本 3.3 IDEA版本 2016 在idea中使用vert.x不用下载或安装其他东西了 ...

  7. 小强学渲染之Unity Shader编程HelloWorld

    第一个简单的顶点vert/片元frag着色器   1)打开Unity 5.6编辑器,新建一个场景后ctrl+s保存命名为Scene_5.默认创建的场景是包含了一摄像机,一平行光,且场景背景是一天空盒而 ...

  8. 使用webstorm+webpack构建简单入门级“HelloWorld”的应用&&引用jquery来实现alert

    使用webstorm+webpack构建简单入门级"HelloWorld"的应用&&构建使用jquery来实现 1.首先你自己把webstorm安装完成. 请参考这 ...

  9. Idea下用SBT搭建Spark Helloworld

    没用过IDEA工具,听说跟Eclipse差不多,sbt在Idea其实就等于maven在Eclipse.Spark运行在JVM中,所以要在Idea下运行spark,就先要安装JDK 1.8+ 然后加入S ...

随机推荐

  1. 数据结构之堆栈java版

    import java.lang.reflect.Array; /* 具体原理在c++版已经说的很清楚,这里不再赘述, 就提一点:java的泛型具有边界效应,一旦离开作用域立马被替换为object类型 ...

  2. [译]使用golang每分钟处理百万请求

    [译]使用golang每分钟处理百万请求 在Malwarebytes,我们正在经历惊人的增长,自从我在1年前加入硅谷的这家公司以来,我的主要职责是为多个系统做架构和开发,为这家安全公司的快速发展以及百 ...

  3. 暴风雨中的 online :.net core 版博客站点遭遇的高并发问题进展

    今天暴风雨袭击了杭州,而昨天暴风雨(高并发问题)席卷了园子,留下一片狼藉. 在前天傍晚,我们进行了 .net core 版博客站点的第二次发布尝试,在发布后通过 kestrel 直接监听取代 ngin ...

  4. 洛谷 P4124 [CQOI2016]手机号码

    题意简述 求l~r之间不含前导零,至少有三个相邻的相同数字,不同时含有4和8的11位正整数的个数 题解思路 数位DP,注意在l,r位数不够时补至11位 代码 #include <cstdio&g ...

  5. (三十二)c#Winform自定义控件-表格

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. 消息中间件——RabbitMQ(四)命令行与管控台的基本操作!

    前言 在前面的文章中我们介绍过RabbitMQ的搭建:RabbitMQ的安装过以及各大主流消息中间件的对比:,本章就主要来介绍下我们之前安装的管控台是如何使用以及如何通过命令行进行操作. 1. 命令行 ...

  7. 基于ZooKeeper的三种分布式锁实现

    [欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 今天介绍基于ZooKeeper的分布式锁的简单实现,包括阻塞锁和非阻塞锁.同时增加了网上很少介绍的基于节点的非阻塞锁实现,主 ...

  8. Zabbix遇到的问题集锦

    一.Web界面上显示Zabbix server is not running 二.Zabbix显示中文字体 三.利用Python发送告警注意细节 四.zabbix上发告警信息不发恢复信息 五.Agen ...

  9. python 18 re模块

    目录 re 模块 1. 正则表达式 2. 匹配模式 3. 常用方法 re 模块 1. 正则表达式 \w 匹配字母(包含中文)或数字或下划线 \W 匹配非字母(包含中文)或数字或下划线 \s 匹配任意的 ...

  10. thinkPHP中的简单文章推荐(按浏览量)功能实现

    在公司中接触到了thinkPHP框架,其中要在项目中实现文章推荐功能.记录笔记如下: 一.在Controller中获取从文章列表页进入详情页传入的文章ID值. 二.在Controller中绑定数据库查 ...