码头,我的五个线程在哪里? [英] Jetty, where are my five threads?

查看:83
本文介绍了码头,我的五个线程在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring + jetty.我正在配置码头:

I'm using spring+jetty. I'm configuring jetty:

@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
    final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(port);
    factory.addServerCustomizers((Server server) -> {
        final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
        threadPool.setMinThreads(minThreads);
        threadPool.setMaxThreads(maxThreads);
        threadPool.setIdleTimeout(1000);
    });
    return factory;
}

它有效,但很奇怪.

  1. 设置minThreads = 1,maxThreads = 5,它不处理连接.
  2. 设置minThreads = 1,maxThreads = 6,并且仅处理一个连接
  3. 设置minThreads = 1,maxThreads = 7,并且仅处理两个连接. 当我说处理时,我的意思是说它接受连接,但是什么也不做.不回复,不中止他们. (我认为他们正在排队)
  1. Set minThreads=1, maxThreads=5, and it doesn't processing connections.
  2. Set minThreads=1, maxThreads=6, and it processing only one connection
  3. Set minThreads=1, maxThreads=7, and it processing only two connections. When I say processing, I mean, that it accepts connections, but do nothing. Doesn't reply, doesn't abort them. (I think they are in a queue)

那我的5个线程在哪里?

So, where are my 5 threads?

推荐答案

您没有考虑每个连接器如何将选择器和接受器分配到同一线程池中.

You are not accounting for how each of your connectors allocates selectors and acceptors into the same threadpool.

您还没有考虑线程池中的系统/硬件影响(您拥有多少个CPU内核?是的,这很重要).

You are also not taking into account the system/hardware effects into the thread pool (how many cpu cores do you have? yes it matters).

您还正在定义一个很小的线程池.您打算连续仅提供1个http连接吗? 1个连接器上使用HTTP/1.0,具有保持活动状态且无压缩,并具有永不失败或超时的完美网络条件?

You are also defining a rediculously tiny threadpool. Do you plan on only serving 1 http connection in a row? on 1 connector, using HTTP/1.0, with keep-alive, and no compression, with perfect network conditions that never fail or timeout?

您是否100%确定您的用户代理(客户端)会遵守这些规则?

Are you 100% sure that your user-agents (clients) will follow those rules?

提示:您正在使用现代Web浏览器在stackoverflow上查看的当前网页将使用线程池中的9到18个活动线程,将线程池的最大数量增加到约35个,并且在399毫秒内完成了对它们的处理.

Hint: the web page you are looking at right now, on stackoverflow, using a modern web browser, would have used from 9 to 18 active threads from the thread pool, grown the thread pool maximum to about 35, and would have been done with them all in 399ms.

请考虑以下任何将来的决定都会增加您的线程池压力.

Consider that any of the following future decisions would increase your thread pool pressures.

  • 使用jetty-proxy:AsyncProxyServlet,AsyncMiddlemanServlet
  • 在服务器端使用WebSockets.
  • 使用WebSocket客户端
  • 使用Http客户端
  • 启用HTTP/2

您的设置应该抛出IllegalStateException,表明您的配置很低.

Your settings should have thrown an IllegalStateException indicating your very low configuration.

java.lang.IllegalStateException: Insufficient threads: max=8 < needed(acceptors=1 + selectors=8 + request=1)

既然您说过您正在使用Spring,请考虑在以下位置关注有关此问题的未解决问题

Since you said you are using Spring, consider following the open issue about this at

https://github.com/spring-projects/spring-boot/问题/8917

以下是一些有关调整最大线程的一般建议(大挥手)...

Here's some general (large hand waving) advice about tuning for maximum threads ...

  • 飞行中的活动连接数是您的基准,将其作为最大线程数开始,然后从此增加.
  • HTTP/1.x使用的线程少于HTTP/2
  • 较小的网络资源倾向于较低的最大线程数
  • 持续时间较长或较长的Web资源倾向于更大的最大线程数
  • 使用Servlet 3.1+异步I/O意味着更少的最大线程(仅在需要实际读取和/或写入的情况下才使用线程)
  • 使用Servlet 3.0(或更旧版本)阻止I/O意味着更多的最大线程(必须将线程专用于每个连接的读/写)
  • 使用HTTP/1.x,平均每个网站有
  • 200个到500个最大线程,具有平均负载,平均Web资源大小和平均Web资源时间.
  • 对于繁重的网站,使用许多功能(代理,websockets,httpclient,http/2),通常最大线程配置为3,000到5,000.
  • 对于极端网站,请尽可能多地使用CPU内核,专门使用Servlet 3.1异步I/O,最多将线程线程数增加到9,000(最多20,000)(取决于您的负载配置文件)
  • number of active, in flight, connections is your baseline, start with that as your maximum thread count and move up from there.
  • HTTP/1.x uses less threads then HTTP/2
  • small web resource sizes lean towards lower maximum threads
  • large or long duration web resources lean towards larger maximum threads
  • use of Servlet 3.1+ async I/O means less maximum threads (only uses threads if there is actual read and/or write needed)
  • use of Servlet 3.0 (or older) blocking I/O means more maximum threads (must dedicate thread to each connection read/write)
  • 200 to 500 maximum threads for an average web site, with average load, and average web resource sizes, and average web resource timing, using HTTP/1.x
  • for a heavy web site, using many features (proxy, websockets, httpclient, http/2) a maximum threads configuration of 3,000 to 5,000 is typical.
  • for extreme web sites, dedicate as many CPU cores as you can, use Servlet 3.1 Async I/O exclusively, crank your threads maximum to 9,000 on up to 20,000 (depending on the load profile you have)

我们知道使用Jetty的最小的网站使用19MB的内存,并且在其线程池中配置了8个最大线程. (位于挪威偏远地区的气象站)

The smallest web site we know of using Jetty uses 19MB of memory, and has a configured 8 maximum threads in its thread pool. (its a weather station in a remote part of Norway)

我们所知道的最大的使用Jetty的单机网站在30台核心计算机上使用189GB内存,最大配置了30,000个线程,并具有8个网络接口,每个接口均启用HTTP/1.x和HTTP/2 SSL + Gzip + WebSockets,任意时刻平均有280,000个活动连接(每天每天特定时间的峰值只有800,000个活动连接)

The largest single machine web site we know of using Jetty uses 189GB of memory, on a 30 core machine, with 30,000 thread maximum configured, with 8 network interfaces, HTTP/1.x and HTTP/2 enabled on each, doing SSL + Gzip + WebSockets, with an average of 280,000 active connections at any one moment (peaks at just shy of 800,000 active connections at certain times of day, every day)

这篇关于码头,我的五个线程在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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