我正在尝试实现Http服务来上传文件。上载的文件通过原始请求进行传输。这是我用vertx 3实现的

public class MainVerticle extends AbstractVerticle {
    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new MainVerticle());
    }

    public static void log(String message) {
        System.out.printf("%s > %s\n", System.currentTimeMillis(), message);
    }

    @Override
    public void start(Future<Void> fut) {
        // Create a router object.
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());

        router.route("/api/v1.0/log/write")
                .handler(routingContext -> {
                    log("Processing... ");
                    JsonObject result = new JsonObject();
                    result.put("status", "ok");
                    routingContext.response()
                            .putHeader("content-type", "application/json")
                            .end(result.toString());
                });

        Handler<HttpServerRequest> handler = new Handler<HttpServerRequest>() {
            @Override
            public void handle(HttpServerRequest request) {
                request.handler(buffer -> log("Handling2...." + buffer.length()));
                request.endHandler(aVoid -> {
                    log("Done");
                    JsonObject result = new JsonObject();
                    result.put("status", "ok");
                    request.response()
                            .putHeader("content-type", "application/json")
                            .end(result.toString());
                });
            }
        };

        vertx
                .createHttpServer()
                .requestHandler(router::accept)
                .listen(9231, result -> {
                    if (result.succeeded()) {
                        fut.complete();
                    } else {
                        fut.fail(result.cause());
                    }
                });
    }
}


为了验证服务,我运行了以下脚本:

max=50
for i in `seq 2 $max`
do
    curl -H "Content-Type: application/json" \
        -X POST \
        --max-time 10 \
        --retry 5 \
        --retry-delay 0 \
        --retry-max-time 60 \
        --data-binary "@./512-kb.log" \
        http://localhost:9231/api/v1.0/log/write
    echo "\n"
done


令我惊讶的是,该服务执行得如此缓慢。大约大约1个请求/秒。下面是运行时日志:

textApr 08, 2018 7:41:06 PM io.vertx.core.StarterINFO: Succeeded in deploying verticle1523191273 > Processing...1523191274 > Processing...1523191275 > Processing...1523191276 > Processing...1523191277 > Processing...1523191278 > Processing...

您能告诉我如何正确实施吗?提前致谢。

最佳答案

令我惊讶的是,该服务执行得如此缓慢。大约大约1个请求/秒。下面是运行时日志


您的运行时输出似乎并不表示您每秒只有一个请求,但是每毫秒有一个请求;)

我尝试使用以下代码:

public class MainVerticle extends AbstractVerticle {
    public static void log(String message) {
        System.out.printf("%s > %s\n", Instant.ofEpochMilli(System.currentTimeMillis()), message);
    }

    @Override
    public void start(Future<Void> fut) {
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());

        router.route("/api/v1.0/log/write").handler(routingContext -> {
            log("Processing... ");
            JsonObject result = new JsonObject();
            result.put("status", "ok");
            routingContext.response().putHeader("content-type", "application/json").end(result.toString());
        });

        vertx.createHttpServer().requestHandler(router::accept).listen(9231, result -> {
            if (result.succeeded()) {
                fut.complete();
            } else {
                fut.fail(result.cause());
            }
        });
    }
}


结果如下:

2018-04-09T06:39:49.154Z > Processing...
2018-04-09T06:39:49.192Z > Processing...
2018-04-09T06:39:49.203Z > Processing...
2018-04-09T06:39:49.213Z > Processing...
2018-04-09T06:39:49.224Z > Processing...
2018-04-09T06:39:49.235Z > Processing...
2018-04-09T06:39:49.246Z > Processing...
2018-04-09T06:39:49.257Z > Processing...
...


而且我有几次相同的结果。

附言:

1)您的处理程序似乎不被使用

2)我使用io.vertx.core.Launcher运行我的Verticle或vertx可执行命令。

关于java - Vertx:处理POST正文太慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49717957/

10-12 03:27