java - 如何让Tomcat8返回503状态码?

标签 java tomcat tomcat8 server.xml

我有一个在 ~100ms 内处理 1 请求的应用程序。我打算做的是在收到的请求多于我无法处理的请求时返回 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,但仍然ALL 我的请求正在处理中。知道我做错了什么吗?

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) 如果我不自己处理请求,将发送。

关于java - 如何让Tomcat8返回503状态码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39910108/

相关文章:

forms - 成功登录Tomcat表单后重定向

java - Webapp 在本地 tomcat7 上运行,但不在 linux tomcat7 上运行

java - .set 背景颜色与十六进制颜色代码 Android Studio

java - 使用mockito的赋值方法

java - Android Studio 添加外部库(android-maven 错误)

java.util.ServiceConfigurationError : javax. xml.parsers.SAXParserFactory : Provider org. apache.xerces.jaxp.SAXParserFactoryImpl 未找到?

java - 监控 Tomcat 进程 CPU 峰值

java - 如何解决这个静态字段名称冲突?

sql-server - 使用集成安全连接时限制对 SQL Server 2012 上一个数据库的访问

tomcat - 缺少 Tomcat8 conf 目录