vert.x是高性能,高并发,可伸缩,支持多语言的web框架,特别适用于手机客户端的服务端,3.0的资料已经很多了,因为项目大并发的需求,经过调研使用vert.x这个框架。
官方例子
https://github.com/vert-x3/vertx-examples
官方例子文件很大,如果你想立马看到效果,就按我的来吧。
maven,jdk8.0
创建maven程序
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>atest</groupId>
<artifactId>atest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>atest Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<vertx.version>3.0.0</vertx.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
</dependencies>
<build>
<finalName>atest</finalName>
<!-- jdk使用的版本 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
VertxTest.java
package bb;
import java.util.function.Consumer;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
public class VertxTest extends AbstractVerticle {
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.route();
router.get("/products").handler(this::print2);
router.get("/*").handler(this::print1);
// 传递方法引用,监听端口
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
public void print1(RoutingContext routingContext){
routingContext.response().putHeader("content-type", "text/html").end("Hello World");
}
public void print2(RoutingContext routingContext){
routingContext.response().putHeader("content-type", "text/html").end("Hi products");
}
public static void main(String[] args) {
//路径前缀
System.setProperty("vertx.cwd", "E:\\work\\atest\\src\\main\\java\\bb");
Vertx vertx = Vertx.vertx(new VertxOptions().setClustered(false));
vertx.deployVerticle(VertxTest.class.getName());
}
}
好了,只需要两步就创建好了
另外需要输出日志 ,我也是拷贝过来的。
vertx-default-jul-logging.properties
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
java.util.logging.SimpleFormatter.format=%5$s %6$s\n
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.formatter=io.vertx.core.logging.impl.VertxLoggerFormatter
# Put the log in the system temporary directory
java.util.logging.FileHandler.pattern=%t/vertx.log
.level=INFO
io.vertx.ext.web.level=FINEST
io.vertx.level=INFO
com.hazelcast.level=INFO
io.netty.util.internal.PlatformDependent.level=SEVERE