jconsole監控遠程linux tomcat運行情況的配置 (轉)


來自:http://zhumeng8337797.blog.163.com/blog/static/100768914201242494649455/

 

步驟如下:

1.編輯tomcat/bin/catalina.sh

Bash代碼 
  1. vi catalina.sh   

 在其中“

# ----- Execute The Requested Command -----------------------------------------”

之前插入新的一行(中間沒有換行),內容如下:

Vi catalina.sh代碼 
  1. CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=jdzz10.ucjoy.com -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxrem  
  2. ote.port=12345 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=true"  

 插入后,應該如圖:



 (其中的選中部分為新增內容)

2.編輯jmxremote.access和jmxremote.password

Bash代碼 
  1. cd /usr/java/jdk1.6.0_18/jre/lib/management  
  2. mv jmxremote.password.template jmxremote.password   
  3. chmod 600 jmxremote.access jmxremote.password  
  4. vi jmxremote.password   

jmxremote.access 一般保持原始內容不變即可。

對於jmxremote.password,將 其中的:

# monitorRole  mzxwswj
# controlRole  mzxwswj

井號注釋取消掉。其中monitorRole為只擁有只讀權限的角色,controlRole有更高權限:讀寫等等。

 

3.

a.最后將端口加入防火牆信任列表

Bash代碼 
  1. vi /etc/sysconfig/iptables  

編輯iptables,加入新一行內容:-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 12345 -j ACCEPT

如圖(選中內容為新增的):

 
b.啟動tomcat

Bash代碼 
  1. /usr/local/tomcat/bin/startup.sh   

而我們在上面配置的jmx代理就會隨tomcat一起啟動。

可以使用netstat -an | grep 12345命令查看端口是否正常啟動。

 

至此,服務器端配置完畢。

4.使用windows客戶機上的jconsole連接服務器端 進行監控。

打開C:\Program Files\Java\jdk1.6.0_10\bin\jconsole.exe,輸入服務器端相關信息:


點擊“連接”,進入監控頁面:


 

 


 

1. 添加tomcat管理員帳戶
添加管理員賬戶tomcat-users.xml
< ?xml version='1.0' encoding='utf-8'?>

2. TOMCAT內存
基 本原理:JAVA程序啟動時都會JVM 都會分配一個初始內存和最大內存給這個應用程序。這個初始內存和最大內存在一定程度都會影響程序的性能。比如說在應用程序用到最大內存的時候,JVM是要 先去做垃圾回收的動作,釋放被占用的一些內存。 所以想調整Tomcat的啟動時初始內存和最大內存就需要向JVM聲明,一般的JAVA程序在運行都可以通過中-Xms -Xmx來調整應用程序的初始內存和最大內存: 如:java -Xms64m -Xmx128m application.jar.

方法1:如果是使用的tomcat_home/bin/catalina.sh(linux)或catalina.bat(win)啟動的:
修改相應文件,加上下面這句:
JAVA_OPTS='$JAVA_OPTS -server -Xmx800m -Xms512m -XX:MaxNewSize=256m -XX:MaxPermSize=256m -Djava.awt.headless=true'--ms是最小內存,mx是最大內存。這里設置最小內存為512M,最大內存為 800M。$JAVA_OPTS是保留先前設置。 CATALINA_OPTS似乎可以與JAVA_OPTS不加區別的使用。[對於catalina.bat則是如此設置: set JAVA_OPTS=-Xms1024m -Xmx1024m]

方法2:如果使用的winnt服務啟動:
在 命令行鍵入regedit,找到 HKEY_LOCAL_MACHINE-->SOFTWARE-->Apache Software Foundation-->Procrun 2.0-->Tomcat5-->Parameters的Java,
修改Options的值,新添加屬性:
-Xms64m
-Xmx128m 或者直接修改JvmMx(最大內存)和JvmMs(最小內存)。

有人建議Xms和Xmx的值取成一樣比較好,說是可以加快內存回收速度。
修改完之后,可以訪問http://127.0.0.1:8080/manager/status查看內存大小。

