关于Web服务器的vert.x的概念? [英] Concept of vert.x concerning a webserver?

查看:107
本文介绍了关于Web服务器的vert.x的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解如何将vert.x应用于Web服务器.

I don't quite get how vert.x is applied for a webserver.

我对Web服务器了解的概念是基于线程的.

The concept I know for webserver is the thread-based one.

  1. 您启动Web服务器,然后该Web服务器将运行.
  2. 然后,对于每个连接的客户端,您将获得一个套接字,然后将其传递给它自己的线程处理程序.
  3. 然后,线程处理程序处理此特定套接字的任务.

因此,明确定义了哪个线程正在为哪个套接字工作.但是,对于每个套接字,您都需要一个新线程,从长远来看,这对于许多套接字来说是昂贵的.

So it is clearly defined which thread is doing the work for which socket. However for every socket you need a new thread, which is expensive in the long run for many sockets.

然后是vert.x提供的基于事件的概念.到目前为止,我已经知道,它应该这样工作:

Then there is the event-based concept that vert.x supplies. So far I have understood, it should work anyhow like this:

  1. Vertx实例部署Verticles.
  2. 顶点在后台线程中运行,但并非每个顶点都有其自己的线程.例如,在Vertx实例中可能部署了1000个顶点,但是Vertx实例仅处理8个线程(核心数* 2).
  3. 然后是事件循环.我不确定它们如何指代顶点.我已经阅读到每个顶点都有2个事件循环,但是真的不知道它是如何工作的.

作为网络服务器示例:

class WebServer: AbstractVerticle() {
    lateinit var server: HttpServer

    override fun start() {
        server = vertx.createHttpServer(HttpServerOptions().setPort(1234).setHost("localhost"))
        var router = Router.router(vertx);
        router.route("/test").handler { routingContext ->
            var response = routingContext.response();
            response.end("Hello from my first HttpServer")
        }
        server.requestHandler(router).listen()
    }
}

此WebServer可以在Vertx实例中多次部署.看起来,每个WebServer实例都有其自己的线程.当我尝试连接100个客户端并以简单的响应进行答复时,似乎每个客户端都是同步处理的.因为当我在每个服务器处理程序中执行Thread.sleep语句时,每个第二个客户端都会得到响应.但是应该是所有服务器处理程序都应该开始其1秒钟的睡眠,然后在这段时间之后几乎相同地回复所有客户端.

This WebServer can be deployed multiple times in a Vertx instance. And as it seems, each WebServer instance gets its own thread. When I try to connect 100 Clients and reply with a simple response, it seems like each Client is handled synchronously. Because when I do a Thread.sleep statement in each server handler, then the every second one client gets a response. However it should be that all server handlers should start their 1 second sleep and then almost identically reply to all clients after this time.

这是启动100个客户端的代码:

This is the code to start 100 clients:

fun main(){
    Vertx.vertx().deployVerticle(object : AbstractVerticle(){
        override fun start() {
            for(i in 0 .. 100)
                MyWebClient(vertx)
        }
    })
}

class MyWebClient(val vertx: Vertx) {
    init {
        println("Client starting ...")
        val webClient = WebClient.create(vertx, WebClientOptions().setDefaultPort(1234).setDefaultHost("localhost"))
        webClient.get("/test").send { ar ->
            if(ar.succeeded()){
                val response: HttpResponse<Buffer> = ar.result()

                println("Received response with status code ${response.statusCode()} + ${response.body()}")
            } else {
                println("Something went wrong " + ar.cause().message)
            }
        }
    }
}

有人知道这个的解释吗?

Does anybody know an explanation for this?

推荐答案

那里存在一些主要问题.

There are some major issues there.

执行此操作时:

class WebServer: AbstractVerticle() {
    lateinit var server: HttpServer

    override fun start() {
        server = vertx.createHttpServer(HttpServerOptions().setPort(1234).setHost("localhost"))
       ...
    }
}

然后这样:

vertx.deployVerticle(WebServer::class.java.name, DeploymentOptions().setInstances(4)

您将获得4个顶点,但实际上只有一个顶点会在端口上侦听.因此,您不再需要更多的并发性.

You'll get 4 verticles, but only single one of them will actually listen on the port. So, you're not getting any more concurrency.

第二,当您在Vert.x代码中使用 Thread.sleep 时,您将阻塞事件循环线程.

Second, when you use Thread.sleep in your Vert.x code, you're blocking the event loop thread.

第三,您对客户的测试不正确.创建WebClient非常昂贵,因此通过一个接一个地创建WebClient,实际上您发出的请求非常缓慢.如果您确实要测试您的Web应用程序,请使用 https://github.com/wg/wrk

Third, your test with client is incorrect. Creation of a WebClient is very expensive, so by creating those one after the other, you're actually issuing requests very slowly. If you really want to test your web application, use something like https://github.com/wg/wrk

这篇关于关于Web服务器的vert.x的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