如何监控/记录 Tomcat 的线程池?

发布于 2024-12-03 09:08:26 字数 800 浏览 2 评论 0原文

我有一个 Tomcat 安装,我怀疑由于线程未正确释放,线程池可能会随着时间的推移而减少。当达到 maxthreads 时,我在 catalina.out 中收到错误,但我想每五分钟将正在使用的线程数记录到一个文件中,以便我可以验证这个假设。有人可以建议如何做到这一点吗?

此外,在此安装中没有 Tomcat 管理器,看来最初安装的人出于某种原因删除了管理器 web 应用程序。我不确定经理是否能够执行上述操作,或者我是否可以在不损坏现有安装的情况下重新安装它?我真正想做的就是跟踪线程池。

另外,我注意到 Tomcat 的最大线程数为 200,但 Apache 的最大并发连接数较低(Apache 使用 mod_proxy 和 mod_proxy_ajp (AJP 1.3) 来提供 Tomcat)。这似乎也是错误的,这些数字之间的正确关系是什么?

非常感谢任何帮助:D

更新:只是一个快速更新,说明直接 JMX 访问有效。不过,我还必须设置 Dcom.sun.management.jmxremote.host。我将其设置为 localhost 并且它有效,但是没有它就没有骰子。如果其他人在尝试启用 JMX 时遇到类似问题,我建议您也设置此值,即使您是从本地计算机连接也是如此。似乎某些版本的 Tomcat 需要它。


简单更新一下,直接 JMX 访问有效。不过,我还必须设置 Dcom.sun.management.jmxremote.host。我将其设置为 localhost 并且它有效,但是没有它就没有骰子。如果其他人在尝试启用 JMX 时遇到类似问题,我建议您也设置此值,即使您是从本地计算机连接也是如此。似乎某些版本的 Tomcat 需要它。

I have a Tomcat installation where I suspect the thread pool may be decreasing over time due to threads not being properly released. I get an error in catalina.out when maxthreads is reached, but I would like to log the number of threads in use to a file every five minutes so I can verify this hypothesis. Would anyone please be able to advise how this can be be done?

Also in this installation there is no Tomcat manager, it appears whoever did the original installation deleted the manager webapp for some reason. I'm not sure if manager would be able to do the above or if I can reinstall it without damaging the existing installation? All I really want to do is keep track of the thread pool.

Also, I noticed that maxthreads for Tomcat is 200, but the max number of concurrent connections for Apache is lower (Apache is using mod_proxy and mod_proxy_ajp (AJP 1.3) to feed Tomcat). That seems wrong too, what is the correct relationship between these numbers?

Any help much appreciated :D

Update: Just a quick update to say the direct JMX access worked. However I also had to set Dcom.sun.management.jmxremote.host. I set it to localhost and it worked, however without it no dice. If anyone else has a similar problem trying to enable JMX I recommend you set this value also, even if you are connecting from the local machine. Seems it is required with some versions of Tomcat.


Just a quick update to say the direct JMX access worked. However I also had to set Dcom.sun.management.jmxremote.host. I set it to localhost and it worked, however without it no dice. If anyone else has a similar problem trying to enable JMX I recommend you set this value also, even if you are connecting from the local machine. Seems it is required with some versions of Tomcat.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提笔书几行 2024-12-10 09:08:26

直接 JMX 访问

尝试将其添加到 catalina.sh/bat

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=5005
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

更新:Alex P 建议在某些情况下可能还需要以下设置:

-Dcom.sun.management.jmxremote.host=localhost

这会在端口 5005 上启用远程匿名 JMX 连接。您还可以考虑 JVisualVM,它更受欢迎,并且允许通过插件浏览 JMX。

您正在寻找的是 Catalina -> 线程池 -> http-bio-8080 ->各种有趣的指标。

JMX 代理 servlet

更简单的方法可能是使用 Tomcat 的 JMX 代理 servlet,如下: < a href="http://localhost:8080/manager/jmxproxy">http://localhost:8080/manager/jmxproxy。例如,尝试以下查询:

$ curl --user tomcat:tomcat http://localhost:8080/manager/jmxproxy?qry=Catalina:name=%22http-bio-8080%22,type=ThreadPool

