vert.x简介
vert.x是一个基于netty的开源应用集,包括有web server, web client,支持http1.1 http2 支持mtls的轻量级应用,不依赖web容器,直接类似像spring-boot一样一个jar包就可以跑起来的应用,配置灵活,功能强大,可自行到官网了解
官方文档:vert.x 文档
构建一个hello world
添加依赖
本人是自己使用idea开发,下面是完整的pom依赖:
<?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>cn.dashagua.web</groupId>
<artifactId>vert-web</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>4.0.0.CR2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
代码实现
可以看到代码非常简单
package cn.dashagua.web.vertx;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import lombok.extern.slf4j.Slf4j;
/**
* @author : dashagua
* @date : 2020/12/3
*/
@Slf4j
public class WebMainApp {
public static void main(String[] args) {
VertxOptions vertxOptions = new VertxOptions().setEventLoopPoolSize(2).setWorkerPoolSize(2);
Vertx vertx = Vertx.vertx(vertxOptions);
HttpServer server = vertx.createHttpServer();
server.requestHandler(
httpServerRequest -> {
HttpServerResponse response = httpServerRequest.response();
response.putHeader("content-type", "text/plain");
response.end("Hello World");
});
server.listen(8080);
log.info("Server listen : 8080");
}
}
直接运行用浏览器访问http://localhost:8080
就可以看到效果了