为什么Vert.x为http服务器创建一个新的事件循环?

问题描述 投票:2回答:2

我有一个非常简单的Vert.x应用程序,它暴露了一个ping端点:

launcher ver提出了.Java

public class LauncherVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) throws Exception {
        DeploymentOptions options = new DeploymentOptions();
        options.setConfig(config());
        options.setInstances(1);

        String verticleName = Example1HttpServerVerticle.class.getName();
        vertx.deployVerticle(verticleName, options, ar -> {
            if (ar.succeeded()) {
                future.complete();
            } else {
                future.fail(ar.cause());
            }
        });
    }
}

ping ver提出了.Java

public class PingVerticle extends AbstractVerticle {
    @Override
    public void start(Future<Void> future) throws Exception {
        Router router = Router.router(vertx);

        router.get("/ping").handler(context -> {
            String payload = new JsonObject().put("hey", "ho").encode();
            context.response().putHeader("content-type", "application/json").end(payload);
        });
    }
}

正如所料,我可以用VisualVM看到的by default Vert.x creates two event loop threads

2 event loop threads

当然,应用程序没有做任何事情,所以我知道去添加一个http服务器到PingVerticle

    String host = "0.0.0.0";
    int port = 7777;
    vertx.createHttpServer().requestHandler(router::accept).listen(port, host, ar -> {
        if (ar.succeeded()) {
            future.complete();
        } else {
            future.fail(ar.cause());

        }
    });

现在我在VisualVM中看到有两个新线程,一个我或多或少能理解的接受器线程,以及另一个eventloop线程:

3 eventloop threads

为什么创建了第三个eventloop线程?

java multithreading vert.x event-loop
2个回答
1
投票

vert.x javadoc说:

要使用的默认事件循环线程数= 2 *计算机上的核心数。

看来,你有超过1个核心。


1
投票

关于vert.x架构的文档不多,但有一篇关于Understanding Vert.x Architecture的有趣读物

顺便说一下,我有四个核心机器,我在应用程序启动时看到相同数量的线程。我注意到,当生成更多负载时,eventloop线程数量会增加,而其他线程在每个vert.x进程中保持单个。

简而言之,

  1. vert.x受体线程0 在创建HttpServer时始终存在
  2. vert.x-事件循环线程0
  3. vert.x-事件循环线程1 Vert.x应用程序以两个eventloop线程开始,并根据需要动态添加,直到核心数量增加一倍,即根据文档的2 *核心。
  4. vert.x封闭的线程检查器 总是在那里检测eventloop上的阻塞例程超过2000毫秒。
© www.soinside.com 2019 - 2024. All rights reserved.