Tomcat 性能监控与优化
JMX
JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架。JMX是一套标准的 代理和服务,实际上,用户可以在任何Java应用程序中使用这些代理和服务实现管理。可以 利用JDK的JConsole来访问Tomcat JMX接口实施监控,具体步骤如下: 首先,打开tomcat5的bin目录中的catalina.bat文件,在头部注释部分的后面加上: set JAVA_OPTS=%JAVA_OPTS% ‐Dcom.sun.management.jmxremote.port=8999 ‐Dcom.sun.management.jmxremote.authenticate=false ‐Dcom.sun.management.jmxremote.ssl=false 如果已经配置好,则可使用 JConsole 打开监控平台查看 Tomcat 性能。
Linux 下配置 catalina.sh 的例子: JAVA_OPTS='-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=192.168.0.106'
由于 JMX 提供的接口是任何 Java 程序都可以调用访问的,因此我们可以编写 JAVA 程序来收 集 Tomcat 性能数据,具体代码如下所示: import java.lang.management.MemoryUsage; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Formatter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo; import javax.management.MBeanServerConnection; import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.management.openmbean.CompositeDataSupport; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL;
public class MonitorTomcat { /** * @param args */ public static void main(String[] args) { try { String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.0.106:8999/jmxrmi";//tomcat jmx url JMXServiceURL serviceURL = new JMXServiceURL(jmxURL); Map map = new HashMap(); String[] credentials = new String[] { "monitorRole" , "QED" }; map.put("jmx.remote.credentials", credentials); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map); MBeanServerConnection mbsc = connector.getMBeanServerConnection();
ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080"); MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName); String attrName = "currentThreadCount"; //tomcat 的线程数对应的属性值 MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes(); System.out.println("currentThreadCount:"+mbsc.getAttribute(threadObjName, attrName)); //heap for(int j=0;j <mbsc.getDomains().length;j++){ System.out.println("###########"+mbsc.getDomains()[j]); } Set MBeanset = mbsc.queryMBeans(null, null); System.out.println("MBeanset.size() : " + MBeanset.size()); Iterator MBeansetIterator = MBeanset.iterator(); while (MBeansetIterator.hasNext()) { ObjectInstance objectInstance = (ObjectInstance)MBeansetIterator.next(); ObjectName objectName = objectInstance.getObjectName(); String canonicalName = objectName.getCanonicalName(); System.out.println("canonicalName : " + canonicalName); if (canonicalName.equals("Catalina:host=localhost,type=Cluster")) { // Get details of cluster MBeans System.out.println("Cluster MBeans Details:"); System.out.println("=========================================");
//getMBeansDetails(canonicalName); String canonicalKeyPropList = objectName.getCanonicalKeyPropertyListString(); } } //‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ system ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime"); System.out.println("厂商:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVendor")); System.out.println("程序:"+ (String)mbsc.getAttribute(runtimeObjName, "VmName")); System.out.println("版本:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVersion")); Date starttime=new Date((Long)mbsc.getAttribute(runtimeObjName, "StartTime")); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("启动时间:"+df.format(starttime)); Long timespan=(Long)mbsc.getAttribute(runtimeObjName, "Uptime"); System.out.println("连续工作时间:"+MonitorTomcat.formatTimeSpan(timespan)); //-------------------------JVM--------------------------------- //堆使用率 ObjectName heapObjName = new ObjectName("java.lang:type=Memory");
MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "HeapMemoryUsage")); long maxMemory = heapMemoryUsage.getMax(); //堆最大 long commitMemory = heapMemoryUsage.getCommitted(); //堆当前分配 long usedMemory = heapMemoryUsage.getUsed(); System.out.println("heap:"+(double)usedMemory*100/commitMemory+"%"); // 堆使用率
MemoryUsage nonheapMemoryUsage = MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "NonHeapMemoryUsage")); long noncommitMemory = nonheapMemoryUsage.getCommitted(); long nonusedMemory = heapMemoryUsage.getUsed(); System.out.println("nonheap:"+(double)nonusedMemory*100/noncommitMemory+"%"); ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");
MemoryUsage permGenUsage = MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(permObjName, "Usage")); long committed = permGenUsage.getCommitted();//持久堆大小 long used = heapMemoryUsage.getUsed();// System.out.println("perm gen:"+(double)used*100/committed+"%");//持久堆使用率 //‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Session ‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ObjectName managerObjName = new ObjectName("Catalina:type=Manager,*");
PrefTest 性能测试工作室 - http://cnblogs.com/preftest 7
PrefTest 性能测试工作室 - http://cnblogs.com/preftest
Set<ObjectName> s=mbsc.queryNames(managerObjName, null); for (ObjectName obj:s){ System.out.println("应用名:"+obj.getKeyProperty("path")); ObjectName objname=new ObjectName(obj.getCanonicalName()); System.out.println(" 最大会话数:"+ mbsc.getAttribute( objname, "maxActiveSessions")); System.out.println("会话数:"+ mbsc.getAttribute( objname, "activeSessions")); System.out.println("活动会话数:"+ mbsc.getAttribute( objname, "sessionCounter")); } //‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ Thread Pool ‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ObjectName threadpoolObjName = new ObjectName("Catalina:type=ThreadPool,*"); Set<ObjectName> s2=mbsc.queryNames(threadpoolObjName, null); for (ObjectName obj:s2){ System.out.println("端口名:"+obj.getKeyProperty("name")); ObjectName objname=new ObjectName(obj.getCanonicalName()); System.out.println("最大线程数:"+ mbsc.getAttribute( objname, "maxThreads")); System.out.println(" 当前线程数:"+ mbsc.getAttribute( objname, "currentThreadCount")); System.out.println(" 繁忙线程数:"+ mbsc.getAttribute( objname, "currentThreadsBusy")); } } catch (Exception e) { e.printStackTrace(); } }
public static String formatTimeSpan(long span){ long minseconds = span % 1000; span = span /1000; long seconds = span % 60; span = span / 60; long mins = span % 60; span = span / 60; long hours = span % 24; span = span / 24; long days = span; return (new Formatter()).format("%1$d 天 %2$02d:%3$02d:%4$02d.%5$03d", days,hours,mins,seconds,minseconds).toString(); } }
PrefTest 性能测试工作室 - http://cnblogs.com/preftest 8
PrefTest 性能测试工作室 - http://cnblogs.com/preftest
内存使用调整(Out of Memery 问题)
Tomcat 默认可以使用的内存为 128MB ,在较大型的应用项目中,这点内存是不够的,需 要调大。
Windows 下,在文件 {tomcat_home}/bin/catalina.bat , Unix 下,在文件
{tomcat_home}/bin/catalina.sh 的前面,增加如下设置: JAVA_OPTS='-Xms【初始化内存大小】 -Xmx【可以使用的最大内存】 '
需要把这个两个参数值调大。例如: rem ----- Execute The Requested Command --------------------------------------- set JAVA_OPTS='-Xms256m -Xmx512m'
表示初始化内存为 256MB ,可以使用的最大内存为 512MB
可用 JDK 附带的 Jconsole 查看 tomcat 的内存使用情况。
连接线程数调整(cannot connect to server 问题)
优化 tomcat 配置:maxThreads="500" minSpareThreads="400" maxSpareThreads="450"。
maxThreads This is the maximum number of threads allowed. This defines the upper bound to the concurrency, as Tomcat will not create any more threads than this. If there are more than maxThreads requests, they will be queued until the number of threads decreases. Increasing maxThreads increases the capability of Tomcat to handle more connections concurrently. However,threads use up system resources. Thus, setting a very high value might degrade performance, and could even cause Tomcat to crash. It is better to deny some incoming connections, rather that affect the ones that are being currently serviced.
maxSpareThreads This is the maximum number of idle threads allowed. Any excess idle threads are shut down by Tomcat. Setting this to a large value is not good for performance; the default (50) usually works for most Web sites with an average load. The value of maxSpareThreads should be greater than minSpareThreads, but less than maxThreads.
minSpareThreads This is the minimum number of idle threads allowed. On Tomcat startup, this is also the number of threads created when the Connector is initialized.If the number of idle threads falls below minSpareThreads, Tomcat creates new threads. Setting this to a large value is not good for performance, as each thread uses up resources. The default (4) usually works for most Web sites with an average load. Typically, sites with “bursty” traffic would need higher values for
minSpareThreads.
Tomcat6 使用默认的配置,在进行压力测试时,老是报错,用 Jmeter 模拟 40 个用户并发时, 正常,当超过 60 个时,居然全部报错,打开 server.xml 可以看到如下配置: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> Tomcat 的官方网站的解释如下, maxThread 如果没有 set,默认值为 200. The maximum number of request processing threads to be created by this Connector , which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. 这个就产生问题了,打开 Tomcat 的源码发现默认值是 40,而不是 200,所以压力测试时遇 到同类的问题,大家一定要注意,添加以下配置: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxThreads="150"/>
Tomcat 性能监控与优化的更多相关文章
- 性能调优之Java系统级性能监控及优化
性能调优之Java系统级性能监控及优化 对于性能调优而言,通常我们需要经过以下三个步骤:1,性能监控:2,性能剖析:3,性能调优 性能调优:通过分析影响Application性能问题根源,进行优化 ...
- Linux指令--性能监控和优化命令相关指令
原文出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html.感谢作者无私分享 性能监控和优化命令相关指令有:top,free,v ...
- Tomcat性能监控
Tomcat性能监控工具很多,这里介绍两种1.JMeter 2.probe,使用这两种工具都需要在tomcat的安装目录/conf/tomcat-users.xml添加 <tomcat-user ...
- Tomcat 性能监控及调优
1.性能监控 方式1: /usr/local/tomcat7/conf/tomcat-users.xml 添加如下: <role rolename="manager-gui" ...
- Tomcat性能监控之Probe
目前采用java进行开发的系统居多,这些系统运行在java容器中,通过对容器的监控可以了解到java进程的运行状况,分析java程序问题.目前市面上流行的中间件有很多(Tomcat.jetty.jbo ...
- ASP.NET 性能监控和优化入门
关键要点: 只有与应用指标相关联,基础设施指标才能最大发挥作用. 高效性能优化的关键在于性能数据. 一些APM工具为ASP.NET提供了开箱即用的支持,这样入门使用ASP.NET仅需最小限度的初始设置 ...
- Java Tomcat7性能监控与优化详解
1. 目的 通过优化tomcat提高网站的并发能力. 2. 服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力有决定性影响. 3. 优化配置 3.1. 配置tomcat管理员账户 ...
- 1、Tomcat7性能监控与优化
1. 目的 通过优化tomcat提高网站的并发能力. 2. 服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力有决定性影响. 3. 优化配置 3.1. 配置tomcat管理员账户 ...
- JVM性能监控与优化笔记(CPU)
基础 对于CPU层面的监控主要以下几个点: 是否系统态CPU的占用率高 CPU运行队列中待运行的任务数 是否CPU停滞多,每时钟指令数(IPC)少(高级点,对于计算密集型的应用需要关注) 系统态CPU ...
随机推荐
- 「模拟8.23」one递推,约瑟夫
前置芝士约瑟夫问题 这样大概就是板子问题了 考场的树状数组+二分的60分暴力??? 1 #include<bits/stdc++.h> 2 #define int long long 3 ...
- Netty 框架学习 —— 基于 Netty 的 HTTP/HTTPS 应用程序
通过 SSL/TLS 保护应用程序 SSL 和 TLS 安全协议层叠在其他协议之上,用以实现数据安全.为了支持 SSL/TLS,Java 提供了 javax.net.ssl 包,它的 SSLConte ...
- 【NLP学习其三】在学习什么是嵌入之前,你应该了解什么是词语表征
在了解什么是嵌入(embeddings)之前,我们需要先搞清楚一个词语在NLP中是如何被表示的 注:本次不涉及任何具体算法,只是单纯对概念的理解 词汇表征 One-Hot 词汇的表示方法有很多,最有名 ...
- 一、JavaSE语言基础之关键字与标示符
1.关键字 所谓关键字指Java中被赋予了特殊含义的单词或字符,Java中常见的关键字共53个,不需要进行记忆,在写代码的过程中会逐渐接触. 2.标示符 标示符,简单来说就是名字:其最大的作用 ...
- IDEA打开文件时,关闭SonarLint自动扫描
操作步骤 1 打开 Preferences mac快捷键:command+, 2 搜索 SonarLint,取消勾选Automatically trigger analysis,保存设置
- 45、screen命令
1.screen命令介绍: 当我们在使用linux远程工具进行远程访问服务器时,进行远程访问的界面往往不能关掉,否则程序将不再运行.而且,程序 在运行的过程中,还必须时刻保证网络的通常,这些条件都很难 ...
- Redis:银河麒麟arm服务器安装redis5.0.3,配置开机自启
百度网盘下载地址 链接:https://pan.baidu.com/s/1f2ghL2-0brPt0IodjfqOqQ提取码:9al1 解压tar包 #解压tar包 tar -xvf arm-r ...
- K8S(Kubernetes)学习笔记
Kubernetes(k8s)是google提供的开源的容器集群管理系统,在Docker技术的基础上,为容器化的应用提供部署运行.资源调度.服务发现和动态伸缩等一系列完整功能,提高了大规模容器集群管理 ...
- swoole实现任务定时自动化调度详解
开发环境 环境:lnmp下进行试验 问题描述 这几天做银行对帐接口时,踩了一个坑,具体需求大致描述一下. 银行每天凌晨后,会开始准备昨天的交易流水数据,需要我们这边请求拿到. 因为他们给的是一个bas ...
- jar\war\SpringBoot加载包内外资源的方式,告别FileNotFoundException吧
工作中常常会用到文件加载,然后又经常忘记,印象不深,没有系统性研究过,从最初的war包项目到现在的springboot项目,从加载外部文件到加载自身jar包内文件,也发生了许多变化,这里开一贴,作为自 ...