如何让Tomcat8 return 503状态码?

How to make Tomcat8 return 503 status code?

我有一个应用程序在 ~100ms 中处理 1 请求。我打算做的是 return 503(service unavailable) 当我收到的请求多于我可以处理的请求时。 tomcat8 中的连接器组件 server.xml 看起来像这样

<Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="200"
                   URIEncoding="UTF-8"
                   redirectPort="8443"
                   maxThreads="200"
                   acceptCount="0"
                   maxConnections="200"
/>

根据 documentation 中提供的定义,我的 acceptCount 组件是我的所有 maxThreads 正在处理(正在使用)时的队列。 maxConnections 是服务器在任何给定时间将接受或处理的连接数。我打算一次只处理 200 个请求。我正在做的负载测试是通过具有以下配置的 apache 基准测试。 ab -l -n 1000 -c 1000 -s 100,总体延迟从 100ms 增加到 2000+ms,但仍在处理 所有 我的请求。知道我做错了什么吗?

EDIT1:正如 dit 所建议的那样,我 运行 它带有 20000 请求,现在我收到了 apr_socket_recv: Connection reset by peer (104),如前所述 here 表明这是由于 tomcat 的配置所致,所以,这是一个加号。但是,为什么发送此错误而不是 HTTP4XX 或 503 响应?

EDIT2:我将尝试我写的答案中提到的方法。

EDIT3:因此,为了获得有关有多少 Tomcat 线程正忙以及有多少连接已建立的信息,我在 Tomcat 上启用了 JMX .我检索了 tomcat

的 mbean 信息

link 建议我必须自己监视工作线程的数量,如果我超载并相应地发送响应。

Track the number of active concurrent requests in memory and use it for fast failing. If the number of concurrent requests is near the estimated active threads (8 in our example) then return an http status code of 503. This will prevent too many worker threads becoming busy because once the peak throughput is hit, any extra threads which become active will be doing a very light weight job of returning 503 and then be available for further processing.

apr_socket_recv: Connection reset by peer (104) 如果我不亲自处理请求,将被发送。