也可以編寫下面測試tomcat內存大小的jsp頁面:
< %
Runtime lRuntime = Runtime.getRuntime();
out.println("*** BEGIN MEMORY STATISTICS ***
");
out.println("Free Memory: "+lRuntime.freeMemory()+"
");
out.println("Max Memory: "+lRuntime.maxMemory()+"
");
out.println("Total Memory: "+lRuntime.totalMemory()+"
");
out.println("Available Processors : "+lRuntime.availableProcessors()+"
");
out.println("*** END MEMORY STATISTICS ***");
%>

3. 增加tomcat連接數
在tomcat配置文件server.xml中的配置中,和連接數相關的參數有:
minProcessors:最小空閑連接線程數,用於提高系統處理性能,默認值為10
maxProcessors:最大連接線程數,即:並發處理的最大請求數,默認值為75
acceptCount:允許的最大連接數,應大於等於maxProcessors,默認值為100
enableLookups:是否反查域名,取值為:true或false。為了提高處理能力,應設置為false
connectionTimeout: 網絡連接超時,單位:毫秒。設置為0表示永不超時,這樣設置有隱患的。通常可設置為30000毫秒。 其中和最大連接數相關的參數為maxProcessors和acceptCount。如果要加大並發連接數,應同時加大這兩個參數。 web server允許的最大連接數還受制於操作系統的內核參數設置,通常Windows是2000個左右,Linux是1000個左右。
如:

maxThreads="150"
minSpareThreads="25"
maxSpareThreads="75"
acceptCount="100"
/>
maxThreads="150" 表示最多同時處理150個連接
minSpareThreads="25" 表示即使沒有人使用也開這么多空線程等待
maxSpareThreads="75" 表示如果最多可以空75個線程,例如某時刻有80人訪問,之后沒有人訪問了,則tomcat不會保留80個空線程,而是關閉5個空的。

acceptCount="100" 當同時連接的人數達到maxThreads時,還可以接收排隊的連接,超過這個連接的則直接返回拒絕連接。

4.使用Jconsole監控
在需要監控的tomcat_home/bin/catalina.sh(linux)文件中添加下面語句:JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote.port=8089
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false"
設置一個新的端口號。然后在本機dos下輸入Jconsole命令,彈出“JAVA 監視和管理控制台”,如果是遠程監視,就輸入:, 這個port就上上面配置的8089,然后再輸入遠程主機的用戶名跟口令,就可以監控啦。 傳說這里面還可以加一句話"-Djava.rmi.server.hostname=192.168.1.80" 沒有試過 ⊙﹏⊙b汗 先拷貝上一句話,留作記憶: 再找找遠程tomcat的jmx配置,多了個參數-Djava.rmi.server.hostname=ip_or_hostname。然后我就加上了 服務器的IP。然后再連接就可以了。對這樣的情況有點不解,再官方又沒有找任何這個參數的說明。回來后,再試用一下。在虛擬機的開一個linux,然后配 上jmx參數(沒有java.rmi.server.hostname的)。結果可以連接。... 公司的為什么不能連接呢?為什么加了IP就行?懷疑多網卡。然后在虛擬機上再加一個網上,然后啟動 tomcat ,恩,不能連接(沒有server.hostname),然后再加上java.rmi.server.hostname啟動 tomcat ,結果或以連接。原來java.rmi.server.hostname是這個作用。


 

1.測試環境
服 務器:RedHat Linux 3.4.3-9.EL4(內核版本 2.6.9-5.EL),Tomcat5.5.20,Sun JDK 1.5.0_09,JProfiler 4.3.2 for linux(安裝包:jprofiler_linux_4_3_2.sh) 
客戶端:Windows XP,JProfiler 4.3.2 for windows(安裝包:jprofiler_windows_4_3_2.exe)

2.JProfiler軟件下載地址 http://www.ej-technologies.com/

3.客戶端 JProfiler 安裝 略

4.服務器端 JProfiler 安裝: 
把 jprofiler_linux_4.3.2.sh 上傳到到服務器,假設路徑為 /opt/jprofiler

# cd /opt/jprofiler
# chmod +x *.sh 
# ./jprofiler_linux_4.3.2.sh -c
按照提示來安裝,提示都很簡單,不在多說。安裝路徑選擇 /opt/jprofiler4

注意,這里的 -c 意思是用字符方式來安裝,如果機器上沒有 X 則加上該參數.

5.客戶端連接配置
1). 運行 JProfiler 。第一次打開會有向導,忽略它。 
2). 選擇 Session->Integration Wizard->New Remote Integratation 
3). 選擇 On a remote computer;Platform of remote computer 選擇 Linux x86/AMD 64;Next 
4). 輸入服務器 IP ;Next 
5). 輸入服務器上的 jprofiler 的安裝路徑,如 /opt/jprofiler4 ;next 
6). 選擇服務器的 JDK 環境,這里是:Sun,1.5.0,hotspot;next 
7). 輸入端口:這里是默認值 8849;next 
8). 選擇啟動模式:這里選第一種 wait for a connection from the jprofiler GUI;next 
9). 這里會列出需要在服務器端做的配置:

Integration type: [Generic application]
Selected JVM: Sun 1.5.0 (hotspot)
Startup mode: Wait for JProfiler GUI

(1) Please insert

-agentlib:jprofilerti=port=8849  -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar

into the start command of your remote application right after the java command.

(2) Please add

/opt/jprofiler4/bin/linux-x86

to the environment variable LD_LIBRARY_PATH.

A remote session named Remote application on 192.168.40.15 will be created that connects to a running instance of the remote application that is started with the modified start command.


6.服務器端的配置
(1)修改系統環境配置文件 /etc/profile ,增加

JPROFILER_HOME=/opt/jprofiler4/bin/linux-x86
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JPROFILER_HOME


