亲宝软件园·资讯

展开

JConsoler监控Tomcat JVM内存

Jiangxl~ 人气:0

通过JConsoler监控Tomcat的JVM内存

1.监控Tomcat的方式

自定义监控 只要是用命令能得到的东西都能去监控

通过jmx 对java进行监控

通过ipmi 监控硬件的

通过snmp 监控网络设备的,只要支持snmp都能监控

2.Java自带的监控命令

[root@localhost ~]# jps -lvm
7457 org.apache.catalina.startup.Bootstrap start start -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp
7525 org.apache.catalina.startup.Bootstrap start start -Djava.util.logging.config.file=/application/tomcat_8082/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -Dcatalina.base=/application/tomcat_8082 -Dcatalina.home=/application/tomcat_8082 -Djava.io.tmpdir=/application/tomcat_8082/temp
9144 sun.tools.jps.Jps -lvm -Denv.class.path=.:/application/jdk/lib:/application/jdk/jre/lib:/application/jdk/lib/tools.jar -Dapplication.home=/application/jdk1.8.0_60 -Xms8m
7482 org.apache.catalina.startup.Bootstrap start start -Djava.util.logging.config.file=/application/tomcat_8081/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -Dcatalina.base=/application/tomcat_8081 -Dcatalina.home=/application/tomcat_8081 -Djava.io.tmpdir=/application/tomcat_8081/temp
[root@localhost ~]# jmap -heap 7457
Attaching to process ID 7457, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.60-b23

using thread-local object allocation.
Mark Sweep Compact GC

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 255852544 (244.0MB)
   NewSize                  = 5570560 (5.3125MB)
   MaxNewSize               = 85262336 (81.3125MB)
   OldSize                  = 11206656 (10.6875MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:			#监控的话关注这一列
New Generation (Eden + 1 Survivor Space):
   capacity = 12779520 (12.1875MB)
   used     = 11689352 (11.147834777832031MB)
   free     = 1090168 (1.0396652221679688MB)
   91.46941356169872% used
Eden Space:				#监控的话关注这一列
   capacity = 11403264 (10.875MB)
   used     = 11059616 (10.547271728515625MB)
   free     = 343648 (0.327728271484375MB)
   96.98640669899426% used
From Space:					#监控的话关注这一列
   capacity = 1376256 (1.3125MB)
   used     = 629736 (0.6005630493164062MB)
   free     = 746520 (0.7119369506835938MB)
   45.75718470982143% used
To Space:				#监控的话关注这一列
   capacity = 1376256 (1.3125MB)
   used     = 0 (0.0MB)
   free     = 1376256 (1.3125MB)
   0.0% used
tenured generation:				#监控的话关注这一列
   capacity = 28164096 (26.859375MB)
   used     = 18313224 (17.46485137939453MB)
   free     = 9850872 (9.394523620605469MB)
   65.02329774760035% used

21146 interned Strings occupying 1882520 bytes.

[root@localhost ~]#  jstack -l 7457

3.Tomcat故障案例

系统负载高,tomcat占用CPU较高

4.配置Tomcat JMX监控

#修改bin/catalina.sh
[root@localhost tomcat]# vim bin/catalina.sh
#注意不能分行写会报错找不到命令,要写在一行上
CATALINA_OPTS="$CATALINA_OPTS"
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jxmremote.ssl=false
-Djava.rmi.server.hostname="192.168.81.210"
#正确写法
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jxmremote.ssl=false -Djava.rmi.server.hostname=192.168.81.210"


#说明
CATALINA_OPTS="$CATALINA_OPTS"		#修改tomcat启动参数
-Dcom.sun.management.jmxremote		#开启tomcat远程管理功能
-Dcom.sun.management.jmxremote.port=12345	#远程管理功能,除了12345端口,还会生成2个随机端口,我们可以开放所有端口
Dcom.sun.management.jmxremote.authenticat=false			#是否在监控时需要认证
-Dcom.sun.management.jmxremote.ssl=false	#是否开启ssl连接
-Djava.rmi.server.hostname="192.168.81.210"		#设置tomcat所在服务器的ip

配置完成后重启tomcat即可
使用ps命令可以看到输出的信息多了我们写入的几行
[root@localhost tomcat]# ps aux | grep java
root      17786  5.7 10.6 2306592 105860 pts/1  Sl   15:52   0:08 /application/jdk/bin/java
-Djava.util.logging.config.file=/application/tomcat/conf/logging.properties 
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
-Djdk.tls.ephemeralDHKeySize=2048 
-Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jxmremote.ssl=false 
-Djava.rmi.server.hostname=192.168.81.210 
-Dignore.endorsed.dirs= 
-classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar 
-Dcatalina.base=/application/tomcat 
-Dcatalina.home=/application/tomcat 
-Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start

使用ss/netstat 命令可以看到多出啦的三个端口
[root@localhost tomcat]# ss -lnptu | grep java
tcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=17786,fd=56))
#我们制定的
tcp    LISTEN     0      50       :::12345                :::*                   users:(("java",pid=17786,fd=22))
#随机的1
tcp    LISTEN     0      50       :::46272                :::*                   users:(("java",pid=17786,fd=21))
#随机的2
tcp    LISTEN     0      50       :::46081                :::*                   users:(("java",pid=17786,fd=24))
tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=17786,fd=65))

5.使用Jsconsole连接JMX查看监控数据

在这里插入图片描述

加载全部内容

相关教程
猜你喜欢
用户评论