Server sharing
服务共享
When several HTTP servers listen on the same port, vert.x orchestrates the request handling using a round-robin strategy.
当几个http服务监听同一个端口,vert.x安排循环处理这个请求。
Let’s take a verticle creating a HTTP server such as:
让我们创建一个http服务:
io.vertx.examples.http.sharing.HttpServerVerticle
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
with:
vertx run io.vertx.examples.http.sharing.HttpServerVerticle -instances 2
这个服务监听着8080端口,所以当verticle被实例化为多个。
, 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.
来源:oschina
链接:https://my.oschina.net/u/96029/blog/662109