標籤:
1) 正常安裝JDK和Tomcat 配置環境變數(此步略過)
2) 配置JMX訪問檔案
# cd $CATALINA_HOME/conf/
# cp $JAVA_HOME/jre/lib/management/jmxremote.password.template jmxremote.password
# chmod 700 jmxremote.password (一定注意這個檔案預設是不可寫的)
# vi $CATALINA_HOME/conf/jmxremote.password
在最下面添加一個使用者(第一個是使用者名稱 第二個是密碼)
fox 123456
# chmod 400 jmxremote.password (要求該檔案是任何使用者均不可寫的)
# cp $JAVA_HOME/jre/lib/management/jmxremote.access jmxremote.access
# vi jmxremote.access (該檔案是對jmx使用者的授權)
到最後一行 將
monitorRole readonly
controlRole readwrite
注釋 更改為
# monitorRole readonly
# controlRole readwrite
然後在最後添加一行
fox readwrite
# chmod 400 jmxremote.access
3) 配置Tomcat啟動參數
# cd $CATALINA_HOME/bin
# vi catalina.sh
找到以下一行
# ----- Execute The Requested Command -----------------------------------------
在其上方添加一段指令碼 加在最後的目的是要沿用之前的JAVA_OPTS和CATALINA_HOME變數設定(特別是堆記憶體參數設定) 網上文章說法有直接設定JAVA_OPTS的 不過這樣設定在stop的時候會有異常拋出 雖然不影響使用
(參數意義依次是:
-Dcom.sun.management.jmxremote 啟用JMX遠程監控
-Dcom.sun.management.jmxremote.port=9004 使用連接埠9004(可隨意指定只要不衝突)
-Dcom.sun.management.jmxremote.authenticate=true 遠端連線要求輸入密碼認證
-Dcom.sun.management.jmxremote.ssl=false 不使用SSL(SSL我試了多次都沒有配置成功:P 暫時不開啟)
-Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access 使用指定的JMX帳號授權檔案
-Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password 使用指定的JMX帳號檔案)
# ----- JMX Config Start -----
if [ "$1" = "run" ]; then
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password"
elif [ "$1" = "start" ]; then
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password"
fi
# ----- JMX Config End -----
也就是說需要在tomcat的bin下需要在catalina.sh下加入如下代碼即可。
# ----- JMX Config Start -----
if [ "$1" = "run" ]; then
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.password"
elif [ "$1" = "start" ]; then
JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10207 -Dcom.sun.management.jmxremote.authenticate=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.access.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.access -Dcom.sun.management.jmxremote.password.file=/usr/java/jdk1.6.0_32/jre/lib/management/jmxremote.password"
fi
# ----- JMX Config End -----
export JAVA_OPTS (重要)
然後我們要做的就是進入的/usr/java/jdk1.6.0_32/jre/lib/management下
有個模板,拷貝一份作為jmxremote.password,切記,這個檔案許可權比較特殊,我用的時候用的400
記得jmsremote.password最後
monitorRole QED
controlRole R&D
前的注釋去掉(系統內建的帳號和密碼,建議自己新增一個,新增的時候記得去jmxremote.access中付給帳號讀寫權限)
然後就可以了。
再就是注意一下hostname那部分,應該就直接可以在用戶端輸入jconsole然後輸入IP地址,連接埠,帳號,密碼,你就可以監控tomcat了
再次感謝@逝水fox大神
vi /etc/hosts 修改主機名稱為外網IP地址,這個很重要,要不然串連不上...
WIndows訪問Linux伺服器Tomcat:使用JDK內建jvisualvm監控tomcat
LINUX配置JMX監控tomcat7