通過JMX可以調試或者監控運行在JVM上的應用程序,使用最多的是JConsole(所有版本JDK都自帶JConsole)和VisualVM(可以通過Oracle頁面下載,JDK6以上也自帶JVisualVM)。
1、在創建JMX監控連接之前,首先需要了解一些JBoss的基本概念:
(1)standalone和domain模式:
JBOSS應用服務器可以運行於standalone或者domain(集羣)模式下。standalone是單機模式,domain是集羣模式。採用什麼模式取決於實際需求,二者皆有優缺點。
不管採用什麼模式,都可以通過JMX實現遠程連接,但是連接配置是不同的。
(2)需要修改classpath參數:
如果要創建遠程JMX連接,需要通過classpath參數引入合適的類庫。(本地連接不需要,但是遠程連接,此步驟不能省略)
(3)JConsole
如果使用JConsole,則很簡單,Jboss自帶了JConsole的腳本文件:$JBOSS_HOME/bin/jconsole.sh,可以直接通過JConsole訪問JBOSS CLI,如下:
(4)VisualVM
如果需要使用VisualVM,需要從akquinet blog下載一個封裝好的腳本,這個腳本包括了JBOSS7和JBOSS8必須的類庫。在運行VisualVM之前需要調整VISUALVM路徑,如下:
(也可以直接使用jdk自帶的jvisualvm,在命令行執行:JAVA_HOME>jvisualvm.exe -cp:a $JBOSS_HOME\bin\client\jboss-client.jar)
2、duang~duang~開始創建JMX監控連接
(1)本地進程監控(適用於standalone和domain模式)
通過JConsole或者VisualVM連接本地進程,不需要配置classpath。但是如果需要使用集成了JBoss CLI的JConsole進行監控,則最好採用前面提到已封裝好的jconsole.sh。
因爲在同一臺主機上,所以不需要身份驗證。啓動JConsle或者VisualVM,從進程列表中選擇需要監控的JAVA進程,如下:
(2)通過密碼驗證和內置管理端口實現的遠程監控(只適用於standalone模式)
這種方式主要適用於監控程序和JBoss應用服務器位於不同主機上的情況,可以通過JBoss的內置管理端端口實現遠程連接。
第一步,確認客戶端可以訪問JBoss內置管理端口。
第二步,因爲內置管理端口默認綁定到127.0.0.1,不能用於遠程連接,所以需要所綁定的IP地址,可以修改屬性jboss.bind.address.management,如下:
$ bin/standalone.sh -Djboss.bind.address.management=IP_ADDRESS
也可以通過JBOSS CLI($JBOSS_HOME/bin/jboss-cli.sh)使之長久生效,如下:
#/interface=management/:write-attribute(name=inet-address,value=IP_ADDRESS)
雖然也可以在domain模式下調用這個命令,但是domain模式下的JMX遠程連接並不適用於內置管理端口,對於domain模式下的遠程連接,可以參看後文。
第三步,重啓以生效,管理端口默認爲9999。
第四步,創建管理用戶。爲了通過遠程主機的密碼認證,需要通過$JBOSS_HOME/bin/add-user.sh腳本創建管理用戶,如下:
$ bin/add-user.sh What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) (a): a Enter the details of the new user to add. Realm (ManagementRealm) : Username : test Password : Re-enter Password : About to add user 'test' for realm 'ManagementRealm' Is this correct yes/no? yes Added user 'test' to file '/home/jboss/standalone/configuration/mgmt-users.properties' Added user 'test' to file '/home/jboss/domain/configuration/mgmt-users.properties' Is this new user going to be used for one AS process to connect to another AS process e.g. slave domain controller? yes/no? yes To represent the user add the following to the server-identities definition <secret value="cWF6IUAjMTIz" />
第五步,創建遠程連接,錄入管理用戶和密碼,如下:
service:jmx:remoting-jmx://HOST:9999
(3)通過密碼驗證和遠程端口實現的遠程監控(適用於standalone和domain模式)
第一步,確認端口可以訪問。
第二步,修改綁定IP,因爲Jboss應用服務器默認綁定IP地址:127.0.0.1,爲了改變所綁定的IP地址,需要在啓動JBoss應用服務器的時候使用-b,如下:
$ bin/standalone.sh -b IP_ADDRESS
也可以通過通過Jboss CLI($JBOSS_HOME/bin/jboss-cli.sh)使該修改長期生效,如下:
# /interface=public/:write-attribute(name=inet-address,value=IP_ADDRESS)
第三步,重啓應用服務器,默認的遠程端口是4447,集羣應用服務器默認端口偏移量爲150,所以第二臺應用服務器的端口是4597,第三臺是4747,以此類推。
第四步,創建應用用戶。如果需要通過遠程端口訪問,需要創建應用用戶,如下:
$ bin/add-user.sh What type of user do you wish to add? a) Management User (mgmt-users.properties) b) Application User (application-users.properties) (a): b Enter the details of the new user to add. Realm (ApplicationRealm) : Username : test Password : Re-enter Password : What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[ ]: About to add user 'test' for realm 'ApplicationRealm' Is this correct yes/no? yes Added user 'test' to file '/home/goldmann/jira/TORQUE-1039-remote-jmx/jboss-as-7.1.2.Final/standalone/configuration/application-users.properties' Added user 'test' to file '/home/goldmann/jira/TORQUE-1039-remote-jmx/jboss-as-7.1.2.Final/domain/configuration/application-users.properties' Added user 'test' with roles to file '/home/goldmann/jira/TORQUE-1039-remote-jmx/jboss-as-7.1.2.Final/standalone/configuration/application-roles.properties' Added user 'test' with roles to file '/home/goldmann/jira/TORQUE-1039-remote-jmx/jboss-as-7.1.2.Final/domain/configuration/application-roles.properties' Is this new user going to be used for one AS process to connect to another AS process e.g. slave domain controller? yes/no? yes To represent the user add the following to the server-identities definition <secret value="cWF6IUAjMTIz" />
第五步,使用遠程端口訪問的方式,就不能再使用內置默認管理端口,不能在一臺主機上同時使用內置管理端口和遠程端口,所以需要關閉管理端口,standalone模式下:
#/subsystem=jmx/remoting-connector=jmx/:write-attribute(name=use-management-endpoint,value=false)
但是Jboss CLI存在bug,domain模式不能使用這個方式,但是可以在domain.xml中進行設置(full模塊中):
<remoting-connector use-management-endpoint="false"/>
第六步,重啓應用服務器。
第七步,創建JMX連接,錄入之前創建的用戶和密碼,如下:
service:jmx:remoting-jmx://HOST:4447
(備註,domain模式下要使IP長期生效,也可以在$JBOSS_HOME/domain/configuration/host.xml中修改IP地址)
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:172.25.111.8}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:172.25.111.8}"/> </interface> <interface name="unsecure"> <!-- Used for IIOP sockets in the standard configuration. To secure JacORB you need to setup SSL --> <inet-address value="${jboss.bind.address.unsecure:172.25.111.8}"/> </interface> </interfaces>
(作者:馮智傑,PMP,高級信息系統項目管理師。大型企業高級開發經理,關注金融、互聯網、供應鏈領域信息化應用)