Vert.x初探

Vert.x是一个异步编程框架,支持多种语言编写。还能支持集群。
用Java写的第一个HTTP请求服务。
新建maven工程,把vert.x的依赖加进去:

  <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-core</artifactId>
      <version>3.4.2</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web</artifactId>
      <version>3.4.2</version>
    </dependency>

然后在main函数下写入下面的代码;

Vertx vertx = Vertx.vertx();
        Router router = Router.router(vertx);

    
        HttpServerOptions options = new HttpServerOptions();
        vertx.createHttpServer(options).requestHandler(router::accept).listen(8080);

     
        router.route().path("/").handler(context -> {
            context.response().end("hello world");
        });

        System.out.println("已启动");

启动程序,打开浏览器访问localhost:8080即可.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容