直入主题,需要在tomcat环境中添加jmx来监控cpu、内存和线程的运行情况,在catalina.sh中增加了配置并启动后,远程无法连接上,在防火墙和安全组上开通了端口仍然不行。
结果是注册端口是固定的,jmx还会产生一个随机端口,这就无法在防火墙上配置了,需要固定两个端口才行。
原配置,在catalina.sh中,加上:
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=10.11.1.150 -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access"
这里指定的端口1099是监听端口,数据传输端口是另外产生的,所以单独在这里配置是没有用的,因此需要调整,把命令行中的端口去掉,改为在server.xml中进行配置。
启动时可能存在找不到jmxremote.password文件的问题,把../conf/jmxremote.password改为绝对路径:$CATALINA_HOME/conf/jmxremote.password。
使用下面:
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=10.11.1.150 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access"
需要下载一个tomcat的jmx插件包:tomcat-catalina-jmx-remote.jar
下载地址:https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina-jmx-remote/6.0.53
根据自己的tomcat版本去下载相应的版本,下载完成后把jar文件复制到tomcat/lib目录下。
在server.xml中,增加一个监听器:
<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener"
rmiRegistryPortPlatform="1099" rmiServerPortPlatform="1098" />
1098和1099两个端口自行定义。
做好以上配置后,重启tomcat即可。
参考:linux tomcat 配置JVM 远程访问 - Jungle的文章 - 知乎 https://zhuanlan.zhihu.com/p/340323913