2016-07-06 59 views
1

我使用Vertx編寫服務器。如何知道:HttpServer-in-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); 
    } 
} 

回答

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()); 
       } 
      } 
     }); 
    } 
} 
1

由於vert.x 3。 XX http://vertx.io/docs/vertx-dropwizard-metrics是vert.x的官方組件

我想你搜索什麼是這樣的:
http://vertx.io/docs/vertx-dropwizard-metrics/java/#http-server-metrics

有幾種可能性,查看指標​​

相關問題