Tomcat open JMX monitoring Detailed

Set up a simulated environment:
OS: 7 
Memory: 1G
the JDK: 1.8.0_131
Tomcat: 8.0.48
Environmental ready we can not directly demonstrate here, directly configure the tomcat jmx
    1, go to the bin directory of tomcat

      # cd /opt/tomcat/apache-tomcat-8.0.48/bin/

    2, edit the configuration file

      # vim catalina.sh

    Was added 3, the following figure above parameters

      

     Looks after adding parameters

      3.1, this configuration does not require connection jmx account password:   
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-DJava.rmi.server.hostname=192.168.100.117
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"

      After configuration screenshots:

       

    Parameter Description:

      hostname: is tomcat monitoring of the server ip address

      jmxremote.port: port number to open the monitor port number

      jmxremote.ssl: Do you want to open ssl connection

      authenticate: false representation monitoring does not require user and password

      3.2, you need to configure the user name and password:  
Copy the code
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.100.117
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.password.file=../conf/jmxremote.password
-Dcom.sun.management.jmxremote.access.file=../conf/jmxremote.access"
Copy the code

      After configuration screenshots:

       

      Parameter Description:

      authenticate: open the account verification is true

      access.file: access to the file path

      password.file: password file path

      3.3, when the password authentication is not configured, does not require this step, when the user is enabled, this operation requires password verification steps

        3.3.1, the installation directory to find jdk

          If you do not know the installation directory to find the place by the command:

            # java -verbose

            

        3.3.2, switch to the directory jdk

            

            Enter # cd jre / lib / management /

            In the file directory will jmxremote.access jmxremote.password.template file, copied to the conf directory of tomcat

              # cp jmxremote.* /opt/tomcat/apache-tomcat-8.0.48/conf/

         3.3.3, modify jmxremote.access file

            

          3.3.4, rename password file:

            # mv jmxremote.password.template jmxremote.password

        3.3.5, edit the password file

            # vim jmxremote.password

            

        3.3.6, after editing the file, to modify the access password files and file permissions, otherwise tomcat does not start up

            # chmod 600 jmxremote.*

    4, start tomcat      
Copy the code
[root@ha bin]# ./startup.sh 
Using CATALINA_BASE:   /opt/tomcat/apache-tomcat-8.0.48
Using CATALINA_HOME:   /opt/tomcat/apache-tomcat-8.0.48
Using CATALINA_TMPDIR: /opt/tomcat/apache-tomcat-8.0.48/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/tomcat/apache-tomcat-8.0.48/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.0.48/bin/tomcat-juli.jar
Tomcat started.
Copy the code

       

       4.1、做完上面操作,使用jdk自带的jvisualvm.exe连接,可以在windows下测试

        

          

          

           

           

         双击打开jmx连接

           

           

       4.2、重启tomcat,测试jmx是否还可以连接上

         # ./shutdown.sh

        4.2.1、再次连接jmx查看是否可以连接上

          数据监控图都查看不了,连接不上

          

           

 为什会出现这个问题?

    网上给出的结论是:

      tomcat的jmx远程健康是需要两个端口,一个是注册端口,一个是服务端口,之前配置指定的9999端口是注册端口,tomcat在启动的时候会随机指定一个服务端口,由于我们注册端口只有一个,所以在重启服务后,注册端口没有变化,服务端口已经变了,再次通过注册端口连接已经连接不上之前的服务端口了,没有办法指定服务端连接,服务端口是随机的。

      有一种方法是设置jmx固定的注册端口和服务端口,通过jar包实现

      

        

   5、登录tomcat官网(注意,我的tomcat版本是1.8.0的官网上只有1.8.5的也可以使用)

      网址:https://tomcat.apache.org/

      根据自己的tomcat版本找到JMX Remote jar

      

   6、把下载下来的上传到tomcta的lib目录下

     获取jar包的连接也可以直接wget到lib目录下

       # wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/extras/catalina-jmx-remote.jar

       

    7、编辑tomcat的conf目录下的server.xml文件   

      # vim server.xml

     添加下面内容:  

  <Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener"
          rmiRegistryPortPlatform="10001" rmiServerPortPlatform="10002" />

     

    8, edit the tomcat bin directory catalina.sh

     # vim catalina.sh

     Delete parameters:

       -Dcom.sun.management.jmxremote.port=9999

      

    9, restart tomcat test, jmx registration port 10001, the service port 10002

      

        

      

      

      

    10, restart tomcat connection test again

       When jmx remote we double-click the previously created connection will find also not connected, which is why, here we should note that the above pid number of connections, after inquiry, we found that the pid number is pid number linux in the tomcat, when we restart tomcat, will produce a new pid number, so we want to connect will create a new connection

       

        

    11, our new jmx connected again when it will get a new number pid

       

        

    12, here we test a success, so when we use zabbix tomcat using jmx monitoring would not have to worry about restarting tomcat jmx not connect the port

Guess you like

Origin www.linuxidc.com/Linux/2019-12/161854.htm
JMX