如何知道:HttpServer-in-Vertx服务的请求数量

时间:2016-07-06 19:01:20

标签: java vert.x

我用Vertx编写服务器。

我有大约4万用户。我可以打印每个请求,但我不知道如何知道我的服务器当前有多少请求

我使用了这个类:http://vertx.io/docs/apidocs/io/vertx/core/http/HttpServer.html但这不是方法吗

Vertx是很棒的工具包,但它无法显示服务器状态,真的是????

我的服务器代码:

public class MyFirstVerticle extends AbstractVerticle {

    private HttpServer httpServer = null;

    @Override
    public void start() throws Exception {
    httpServer = vertx.createHttpServer();

    httpServer.requestHandler(new Handler<HttpServerRequest>() {
        @Override
        public void handle(HttpServerRequest request) {

            String path = request.path();
            System.out.println("incoming request: [" + request.remoteAddress() + "] at " + path);
        }
    });

    httpServer.listen(9999);
    }
}

2 个答案:

答案 0 :(得分:1)

由于vert.x 3.x.x http://vertx.io/docs/vertx-dropwizard-metrics是vert.x的官方组件

我认为你搜索的是这个:
http://vertx.io/docs/vertx-dropwizard-metrics/java/#http-server-metrics

查看指标有多种可能性

答案 1 :(得分:0)

Vert.x请求非常短暂,因此仅根据每秒请求数进行计算并非最佳。但是,为了便于讨论,您可以通过以下方式实现您想要的目标:

public class MyFirstVerticle  extends AbstractVerticle {

    private HttpServer httpServer = null;

    // Bad, done for the sake of simplicity
    public final static AtomicInteger counter = new AtomicInteger(0);

    @Override
    public void start() throws Exception {

        httpServer = vertx.createHttpServer();

        httpServer.requestHandler(new Handler<HttpServerRequest>() {

            public void handle(HttpServerRequest request) {

                counter.incrementAndGet();
                String path = request.path();
                System.out.println("incoming request: [" + request.remoteAddress() + "] at " + path);
                request.response().end();

                request.endHandler(new Handler<Void>() {
                    @Override
                    public void handle(Void aVoid) {
                        counter.decrementAndGet();
                    }
                });
            }
        });

        httpServer.listen(9999);
    }
}

然后为了测试自己,你可以尝试这样的事情:

public class Main {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        MyFirstVerticle verticle = new MyFirstVerticle();

        vertx.deployVerticle(verticle);
        // Very bad, but our request are very short
        vertx.setPeriodic(1, new Handler<Long>() {
            @Override
            public void handle(Long aLong) {
                // Otherwise will overflow our console
                if (MyFirstVerticle.counter.intValue() > 0) {
                    System.out.println(MyFirstVerticle.counter.intValue());
                }
            }
        });
    }
}