Vertx WebClient 共享还是跨多个 Verticles ?

Vertx WebClient shared vs single across multiple verticles?

我正在使用 vert.x 作为 api 网关来将调用路由到下游服务。

截至目前,我正在使用跨多个 Verticle 共享的单个 Web 客户端实例(通过 guice 注入)

每个 Verticle 都有自己的 Web 客户端有意义吗?它会有助于提高性能吗? (我的每个网关实例运行 64 个 Vericle,每秒处理大约 1000 个请求)

每种方法的优缺点是什么?

有人可以帮助找出理想的策略吗?

谢谢

Vert.x 针对使用单个 WebClient per-Verticle 进行了优化。在线程之间共享单个 WebClient 实例可能有效,但它会对性能产生负面影响,并可能导致某些代码 运行 在“错误的”event-loop 线程上,as described by Julien Viet, Vert.x 的首席开发人员:

So if you share a web client between verticles, then your verticle might reuse a connection previously open (because of pooling) and you will get callbacks on the event loop you won't expect. In addition there is synchronization in the web client that might become contented when used intensively from different threads.

此外,the Vert.x documentation for HttpClient,这是 WebClient 使用的底层对象,明确声明不在 Vert.x 个上下文之间共享它(每个 Verticle 都有自己的上下文):

The HttpClient can be used in a Verticle or embedded.

When used in a Verticle, the Verticle should use its own client instance.

More generally a client should not be shared between different Vert.x contexts as it can lead to unexpected behavior.

For example a keep-alive connection will call the client handlers on the context of the request that opened the connection, subsequent requests will use the same context.