【发布时间】:2020-08-31 21:02:38
【问题描述】:
似乎一个verticle只能由一个线程执行,并且总是由同一个线程执行。 但是 Vert.x 能够通过为每个 CPU 创建一个线程来使用机器中的所有 CPU。 每个线程可以向多个 Verticle 发送消息。
但是在性能测试期间在标准 Verticle 上分析 vertx http 服务器时,我只能看到 1 个线程处理所有处理 (vert.x-eventloop-thread-0)。
我可以做些什么来让我的所有 8 个事件循环线程处理到 Verticle 的消息?
CountDownLatch latch = new CountDownLatch(1);
final List<Throwable> errors = new ArrayList<>();
local = new Local(getVertx(), settings, options, connectionHandler, exceptionHandler);
LocalVerticle.vertxDeployMap.put(local.hashCode(), local);
DeploymentOptions dop = new DeploymentOptions()
.setInstances(new VertxOptions()
.getEventLoopPoolSize())
.setConfig(new JsonObject().put("local", local.hashCode()));
Network.getVertx().deployVerticle(LocalVerticle.class.getName(), dop, (event) -> {
if (!event.failed()) {
latch.countDown();
} else {
errors.add(event.cause());
}
});
boolean await = latch.await(10, TimeUnit.SECONDS);
if (!await) {
if (errors.isEmpty()) {
throw new Exception("Failed to initialize Local Verticle");
} else {
throw new Exception("Failed to initialize Local Verticle", errors.get(0));
}
}
LocalVerticle.vertxDeployMap.remove(local.hashCode());
public class LocalVerticle extends AbstractVerticle {
static final Map<Integer, Local> vertxDeployMap = new HashMap<>();
private HttpServer httpServer;
@Override
public void start() throws Exception {
Local local = vertxDeployMap.get(this.config().getInteger("local"));
HttpServerOptions options = local.getOptions();
Handler<HttpConnection> connectionHandler = local.getConnectionHandler();
Handler<Throwable> exceptionHandler = local.getExceptionHandler();
Router router = local.getRouter();
this.httpServer = this.vertx
.createHttpServer(options)
.exceptionHandler(exceptionHandler)
.connectionHandler(connectionHandler)
.requestHandler(router)
.listen();
}
@Override
public void stop() throws Exception {
if (this.httpServer != null) {
this.httpServer.close();
this.httpServer = null;
}
}
}
【问题讨论】:
标签: netty vert.x event-loop