我创建了一个名为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/