可以运用jdk工具监控java应用性能,再配合 jmeter 进行了一个长时间的加压,在加压过程中重点关注了系统资源的使用情况
D:Program Files (x86)Javajdk1.8.0_111in
这个目录下的工具
jps - Lists the instrumented Java Virtual Machines (JVMs) on the target system
jarsigner - Signs and verifies Java Archive (JAR) files.
=================================
jconsole
本地进程
远程进程
=================================
java mission control
jmc
=================================
jvisualVM
本地
远程
jstatd连接
jmx连接
两种地址
1、ip:port
2、jmx service url (service:jmx:rmi:///jndi/rmi://192.168.3.15:9999/jmxrmi)
service:jmx:rmi:///jndi/rmi://localhost:7091/jmxrmi
service:jmx:rmi:///jndi/rmi://<hostname>/jmxrmi
另外我们还可以为VisualVM安装一些插件,比如GC,MBeans等等。满足自己的需要。
=================================
jstatd
jstatd是一个rmi的server应用,用于监控jvm的创建和结束,并且提供接口让监控工具可以远程连接到本机的jvm 。jstatd位于 $JAVA_HOME/bin目录下
jstatd是一个监控 JVM 从创建到销毁过程中资源占用情况并提供远程监控接口的 RMI ( Remote Method Invocation ,远程方法调用)服务器程序,它是一个 Daemon 程序,要保证远程监控软件连接到本地的话需要 jstatd 始终保持运行。
jmx
JMX:Java Management Extensions ,即 Java 管理扩展 , 是一个为应用程序、设备、系统等植入管理功能的框架。 JMX 可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用 。而Visual VM是通过 JMX 来和远程 Java 应用联系的
=================================
在需要被监控的服务器上面,通过jstatd来启动RMI服务
新建文件,编辑内容,启动守护,再visualVM添加jstatd连接
[root@localhost bin]# pwd
/root/jdk1.8.0_144/bin
jstatd运行需要通过 -J-Djava.security.policy=*** 指定安全策略,因此我们需要在服务器上建立一个指定安全策略的文件jstatd.all.policy ,文件内容如下:
[root@localhost bin]# cat jstatd.all.policy
grant codebase "file:${java.home}/../lib/tools.jar" {
permission java.security.AllPermission;
};
grant codebase "file:${java.home}/../lib/tools.jar" { permission java.security.AllPermission; };
上面内容格式不正确,可能导致不能正常连接
看man jstatd,里面都有解释
-Joption Passes option to the JVM, where option is one of the options described on the reference page for the Java application launcher. For example, -J- Xms48m sets the startup memory to 48 MB. See java(1).
[root@localhost bin]# ./jstatd -J-Djava.security.policy=jstatd.all.policy
默认就是1099
jstatd -J-Djava.security.policy=/root/jstatd.policy -p 2099
要使Java VisualVM 成功连接到远程服务器上,服务器端应该在 /etc/hosts 文件中把本机地址设为本机的 IP 地址。使用 hostname -i 命令查看,如果显示的是 127.0.0.1 或者与本机实际 IP 不一致的话,需要把 /etc/hosts 文件中相应的地址改为本机实际 IP 。
[root@localhost ~]# jstatd -J-Djava.security.policy=/root/jdk1.8.0_144/bin/jstatd.all.policy -J-Djava.rmi.server.logCalls=true
Jun 17, 2019 2:43:02 PM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(1)-127.0.0.1: [127.0.0.1: sun.rmi.registry.RegistryImpl[0:0:0, 0]: void rebind(java.lang.String, java.rmi.Remote)]
Jun 17, 2019 2:43:02 PM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(2)-127.0.0.1: [127.0.0.1: sun.rmi.transport.DGCImpl[0:0:0, 2]: java.rmi.dgc.Lease dirty(java.rmi.server.ObjID[], long, java.rmi.dgc.Lease)]
Jun 17, 2019 2:43:02 PM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(3)-192.168.3.169: [192.168.3.169: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
Jun 17, 2019 2:43:07 PM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(3)-192.168.3.169: [192.168.3.169: sun.rmi.registry.RegistryImpl[0:0:0, 0]: java.rmi.Remote lookup(java.lang.String)]
Jun 17, 2019 2:43:11 PM sun.rmi.server.UnicastServerRef logCall
上面是没有改hosts文件时的console输出
下面是改后的console输出
FINER: RMI TCP Connection(4)-192.168.3.169: [192.168.3.169: sun.tools.jstatd.RemoteVmImpl[-6199bad7:16b6432834d:-7ff8, 7381047654591843568]: public abstract byte[] sun.jvmstat.monitor.remote.RemoteVm.getBytes() throws java.rmi.RemoteException]
Jun 17, 2019 2:51:01 PM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(4)-192.168.3.169: [192.168.3.169: sun.tools.jstatd.RemoteVmImpl[-6199bad7:16b6432834d:-7ff2, 5427747741497180388]: public abstract byte[] sun.jvmstat.monitor.remote.RemoteVm.getBytes() throws java.rmi.RemoteException]
Jun 17, 2019 2:51:01 PM sun.rmi.server.UnicastServerRef logCall
FINER: RMI TCP Connection(4)-192.168.3.169: [192.168.3.169: sun.tools.jstatd.RemoteHostImpl[-6199bad7:16b6432834d:-7fff, 8336527158749228898]: public abstract int[] sun.jvmstat.monitor.remote.RemoteHost.activeVms() throws java.rmi.RemoteException,sun.jvmstat.monitor.MonitorException]
Jun 17, 2019 2:51:01 PM sun.rmi.server.UnicastServerRef logCall
=================================
jps
pid java应用主类名称
[root@test1 ~]# jps
5729 Bootstrap
5782 Jstatd
PID: 5782
Host: 192.168.3.15
Main class: sun.tools.jstatd.Jstatd
PID: 5729
Host: 192.168.3.15
Main class: org.apache.catalina.startup.Bootstrap
[root@test1 bin]# jps 5729 Bootstrap 6009 Jps [root@test1 bin]# jps -q 5729 6024 [root@test1 bin]# jps -l 5729 org.apache.catalina.startup.Bootstrap 6039 sun.tools.jps.Jps [root@test1 bin]# jps -m 5729 Bootstrap start 5994 Jps -m [root@test1 bin]# jps -lm 5729 org.apache.catalina.startup.Bootstrap start 6054 sun.tools.jps.Jps -lm [root@test1 bin]# jps -v 5729 Bootstrap -Djava.util.logging.config.file=/root/apache-tomcat-8.5.20/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=192.168.3.15 -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcatalina.base=/root/apache-tomcat-8.5.20 -Dcatalina.home=/root/apache-tomcat-8.5.20 -Djava.io.tmpdir=/root/apache-tomcat-8.5.20/temp 6069 Jps -Dapplication.home=/root/jdk1.8.0_144 -Xms8m [root@test1 bin]# jps -lvm 5729 org.apache.catalina.startup.Bootstrap start -Djava.util.logging.config.file=/root/apache-tomcat-8.5.20/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=192.168.3.15 -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcatalina.base=/root/apache-tomcat-8.5.20 -Dcatalina.home=/root/apache-tomcat-8.5.20 -Djava.io.tmpdir=/root/apache-tomcat-8.5.20/temp 6084 sun.tools.jps.Jps -lvm -Dapplication.home=/root/jdk1.8.0_144 -Xms8m
=================================
为tomcat 配置 jvm 启动参数,以便监控
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.3.225
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"
================================== [root@test1 bin]# jps 5729 Bootstrap 6331 Jps 得到pid jstat - Monitors Java Virtual Machine (JVM) statistics. [root@test1 bin]# jstat -help Usage: jstat -help|-options jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]] Definitions: <option> An option reported by the -options option <vmid> Virtual Machine Identifier. A vmid takes the following form: <lvmid>[@<hostname>[:<port>]] Where <lvmid> is the local vm identifier for the target Java virtual machine, typically a process id; <hostname> is the name of the host running the target Java virtual machine; and <port> is the port number for the rmiregistry on the target host. See the jvmstat documentation for a more complete description of the Virtual Machine Identifier. <lines> Number of samples between header lines. <interval> Sampling interval. The following forms are allowed: <n>["ms"|"s"] Where <n> is an integer and the suffix specifies the units as milliseconds("ms") or seconds("s"). The default units are "ms". <count> Number of samples to take before terminating. -J<flag> Pass <flag> directly to the runtime system. [root@test1 bin]# jstat -options -class -compiler -gc -gccapacity -gccause -gcmetacapacity -gcnew -gcnewcapacity -gcold -gcoldcapacity -gcutil -printcompilation 得到options [root@test1 bin]# jstat -compiler 5729 Compiled Failed Invalid Time FailedType FailedMethod 3166 0 0 9.06 0 [root@test1 bin]# jstat -gc 5729 S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 18432.0 20480.0 480.0 0.0 103936.0 12717.6 142336.0 11663.3 20736.0 19683.0 2560.0 2207.3 14 0.178 1 0.033 0.211 [root@test1 bin]# jstat -gcutil 5729 S0 S1 E O M CCS YGC YGCT FGC FGCT GCT 2.60 0.00 12.27 8.19 94.92 86.22 14 0.178 1 0.033 0.211 ============================================ jstack - Prints Java thread stack traces for a Java process, core file, or remote debug server. [root@test1 bin]# jstack Usage: jstack [-l] <pid> (to connect to running process) jstack -F [-m] [-l] <pid> (to connect to a hung process) jstack [-m] [-l] <executable> <core> (to connect to a core file) jstack [-m] [-l] [server_id@]<remote server IP or hostname> (to connect to a remote debug server) Options: -F to force a thread dump. Use when jstack <pid> does not respond (process is hung) -m to print both java and native frames (mixed mode) -l long listing. Prints additional information about locks -h or -help to print this help message [root@test1 bin]# jstack -l 5729 2019-06-17 16:41:51 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.144-b01 mixed mode): "Attach Listener" #98 daemon prio=9 os_prio=0 tid=0x00007f5f3c013800 nid=0x1928 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None