一点点 grepping 和脚本编写,您就可以轻松远程监控您的应用程序。请注意,tomcat:tomcatconf/tomcat-users.xml 中具有 manager-jmx 角色的用户的用户名/密码。

Direct JMX access

Try adding this to catalina.sh/bat:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=5005
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

UPDATE: Alex P suggest that the following settings might also be required in some situations:

-Dcom.sun.management.jmxremote.host=localhost

This enables remote anonymous JMX connections on port 5005. You may also consider JVisualVM which is much more please and allows to browse JMX via plugin.

What you are looking for is Catalina -> ThreadPool -> http-bio-8080 -> various interesting metrics.

JMX proxy servlet

Easier method might be to use Tomcat's JMX proxy servlet under: http://localhost:8080/manager/jmxproxy. For instance try this query:

$ curl --user tomcat:tomcat http://localhost:8080/manager/jmxproxy?qry=Catalina:name=%22http-bio-8080%22,type=ThreadPool

A little bit of grepping and scripting and you can easily and remotely monitor your application. Note that tomcat:tomcat is the username/password of user having manager-jmx role in conf/tomcat-users.xml.

青芜 2024-12-10 09:08:26

您可以部署 jolokia.war ,然后检索 JSON 中的 mbean 值(无需管理器):

< a href="http://localhost:8080/jolokia/read/Catalina:name=%22http-nio-8080%22,type=ThreadPool?ignoreErrors=true" rel="nofollow noreferrer">http://localhost:8080/jolokia/read/Catalina:name=*,type=ThreadPool?ignoreErrors=true

如果您只需要一些值(currentThreadsBusy、maxThreads、currentThreadCount、connectionCount) ):

<一href="http://localhost:8080/jolokia/read/Catalina:name=%22http-nio-8080%22,type=ThreadPool/currentThreadsBusy,maxThreads,currentThreadCount,connectionCount?ignoreErrors=true" rel="nofollow noreferrer">http://localhost:8080/jolokia/read/Catalina:name=*,type=ThreadPool/currentThreadsBusy,maxThreads,currentThreadCount,connectionCount?ignoreErrors=true

{
    request: {
       mbean: "Catalina:name="http-nio-8080",type=ThreadPool",
       attribute: [
          "currentThreadsBusy",
          "maxThreads",
          "currentThreadCount",
          "connectionCount"
       ],
       type: "read"
    },
    value: {
       currentThreadsBusy: 1,
       connectionCount: 4,
       currentThreadCount: 10,
       maxThreads: 200
    },
    timestamp: 1490396960,
    status: 200
}

注意:本示例适用于 Tomcat7+。

You can deploy jolokia.war and then retrieve mbeans values in JSON (without the manager):

http://localhost:8080/jolokia/read/Catalina:name=*,type=ThreadPool?ignoreErrors=true

If you want only some values (currentThreadsBusy, maxThreads, currentThreadCount, connectionCount):

http://localhost:8080/jolokia/read/Catalina:name=*,type=ThreadPool/currentThreadsBusy,maxThreads,currentThreadCount,connectionCount?ignoreErrors=true

{
    request: {
       mbean: "Catalina:name="http-nio-8080",type=ThreadPool",
       attribute: [
          "currentThreadsBusy",
          "maxThreads",
          "currentThreadCount",
          "connectionCount"
       ],
       type: "read"
    },
    value: {
       currentThreadsBusy: 1,
       connectionCount: 4,
       currentThreadCount: 10,
       maxThreads: 200
    },
    timestamp: 1490396960,
    status: 200
}

Note: This example works on Tomcat7 +.

沧桑㈠ 2024-12-10 09:08:26

寻求更具企业级的解决方案。我一直在我们的生产环境中使用 New Relic。

这提供了线程池随时间变化的图表。

For a more enterprise solution. I have been using New Relic in our production environment.

This provides a graph of the changes to the threadpool over time.

著墨染雨君画夕 2024-12-10 09:08:26

There are cheaper tools out meanwhile: I am using this jar here: https://docs.cyclopsgroup.org/jmxterm
You can automate it via shell/batch scripts. I regexed the output and let prometheus poll it for displaying it in grafana.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文