1、实例一、 软件IDEA gradle
compile 'io.vertx:vertx-core:3.5.0'
compile 'io.vertx:vertx-web:3.5.0'
2、实例二、 maven
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>3.5.0</version> </dependency>
<!--vertx的web路由方式 web组件开发像是于springMVC-->
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> <version>3.5.0</version> </dependency>
(1)编写http服务器
package com.fei.vertxtest; import io.vertx.core.Vertx; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerRequest; import io.vertx.ext.web.Router; import org.junit.Test; /** * Created by Thinkpad on 2018/3/3. */ public class VertxTest { @Test //vertx响应式实现http服务器 public void vertxDemo01(){ //vertx核心 Vertx vertx=Vertx.vertx(); //创建http的Server HttpServer server= vertx.createHttpServer(); //服务器响应 server.requestHandler(request->{ request.response().end("hello world"); }); //监听端口 server.listen(8080); } @Test public void vertxWeb(){ //创建vertx对象 Vertx vertx= Vertx.vertx(); //web路由 Router router=Router.router(vertx); router.route("/").handler(context->{ //context.request(); context.response().end("hello root"); }); router.route("/abc").handler(context->{ HttpServerRequest request=context.request(); String name=request.getParam("name"); String handler=request.getHeader("fei"); //context.request(); context.response().end("hello abc"+name); }); //创建httpserver的server服务器 HttpServer server=vertx.createHttpServer(); server.requestHandler(request->{ //处理request请求 router.accept(request); }); //监听端口8082 server.listen(8082); } }
2、Resultful 风格
package com.fei.vertxtest; import io.vertx.core.Vertx; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerRequest; import io.vertx.ext.web.Router; /** * Created by Thinkpad on 2018/3/3. */ public class VertxRestFul { //restFul方式 public static void main(String[] args){ //创建vertx对象 Vertx vertx= Vertx.vertx(); //web路由 Router router=Router.router(vertx); router.route("/").handler(context->{ //context.request(); context.response().end("hello root"); }); router.post("/post").handler(context->{ //context.request(); context.response().end("hello post"); }); router.get("/api/*").handler(context->{ //context.request(); context.response().end("hello *!"); }); //创建httpserver的server服务器 HttpServer server=vertx.createHttpServer(); server.requestHandler(request->{ //处理request请求 router.accept(request); }); //监听端口8082 server.listen(8082); } }(3)异步的路由
package com.fei.vertxtest; import io.vertx.core.Vertx; import io.vertx.core.http.HttpServer; import io.vertx.ext.web.Router; /** * Created by Thinkpad on 2018/3/3. */ public class VertxYiBu { public static void main(String[] args){ //创建vertx对象 Vertx vertx= Vertx.vertx(); //web路由 Router router=Router.router(vertx); router.post("/post").handler(context->{ //context.request(); context.response().end("hello post"); }); router.get("/api/*").handler(context->{ //context.request(); context.response().end("hello *!"); }); //同步请求 router.get("/sync").handler(context->{ //context.request(); context.response().end("hello sync"); System.out.print("sync"+Thread.currentThread().getName()); }); //异步请求 //执行耗时操作 //数据库访问 //服务访问 router.get("/async").blockingHandler(context->{ //context.request(); context.response().end("hello async"); System.out.print("async"+Thread.currentThread().getName()); }); //创建httpserver的server服务器 HttpServer server=vertx.createHttpServer(); server.requestHandler(request->{ //处理request请求 router.accept(request); }); //监听端口8082 server.listen(8082); } }