跟菜鳥一起學習vertx

1、什么是vertx

vert.x是Eclipse軟件基金會頂級java開源項目之一,它基于netty的、運行在jvm之上的、支持多種編程語言的異步、非阻塞、響應式編程工具集。

vert.x支持java,Kotlin,JavaScript, Groovy,Ruby,Scala

2、Vert.x擁有目前最完整的異步生態(tài)系統(tǒng)

Vert.x可以開發(fā)Web應用,但Vert.x不僅僅是一個Web開發(fā)框架,他更像Spring全家桶,是一個技術棧),或者說是一個Vert.x生態(tài)體系。在這個體系中,Vert.x提供了一些列配套的異步組件。下面對Vertx生態(tài)和Spring生態(tài)做一個對比:

項目 Spring Vertx
核心框架 spring-core vertx-core
Web開發(fā) spring-webmvc vertx-web
jdbc框架 spring-jdbc vertx-jdbc-client
redis spring-data-redis vertx-redis-client
微服務 spring-cloud vertx-hazelcast

可以說,很多的spring能做的事情,Vertx也都能實現(xiàn)。在性能上vertx系列也是甩spring幾百條街

那么既然如此,Vertx用戶群如此小眾? 被spring甩幾百條街呢?

Vertx的操作是異步的。異步帶來了更高的性能,但同時也帶來了編碼和調(diào)試的復雜度,對于追求效率工作的同學來說并不是一件輕松的事

畢竟早點下班比高并發(fā)重要...............

Vert.x 入門Hello Wolrd

1.pom文件

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

2、創(chuàng)建verticle


import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;

public class HttpServerVerticle extends AbstractVerticle {
    @Override
    public void start() throws Exception {
        // 創(chuàng)建HttpServer
        HttpServer server = vertx.createHttpServer();
        // 創(chuàng)建路由對象
        Router router = Router.router(vertx);
        //響應請求
        router.route("/").handler(event -> event.end("hello world"));
        // 把請求交給路由處理
        server.requestHandler(router);
      //監(jiān)聽端口
        server.listen(9999).onComplete(event -> {
            if(event.succeeded()){
                System.out.println("服務器啟動成功 端口:" + event.result().actualPort());
            }else{
                event.cause().printStackTrace();
            }
        });
    }
}

3、啟動

public class Main {
    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new HttpServerVerticle());
}

疑問環(huán)節(jié)

1、vertx的線程模型是怎么樣的? 我此刻在哪?

image.png
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容