web server apache tomcat11-21-monitor and management 监控与管理

发布于:2024-04-26 ⋅ 阅读:(29) ⋅ 点赞:(0)

前言

整理这个官方翻译的系列,原因是网上大部分的 tomcat 版本比较旧,此版本为 v11 最新的版本。

开源项目

从零手写实现 tomcat minicat 别称【嗅虎】心有猛虎,轻嗅蔷薇。

系列文章

web server apache tomcat11-01-官方文档入门介绍

web server apache tomcat11-02-setup 启动

web server apache tomcat11-03-deploy 如何部署

web server apache tomcat11-04-manager 如何管理?

web server apache tomcat11-06-Host Manager App -- Text Interface

web server apache tomcat11-07-Realm Configuration

web server apache tomcat11-08-JNDI Resources

web server apache tomcat11-09-JNDI Datasource

web server apache tomcat11-10-Class Loader

...

简介

监控是系统管理的关键方面。查看运行中的服务器,获取一些统计信息或重新配置应用程序的某些方面都是日常管理任务。

启用 JMX 远程

注意:只有在您要远程监视 Tomcat 时才需要此配置。如果您打算使用与 Tomcat 运行的相同用户在本地监视它,则不需要此配置。

Oracle 网站包含了有关选项列表以及如何在 Java 11 上配置 JMX 远程的信息:Java 11 JMX 远程配置

以下是 Java 11 的快速配置指南:

在 Tomcat 的 setenv.bat 脚本中添加以下参数(有关详细信息,请参阅 RUNNING.txt)。 注意:此语法适用于 Microsoft Windows。命令必须在同一行上。它被包装以增加可读性。如果 Tomcat 作为 Windows 服务运行,请使用其配置对话框为服务设置 Java 选项。对于 Linux、MacOS 等,请从行的开头删除 "set "。

set CATALINA_OPTS=-Dcom.sun.management.jmxremote.port=%my.jmx.port%
  -Dcom.sun.management.jmxremote.rmi.port=%my.rmi.port%
  -Dcom.sun.management.jmxremote.ssl=false
  -Dcom.sun.management.jmxremote.authenticate=false

如果不设置 com.sun.management.jmxremote.rmi.port,则 JSR 160 JMX-Adaptor 将随机选择一个端口,这将使得配置防火墙以允许访问变得困难。

如果需要 TLS:

更改并添加以下内容:

  -Dcom.sun.management.jmxremote.ssl=true
  -Dcom.sun.management.jmxremote.registry.ssl=true

要配置协议和/或密码套件,请使用:

  -Dcom.sun.management.jmxremote.ssl.enabled.protocols=%my.jmx.ssl.protocols%
  -Dcom.sun.management.jmxremote.ssl.enabled.cipher.suites=%my.jmx.cipher.suites%

对于客户端证书身份验证,请使用:

  -Dcom.sun.management.jmxremote.ssl.need.client.auth=%my.jmx.ssl.clientauth%

如果需要授权(强烈建议始终使用身份验证的 TLS):

更改并添加以下内容:

  -Dcom.sun.management.jmxremote.authenticate=true
  -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password
  -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access

编辑访问授权文件 $CATALINA_BASE/conf/jmxremote.access:

monitorRole readonly
controlRole readwrite

编辑密码文件 $CATALINA_BASE/conf/jmxremote.password:

monitorRole tomcat
controlRole tomcat

提示:密码文件应该是只读的,并且只能被 Tomcat 运行的操作系统用户访问。

或者,您可以使用以下配置 JAAS 登录模块:

  -Dcom.sun.management.jmxremote.login.config=%login.module.name%

如果需要指定用于发送到客户端的 RMI 存根的主机名(例如,因为必须使用的公共主机名与本地主机名不同),则可以设置:

set CATALINA_OPTS=-Djava.rmi.server.hostname

如果需要为 JMX 服务绑定到的特定接口,请设置:

set CATALINA_OPTS=-Dcom.sun.management.jmxremote.host

