Vert.X使用笔记(一)
一、开发环境
- 框架 VERTX_VER = 3.9.4
- 日志 LOGBACK_VER = 1.2.3
- 语言 GROOVY_VER = 2.4.12
- 数据库连接 HIKARICP_VER = 2.7.2
- 数据库驱动 MSSQLJDBC_VER = 6.2.2.jre8
- 数据库驱动 POSTGRESQL_VERS=42.2.6
- 数据库驱动 HBASE_VER = 1.3.1
- 数据解析 JSON_VERSION=20160810
二、官方文档
官方文档
建议:官方文档形象但不步骤不详细,可以看看这个文档database
三、vert.x概述
是一个基于NETTY的事件驱动框架
包含许多模块
- vertx-core
- vertx-web
- vertx-ignite
- vertx-tcp-eventbus-bridge
- vertx-jdbc-client
1、vertx-core
实质:封装了一系列java的API,包含:
- java tcp server、client;
- java HTTP server 、client;
- java Websocket;
- java eventbus;
- java file system
- 等等
四、Hello World
1、生成工程
官方的工程生成器
注意: 1、选择java版本,否则默认java11
2、打开工程
出现maven pom文件报错
解决办法:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>//添加此行
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
3、编写启动类
新建一个类 psvm生成主函数
写入以下语句
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new MainVerticle());
4、启动项目
浏览器打开http://127.0.0.1:8888/
5、另一种启动方法
无需添加主函数,直接使用依赖中的主函数
1、观察pom文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-shade-plugin.version>3.2.4</maven-shade-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>
<vertx.version>4.3.3</vertx.version>
<junit-jupiter.version>5.7.0</junit-jupiter.version>
<main.verticle>helloworld.MainVerticle</main.verticle>//这是主vertx程序
<launcher.class>io.vertx.core.Launcher</launcher.class>//这是vertx启动程序
</properties>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec-maven-plugin.version}</version>
<configuration>
<mainClass>io.vertx.core.Launcher</mainClass>
<arguments>
<argument>run</argument> //启动命令是run
<argument>${main.verticle}</argument>//查看上面prop
</arguments>
</configuration>
</plugin>
如果是gradle工程
shadowJar {
baseName = 'mes-dataaccess'
classifier = null
version = null
manifest {
attributes 'Main-Class': 'Launcher'//启动类
attributes 'Main-Verticle': 'MainVerticle'//主处理程序
}
mergeServiceFiles {
include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
}
}
可以在IDEA中配置启动选项以启动
然后使用IDEA 启动
四、理解示例程序
package helloworld;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
public class MainVerticle extends AbstractVerticle {
//vertx对象启动时调用的方法,startPromise是监听对象
@Override
public void start(Promise<Void> startPromise) throws Exception {
//启动时创建一个http serve 监听8888端口
vertx.createHttpServer().requestHandler(req -> {
req.response()
.putHeader("content-type", "text/plain")
.end("Hello from Vert.x!");
}).listen(8888, http -> {
//体现了vertx的事件驱动,
if (http.succeeded()) {
//将startPromise状态置为完成
startPromise.complete();
System.out.println("HTTP server started on port 8888");
} else {
startPromise.fail(http.cause());
}
});
}
}
五、概念
1、事件驱动
当一些事件发生,vertx会主动发送事件呼叫,例如计时结束、有http请求等,当事件发生,vertx会异步调用
2、非阻塞
vertx本身不会阻塞线程,除非用户设置了阻塞方法,传统的等待使得线程利用率低,并发情况需要多个线程等待,内存消耗大;
3、链式循环处理
vertx实际基于netty netty就有循环处理的概念,vertx将其复用重写,一个事件会经过多到处理程序,每个vertx实例都维护多个事件循环
4、未来(future)
这是vertx4的特性,与netty一致,vertx3使用的回调
5、verticle
相当于一个处理机,一般来说我们直接集成抽象类AbstractVerticle ,重写start、stop方法
start方法在 一个处理机生成时就调用,该方法完成时,Verticle 将被视为已启动
stop方法在 一个处理取消部署时调用,并且当方法完成时,Verticle 将被视为已停止
6、event bus
待续
六、使用案例
1、定时执行
vertx.setPeriodic(1000, -> {...});
定时一秒执行后续程序