vert.x - 在 vert.x 中,在运行时创建多个 HttpServer 实例有意义吗?

标签 vert.x

我创建了一个名为HttpServerVerticle的verticle,并在其中通过vertx.createHttpServer()创建一个HttpServer实例,然后在我的main中我通过vertx.deployVerticle("xxx.xxx.HttpServerVerticle",deploymentOptionsOf(instances = 2))部署了这个HTTP verticle超过1个实例。

在运行时中有多个 HttpServer 实例有意义吗?如果是的话,为什么我没有看到类似的错误消息,例如“8080 端口已在使用中”?

最佳答案

Vert.x 实际上会 round-robin between your HttpServer instances listening on the same port :

When several HTTP servers listen on the same port, vert.x orchestrates the request handling using a round-robin strategy...

So, when [a] verticle is instantiated multiple times as with: vertx run io.vertx.examples.http.sharing.HttpServerVerticle -instances 2, what’s happening? If both verticles would bind to the same port, you would receive a socket exception. Fortunately, vert.x is handling this case for you. When you deploy another server on the same host and port as an existing server it doesn’t actually try and create a new server listening on the same host/port. It binds only once to the socket. When receiving a request it calls the server handlers following a round robin strategy...

Consequently the servers can scale over available cores while each Vert.x verticle instance remains strictly single threaded, and you don’t have to do any special tricks like writing load-balancers in order to scale your server on your multi-core machine.

因此,如果需要跨内核扩展,创建多个 HttpServer 实例既安全又值得鼓励。

关于vert.x - 在 vert.x 中,在运行时创建多个 HttpServer 实例有意义吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62558613/

相关文章:

java - httpClient 连接未关闭

java - 使用 vertx 在 mongoDB 中批量写入

java - 如何防止Vertx自动写入日志?

java - 模拟 Verticle 所依赖的类

java - 在 VertX 中使用 Bcrypt 提高性能

java - 如何响应来自eventbus消费者的http请求

java - Vertx JDBC 的底层工作原理

java - 使用 EventBus 时处理 Vert.x NetSocket writeQueueFull

java - Vert.x Java 实时服务器客户端通信

java - Vert.x 的 Verticle(s) JSON/YAML 配置(每个环境最好)