一、問題概述
參考前一篇:
jvisualvm連接遠程應用終於成功,附踩大坑記錄!!(一:jstatd方式)
這篇主要講講jmx方式。
二、啟動前設置jmx參數
我這邊拿tomcat舉例,其余java應用只會比它更簡單,讀者可以自行嘗試下。
在tomcat的bin目錄下,創建setenv.sh(文件名不能錯,這個是tomcat提供的一個定制參數的鈎子,名字不同就找不到了)
內容如下:
#!/bin/sh JAVA_OPTS="$JAVA_OPTS -Xmx1024m -Djava.rmi.server.hostname=192.168.19.114 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9998 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
其中,
-Djava.rmi.server.hostname=192.168.19.114 ---------------- 192.168.19.114為tomcat所在機器的ip地址。
-Dcom.sun.management.jmxremote ----------------- 開啟jmx,jdk1.5之前還要手動開啟,現在已經默認開啟了,所以可以省略
-Dcom.sun.management.jmxremote.port=9998 -------------------jmx的端口
-Dcom.sun.management.jmxremote.authenticate=false ---------------- 不開啟驗證
-Dcom.sun.management.jmxremote.ssl=false ----------------------不開啟ssl通信
配置好了之后,正常啟動應用即可。接下來驗證是否可以連接。
除了上面幾個參數,oralce官方(jdk1.8版本)還公布了其他的相關參數,如下:
Property | Description | Values |
---|---|---|
com.sun.management.jmxremote |
Enables the JMX remote agent and local monitoring via a JMX connector published on a private interface used by JConsole and any other local JMX clients that use the Attach API. JConsole can use this connector if it is started by the same user as the user that started the agent. No password or access files are checked for requests coming via this connector. |
true / false. Default is true. |
com.sun.management.jmxremote. port |
Enables the JMX remote agent and creates a remote JMX connector to listen through the specified port. By default, the SSL, password, and access file properties are used for this connector. It also enables local monitoring as described for the com.sun.management.jmxremote property. |
Port number. No default. |
com.sun.management.jmxremote. registry.ssl |
Binds the RMI connector stub to an RMI registry protected by SSL. |
true / false. Default is false. |
com.sun.management.jmxremote. ssl |
Enables secure monitoring via SSL. If false, then SSL is not used. |
true / false. Default is true. |
com.sun.management.jmxremote. ssl.enabled.protocols |
A comma-delimited list of SSL/TLS protocol versions to enable. Used in conjunction with com.sun.management.jmxremote.ssl. |
Default SSL/TLS protocol version. |
com.sun.management.jmxremote. ssl.enabled.cipher.suites |
A comma-delimited list of SSL/TLS cipher suites to enable. Used in conjunction with com.sun.management.jmxremote.ssl. |
Default SSL/TLS cipher suites. |
com.sun.management.jmxremote. ssl.need.client.auth |
If this property is true and the property com.sun.management.jmxremote.ssl is also true, then client authentication will be performed. It is recommended that you set this property to true. |
true / false. Default is false. |
com.sun.management.jmxremote. authenticate |
If this property is false then JMX does not use passwords or access files: all users are allowed all access. |
true / false. Default is true. |
com.sun.management.jmxremote. password.file |
Specifies location for password file. If com.sun.management.jmxremote.authenticate is false, then this property and the password and access files are ignored. Otherwise, the password file must exist and be in the valid format. If the password file is empty or nonexistent, then no access is allowed. |
JRE_HOME/lib/management/ jmxremote.password |
com.sun.management.jmxremote. access.file |
Specifies location for the access file. If com.sun.management.jmxremote.authenticate is false, then this property and the password and access files are ignored. Otherwise, the access file must exist and be in the valid format. If the access file is empty or nonexistent, then no access is allowed. |
JRE_HOME/lib/management/ jmxremote.access |
com.sun.management.jmxremote.login.config |
Specifies the name of a Java Authentication and Authorization Service (JAAS) login configuration entry to use when the JMX agent authenticates users. When using this property to override the default login configuration, the named configuration entry must be in a file that is loaded by JAAS. In addition, the login modules specified in the configuration should use the name and password callbacks to acquire the user's credentials. For more information, see the API documentation forjavax.security.auth.callback.NameCallback and javax.security.auth.callback.PasswordCallback. |
Default login configuration is a file-based password authentication. |
鏈接如下:
https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html#gdeum
三、設置setenv.sh的可執行權限
[root@localhost bin]# chmod +x setenv.sh [root@localhost bin]# pwd /home/apache-tomcat-8.5.28/bin [root@localhost bin]# ll setenv.sh -rwxr-xr-x 1 root root 259 Mar 13 10:56 setenv.sh [root@localhost bin]# pwd /home/apache-tomcat-8.5.28/bin // 添加可執行權限 [root@localhost bin]# chmod +x setenv.sh
然后啟動startup.sh來啟動tomcat。
等待tomcat啟動后,查看9998端口是否開啟:
netstat -nltp
三、本機visualvm進行連接
參考前一篇,在本機的visualvm上,依然是失敗的。
四、換台機器試試
五、jconsole連接
更新於2020-3-13,
今天被同事問到這個,於是用jconsole也試了下,發現可以連接:
六、結論
依然是,遇到bug不要怕,換個客戶端,不行就換台機器試試