使用 JMX 远程 Ant 任务管理 Tomcat

为了简化 Ant 中的 JMX 使用,提供了一组可用于 antlib 的任务。

antlib:将 catalina-ant.jar 从 $CATALINA_HOME/lib 复制到 $ANT_HOME/lib。

以下示例显示了 JMX Accessor 的用法:

注意:这里对 name 属性值进行了包装以增加可读性。它必须全部在同一行上,没有空格。

<project name="Catalina Ant JMX"
      xmlns:jmx="antlib:org.apache.catalina.ant.jmx"
      default="state"
      basedir=".">
  <property name="jmx.server.name" value="localhost" />
  <property name="jmx.server.port" value="9012" />
  <property name="cluster.server.address" value="192.168.1.75" />
  <property name="cluster.server.port" value="9025" />

  <target name="state" description="Show JMX Cluster state">
    <jmx:open
      host="${jmx.server.name}"
      port="${jmx.server.port}"
      username="controlRole"
      password="tomcat"/>
    <jmx:get
      name=
"Catalina:type=IDataSender,host=localhost,
senderAddress=${cluster.server.address},senderPort=${cluster.server.port}"
      attribute="connected"
      resultproperty="IDataSender.backup.connected"
      echo="false"
    />
    <jmx:get
      name="Catalina:type=ClusterSender,host=localhost"
      attribute="senderObjectNames"
      resultproperty="senderObjectNames"
      echo="false"
    />
    <!-- get current maxActiveSession from ClusterTest application
       echo it to Ant output and store at
       property <em>clustertest.maxActiveSessions.original</em>
    -->
    <jmx:get
      name="Catalina:type=Manager,context=/ClusterTest,host=localhost"
      attribute="maxActiveSessions"
      resultproperty="clustertest.maxActiveSessions.original"
      echo="true"
    />
    <!-- set maxActiveSession to 100
    -->
    <jmx:set
      name="Catalina:type=Manager,context=/ClusterTest,host=localhost"
      attribute="maxActiveSessions"
      value="100"
      type="int"
    />
    <!-- get all sessions and split result as delimiter <em>SPACE</em> for easy
       access all session ids directly with Ant property sessions.[0..n].
    -->
    <jmx:invoke
      name="Catalina:type=Manager,context=/ClusterTest,host=localhost"
      operation="listSessionIds"
      resultproperty="sessions"
      echo="false"
      delimiter=" "
    />
    <!-- Access session attribute <em>Hello</em> from first session.
    -->
    <jmx:invoke
      name="Catalina:type=Manager,context=/ClusterTest,host=localhost"
      operation="getSessionAttribute"
      resultproperty="Hello"
      echo="false"
    >
      <arg value="${sessions.0}"/>
      <arg value="Hello"/>
    </jmx:invoke>
    <!-- Query for all application manager.of the server from all hosts
       and bind all attributes from all found manager MBeans.
    -->
    <jmx:query
      name="Catalina:type=Manager,*"
      resultproperty="manager"
      echo="true"
      attributebinding="true"
    />
    <!-- echo the create properties -->
<echo>
senderObjectNames: ${senderObjectNames.0}
IDataSender.backup.connected: ${IDataSender.backup.connected}
session: ${sessions.0}
manager.length: ${manager.length}
manager.0.name: ${manager.0.name}
manager.1.name: ${manager.1.name}
hello: ${Hello}
manager.ClusterTest.0.name: ${manager.ClusterTest.0.name}
manager.ClusterTest.0.activeSessions: ${manager.ClusterTest.0.activeSessions}
manager.ClusterTest.0.counterSend_EVT_SESSION_EXPIRED:
 ${manager.ClusterTest.0.counterSend_EVT_SESSION_EXPIRED}
manager.ClusterTest.0.counterSend_EVT_GET_ALL_SESSIONS:
 ${manager.ClusterTest.0.counterSend_EVT_GET_ALL_SESSIONS}
</echo>

  </target>

</project>