通过JMX可以调试或者监控运行在JVM上的应用程序,使用最多的是JConsole(所有版本JDK都自带JConsole)和VisualVM(可以通过Oracle页面下载,JDK6以上也自带JVisualVM)。

   

    1、在创建JMX监控连接之前,首先需要了解一些JBoss的基本概念:

    (1)standalonedomain模式

           JBOSS应用服务器可以运行于standalone或者domain(集群)模式下。standalone是单机模式,domain是集群模式。采用什么模式取决于实际需求,二者皆有优缺点。

    不管采用什么模式,都可以通过JMX实现远程连接,但是连接配置是不同的。

    (2)需要修改classpath参数:

    如果要创建远程JMX连接,需要通过classpath参数引入合适的类库。(本地连接不需要,但是远程连接,此步骤不能省略)

    (3)JConsole

    如果使用JConsole,则很简单,Jboss自带了JConsole的脚本文件:$JBOSS_HOME/bin/jconsole.sh,可以直接通过JConsole访问JBOSS CLI,如下:

    wKiom1USZw-ypYPaAAMEgP_j2oE781.jpg

       

       (4)VisualVM

     如果需要使用VisualVM,需要从akquinet blog下载一个封装好的脚本,这个脚本包括了JBOSS7JBOSS8必须的类库。在运行VisualVM之前需要调整VISUALVM路径,如下:

  wKiom1USaPnjkPthAAUQeq0Tvs8766.jpg


           

             也可以直接使用jdk自带的jvisualvm,在命令行执行:JAVA_HOME>jvisualvm.exe -cp:a $JBOSS_HOME\bin\client\jboss-client.jar)


          2、duang~duang~开始创建JMX监控连接

          (1)本地进程监控(适用于standalonedomain模式

     通过JConsole或者VisualVM连接本地进程,不需要配置classpath。但是如果需要使用集成了JBoss CLI的JConsole进行监控,则最好采用前面提到已封装好的jconsole.sh。

      因为在同一台主机上,所以不需要身份验证。启动JConsle或者VisualVM,从进程列表中选择需要监控的JAVA进程,如下:

     wKioL1USa8KQvXwtAAHt1w4pSv0686.jpg     wKiom1USarDhh7llAAMJ_OXXUBc057.jpg


    

    (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

     

wKioL1USbuSgEAj9AAIiD3DCR4E487.jpg  wKioL1USb1qyeE_0AAM-lcoTpuM413.jpg


    (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存在bugdomain模式不能使用这个方式,但是可以在domain.xml中进行设置(full模块中):    

<remoting-connector use-management-endpoint="false"/>

    第六步,重启应用服务器。

    第七步,创建JMX连接,录入之前创建的用户和密码,如下:

service:jmx:remoting-jmx://HOST:4447

    wKiom1UScY_jjgOeAAIoTvHUFlI878.jpg  wKiom1USca_BArAlAANmzhuD8is447.jpg


  

   (备注,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,高级信息系统项目管理师。大型企业高级开发经理,关注金融、互联网、供应链领域信息化应用)