服務共享java
When several HTTP servers listen on the same port, vert.x orchestrates the request handling using a round-robin strategy.app
當幾個http服務監聽同一個端口,vert.x安排循環處理這個請求。socket
Let’s take a verticle creating a HTTP server such as:this
讓咱們建立一個http服務:spa
io.vertx.examples.http.sharing.HttpServerVerticlecode
vertx.createHttpServer().requestHandler(request -> { request.response().end("Hello from server " + this); }).listen(8080);
This service is listening on the port 8080. So, when this verticle is instantiated multiple times as orm
with:server
vertx run io.vertx.examples.http.sharing.HttpServerVerticle -instances 2ip
這個服務監聽着8080端口,因此當verticle被實例化爲多個。ci
, what’s happening ? If both verticles would bind to the same port, you would receive a socket exception. Fortunately,
將發生什麼?2個實例綁定到同一個端口,你將得到一個socket異常。幸運的是
vert.x is handling this case for you. When you deploy another server on the same host and port as an existing server it
vert.x已經幫你處理了這個場景。當你在已經存在的地址、端口上發佈另一個服務。
doesn’t actually try and create a new server listening on the same host/port. It binds only once to the socket. When
它事實上不會建立一個新的服務監聽這個地址/端口,它只是一次性綁定到這個socket上。
receiving a request it calls the server handlers following a round robin strategy.
當收到一個請求,它會循環調用服務處理者。
Let’s now imagine a client such as:
咱們想象有個client這樣的。每100毫秒執行一次訪問服務8080。
vertx.setPeriodic(100, (l) -> { vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> { resp.bodyHandler(body -> { System.out.println(body.toString("ISO-8859-1")); }); }); });
Vert.x delegates the requests to one of the server sequentially:
vert.x將依次請求服務。
Hello from i.v.e.h.s.HttpServerVerticle@1 Hello from i.v.e.h.s.HttpServerVerticle@2 Hello from i.v.e.h.s.HttpServerVerticle@1 Hello from i.v.e.h.s.HttpServerVerticle@2 ...
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.