我有一个程序,我在其中部署了一个 worker verticle(vertx 版本 3.6.3)。从这里,工人 Verticle,我正在使用 Vertx WebClient 库发出 HTTP 请求。我的 vertx 工作池大小为 20,事件循环池大小为 10。之后,发出 http 请求(在 send() 调用之后),我阻塞了使用可完成未来发出 HTTP 请求(工作线程)的工作线程。当我阻塞工作线程时,HTTP 请求永远不会响应并且总是超时。当工作线程没有被阻塞时它会响应。我想,如果我阻塞了工作线程,池中还有其他工作线程来处理 HTTP 请求。我在这里做错了什么?此外,我启用了网络日志活动,但我没有看到任何网络日志打印在日志中。
以下是我尝试过的程序,我正在运行一个示例 HTTP 服务器,该服务器在 localhost 的 8080 端口上运行,响应良好。
import java.util.concurrent.TimeUnit;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
public class VertxTestMain extends AbstractVerticle {
private static int workerPoolSize = 20;
private static int eventLoopPoolSize = 10;
@Override
public void start() {
vertx.eventBus().consumer("vertx.address", event -> {
CompletableFuture<String> future = new CompletableFuture<>();
doAHttpRequest(vertx, future);
try {
//future.get(20, TimeUnit.SECONDS);
} catch (Exception e) {
System.out.println(Thread.currentThread().getName()+ " ----- HTTP request never responded");
e.printStackTrace();
}
});
}
private void doAHttpRequest(Vertx vertx, CompletableFuture<String> future) {
//System.setProperty("java.util.logging.config.file", "/opt/maglev/persisted/data/vertx-default-jul-logging.properties");
WebClientOptions options = new WebClientOptions();
options.setLogActivity(true);
WebClient webClient = WebClient.create(vertx, options );
int port = 8080;
String host = "localhost";
String url = "/";
System.out.println(Thread.currentThread().getName()+ " ----- Sending http://" + host + ":" + port + "/" + url);
// Send a GET request
webClient
.get(port, host, url)
.timeout(10000)
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
System.out.println(Thread.currentThread().getName()+ " ----- Received response. " + response.bodyAsString());
System.out.println(Thread.currentThread().getName()+ " ----- Received response with status code. " + response.statusCode());
future.complete("success");
} else {
System.out.println(Thread.currentThread().getName()+ " ----- Something went wrong. " + ar.cause().getMessage());
future.completeExceptionally(ar.cause());
}
});
}
public static void main(String[] args) {
DeploymentOptions deploymentOptions = new DeploymentOptions().setWorker(true).setInstances(2);
VertxOptions vertxOptions = new VertxOptions();
vertxOptions.setWorkerPoolSize(workerPoolSize);
vertxOptions.setEventLoopPoolSize(eventLoopPoolSize);
Vertx vertx = Vertx.vertx(vertxOptions);
vertx.deployVerticle(VertxTestMain.class.getName(), deploymentOptions, event -> {
if(event.succeeded()) {
System.out.println(Thread.currentThread().getName()+ " ----- VertxTestMain verticle is deployed");
vertx.eventBus().send("vertx.address", "send");
}
else {
System.out.println(Thread.currentThread().getName()+ " ----- VertxTestMain verticle deployment failed. " + event.cause());
}
});
}
}