我有一个程序,我在其中部署了一个worker verticle (vertx版本3.6.3)。在worker verticle中,我使用Vertx WebClient库发出了一个HTTP请求。我的vertx工作线程池大小是20,事件循环池大小是10。在发出http请求之后(在send()调用之后),我阻塞了使用completable发出HTTP请求(工作线程)的工作线程。当我阻塞工作线程时,HTTP请求永远不会响应,而且总是超时。它在工作线程未被阻塞时进行响应。我想,如果我阻塞工作线程,池中还有其他工作线程来支持HTTP请求。我在这里做错了什么?此外,我已经启用了网络日志活动,但在日志中看不到任何网络日志打印。
下面是我尝试过的程序,我正在运行一个示例HTTP服务器,它在端口8080的localhost上运行,响应良好。
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());
}
});
}
}发布于 2020-03-22 20:05:01
您不允许启动HTTP请求。
从你的方法返回Future,而不是传递它:
private CompletableFuture<String> doAHttpRequest(Vertx vertx) {
CompletableFuture<String> future = new CompletableFuture<>();
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());
}
});
return future;
}您也可以重用WebClient,不需要为每个请求创建它。
此外,还可以查看Promise API Vert.x提供的内容,因为它可能更适合您的用例:
https://stackoverflow.com/questions/60796620
复制相似问题