(2)修改TOMCAT啟動文件catalina.sh,添加-agentlib:jprofilerti=port=8849  -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar 內容到CATALINA_OPTS中;
“-agentlib:jprofilerti=port=8849  -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar ” 此內容由客戶端軟件生成

CATALINA_OPTS="$CATALINA_OPTS -Xms128m -Xmx128m $JPDA_OPTS -agentlib:jprofilerti=port=8849  -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar"


7.Reboot Linux and startup Tomcat using startup.sh;
   The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:

 JProfiler> Protocol version 23
 JProfiler> Using JVMTI
 JProfiler> 32-bit library
 JProfiler> Listening on port: 8849.
 JProfiler> Native library initialized
 JProfiler> Waiting for a connection from the  JProfiler GUI 

 

8.啟動客戶端軟件
   點擊jprofiler菜單 session>start center>Open Session
   Available session configurations中列出了剛才配置的連接,選中使用就OK了!!

9.The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:

 JProfiler> Using dynamic instrumentation
 JProfiler> Time measurement: elapsed time
 JProfiler> CPU profiling enabled
 JProfiler> Hotspot compiler enabled
 JProfiler> Starting org/apache/catalina/startup/Bootstrap 

 

10.當中斷JProfiler連接時
   The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:

 JProfiler> Disconnected. Waiting for reconnection.
 JProfiler> Listening on port: 8849.

  經過我的測試發現了有以上一些問題
1、
)並不是將-agentlib:jprofilerti=port=8849  -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar 加入到CATALINA_OPTS 中而是加入到 JAVA_OPTS中,加入格式如下所示:
其余步同上

  JAVA_OPTS="$JAVA_OPTS "-agentlib:jprofilerti=port=8849 
  JAVA_OPTS="$JAVA_OPTS "-Xbootclasspath/a:/opt/jprofiler5/bin/agent.jar

下面是我采用過的一個完整的配置

if [ -r "$CATALINA_HOME"/bin/tomcat-juli.jar ]; then
  JAVA_OPTS="$JAVA_OPTS "-Xms128m 
  JAVA_OPTS="$JAVA_OPTS "-Xmx256m
  JAVA_OPTS="$JAVA_OPTS "-XX:PermSize=32m 
  JAVA_OPTS="$JAVA_OPTS "-XX:MaxNewSize=128m 
  JAVA_OPTS="$JAVA_OPTS "-XX:MaxPermSize=64m 
    JAVA_OPTS="$JAVA_OPTS "-Dcom.sun.management.jmxremote
  JAVA_OPTS="$JAVA_OPTS "-Dcom.sun.management.jmxremote.port=9009
  JAVA_OPTS="$JAVA_OPTS "-Dcom.sun.management.jmxremote.authenticate=false
  JAVA_OPTS="$JAVA_OPTS "-Dcom.sun.management.jmxremote.ssl=false
  JAVA_OPTS="$JAVA_OPTS "-agentlib:jprofilerti=port=8849  
  JAVA_OPTS="$JAVA_OPTS "-Xbootclasspath/a:/opt/jprofiler5/bin/agent.jar
  JAVA_OPTS="$JAVA_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" "-Djava.util.logging.config.file="$CATALINA_BASE/conf/logging.properties"
fi

 

當時遇到的問題是jconsole無法連接上遠程Linux上的JVM,Google看到Sun官方bugdatabase有人講是bug,就沒再理。今天再次Google,在java.sun.com/j2se/1.5.0/docs/guide/management/faq.html#linux2看到

Run "hostname -i" command. If it reports 127.0.0.1, JConsole would not be able to connect to the JVM running on that Linux machine. To fix this issue, edit /etc/hosts so that the hostname resolves to the host address.

馬上修改/etc/hosts,連接成功了。對於上面提到“在一個jsp中進行while (true);死循環”的測試,一個個線程查看,發現有一個Thread在_jspService方法上始終處於Runnable狀態。看來Programmer還是得用Google 

 

【Linux查看tomcat版本】

【Linux查看tomcat版本】

 

一、不同的tomcat查看版本可能不同,例如有的直接執行./version.sh就可以,其他就沒有;一般來說,在tomcat啟動時就會有版本信息,如:

信息: Initializing Coyote HTTP/1.1 on http-8023
2009-5-6 16:38:09 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1673 ms
2009-5-6 16:38:10 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2009-5-6 16:38:10 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.16

 

另外還可以在以下文件中找到,但據tomcat版本不一樣,文件位置稍有差異:

Tomcat5:tomcat_home\server\lib\catalina.jar org\apache\catalina\util\ServerInfo.properties

Tomcat6:tomcat_home \lib\catalina.jar org\apache\catalina\util\ServerInfo.properties (這里的兩個jar包可以下載下來用rar打開然后再查看。)

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

server.info=Apache Tomcat/6.0.16
server.number=6.0.16.0
server.built=Jan 28 2008 11:35:29

原文出自:http://dengjianqiang200.blog.163.com/blog/static/65811920094644354148/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM