多线程 Vert.x 到每秒处理数千个连接

MultiThreading Vert.x to handling thousands of connections per sec

我正在构建一个类似服务器的 Messenger,我对 vert.x 做了一些更改,例如

vertx.deployVerticle(MainDeployment.class.getCanonicalName(),res -> {
          if (res.succeeded()) {

                Logger.Log("Deployed");

              } else {
                Logger.Log(res.cause().getMessage());
              }
            });

    server.requestHandler(router::accept).listen(port);


 public void createContext(String path,MyHttpHandler handler){
    //handlersMap.put(path.split("\/")[1], handler);
    if(!path.endsWith("/")){
        path += "/";
    }

    path+="*";

    map.put(path, handler);
}

这是我的 MainDeployment class

 public class MainDeployment extends AbstractVerticle{



  @Override
  public void start() throws Exception {

      //GUI.display("Sub Verticle Has Deployed");

    // Different ways of deploying verticles

    // Deploy a verticle and don't wait for it to start

   for(Entry<String, MyHttpHandler> entry : MyVertxServer.map.entrySet()){
       MyVertxServer.router.route(entry.getKey()).handler(new Handler<RoutingContext>() {

            @Override
            public void handle(RoutingContext ctx) {

                String[] handlerID = ctx.request().uri().split(ctx.currentRoute().getPath());

                String suffix = handlerID.length > 1 ? handlerID[1] : null;
                entry.getValue().Handle(ctx, new VertxUtils(), suffix);

            }
        });
   }

  }
}

文档说

 Vert.x guarantees that a particular verticle instance is never executed by more than one thread concurrently. This gives you a huge advantage as a developer, since you can program all your code as single threaded

由于每个 Verticle 都有自己的线程并且是单线程的,所以如果我的 Verticle 上传文件,它不能同时处理 2 个上传或者我误解了 NIO 的概念?

我想您应该更深入地阅读 Vert.x 文档,以更熟悉支持 API 和设计。

我强烈建议的官方文档说明如下:

Even though a Vertx instance maintains multiple event loops, any particular handler will never be executed concurrently, and in most cases (with the exception of worker verticles) will always be called using the exact same event loop.

您尝试执行的文件上传过程被认为是阻塞操作,因此应该执行:

  • 或者通过使用阻塞 API Vertx#executeBlocking
  • 或者在可以通过Vertx#createSharedWorkerExecutor调用
  • 获得的工作池中

这两种方式都会导致 事件循环 线程不会因阻塞操作而停止(我说的是 the event-loop 线程,但我们实际上是在谈论所有 event-loop 线程,因为它们可能不止一个)。