增加tomcat 7中的并发连接数

问题描述:

想知道如何在 tomcat 7 中增加并发连接数.我做了一些研究,从 apache 文档中发现将值设置为 acceptCount、maxConnections 并增加 maxThreads 可以做到这一点,但是尝试后,我只能提交1000 个请求中有 500 个请求.以下是我来自 server.xml 文件的片段 -

Wondering how to increase the number of concurrent connections in tomcat 7. I did some research and found from apache documentation that setting values to acceptCount, maxConnections and increaseing the maxThreads would do it, but when tried, I was only able to submit 500 requests out of 1000. Below is my snippet from server.xml file -

      <Connector port="443" protocol="HTTP/1.1" SSLEnabled="true"
       maxThreads="2000" acceptCount="1000" maxConnections="1000"
       scheme="https" secure="true"
       keystoreFile="certs/tomcat.keystore" keystorePass="xxxxxxxx"
       clientAuth="false" sslProtocol="TLS" server="Apache" />

你能帮我找出最好和最简单的方法来摆脱这个吗?

Can you please help me finding out the best and easy way to get rid of this one.?

谢谢,西里斯.

创建大量线程1 并接受大量请求并不意味着您的服务器将能够处理请求

Creating a large number of threads1 and accepting a large number of requests doesn't mean that your server will be able to process the requests

如果您有 N 个线程并且只有 M 个物理处理器/内核,那么如果 M >= N 和平均 M/N 个处理器,则每个线程将获得 1 个处理器如果 M .假设您有 N 个请求,每个请求在 1 个线程上运行,每个请求需要 R 秒的 CPU 时间.运行一个请求所花费的平均时间 TT = Min(R, R * N/M) 秒.很明显,当您增加 N(活动线程和活动请求的数量)时,每个单独请求的平均经过时间 T 会成比例增加.

If you have N threads and only M physical processors / cores, then each thread will get 1 processor if M >= N and an average M / N processors if M < N. Assume you have N requests each running on a 1 thread, and each request takes R seconds of CPU time. The average elapsed time T taken to run one request is T = Min(R, R * N / M) seconds. It is fairly clear that as you increase N (the number of active threads and active requests) the average elapsed time T for each individual request increases proportionally.

除此之外,如果您有很多线程,它们都将使用内存,并且所有线程都将竞争对共享数据结构...或数据库的访问.所有这些额外的资源使用和争用都以各种方式增加了整个系统的开销.

In addition to that, if you have lots of threads, they will all be using memory, and all will be competing for access to shared data structures ... or the database. An all of this extra resource usage and contention is increasing the overheads of the system as a whole in various ways.

所以,我怀疑正在发生的事情是,每个线程都试图同时处理请求,时间 T 开始接近客户端或服务器端请求超时.(并且请注意,调度程序的变幻莫测等意味着任何给定请求的实际时间可能少于或显着超过平均值.)当请求超时时,这反过来会降低获得请求的吞吐量完成,因为对每个超时请求执行的工作(通常)是浪费的.

So, what I suspect is happening is that with that number of threads each trying to process requests simultaneously, the time T is starting to approach the client or server side request timeout. (And note that the vagaries of the scheduler, etc mean that the actual time for any given request could be less than or considerably more than the average.) When a request times out, this in turn reduces the throughput in terms of requests that get completed, because the work performed on each timed out requests is (typically) wasted.

除非请求需要与缓慢的外部服务通信,否则我建议您将线程数减少到不超过 200 ...... Tomcat 默认2.我预计这将增加系统吞吐量.它不一定会让您处理在那段时间内启动的所有 1000 个请求,但我预测它会增加成功处理的请求数量.

Unless the requests entail talking to slow external services, I'd advise you to REDUCE the number of threads to no more than 200 ... the Tomcat default2. I expect that this will increase the system throughput. It won't necessarily let you process all of those 1000 requests that were launched in that period, but I predict that it will increase the number of requests that are successfully processed.

1 - 实际上,将线程数增加到 1000 并不意味着您将能够接受 1000 个请求.如果您有数百个处于 RUNNABLE 状态的线程,则 Tomcat 的侦听器线程(调用 ServerSocket.accept() 的线程)很可能 CPU 不足,无法跟上请求到达率.

1 - Indeed, increasing the number of threads to 1000 doesn't even mean that you will be able to accept 1000 requests. If you have hundreds of threads in RUNNABLE state, it is likely that Tomcat's listener thread (the one that calls ServerSocket.accept()) will be CPU starved and won't be able to keep up with the request arrival rate.

2 - 您需要对系统进行一些性能调整,但如果减少它甚至进一步改善事情,我不会感到惊讶.这将取决于您的硬件、您的应用程序和(我希望)您的后端数据库.