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 ...
随机推荐
- 惊呆了,Spring Boot居然这么耗内存!
Spring Boot总体来说,搭建还是比较容易的,特别是Spring Cloud全家桶,简称亲民微服务,但在发展趋势中,容器化技术已经成熟,面对巨耗内存的Spring Boot,小公司表示用不起.如 ...
- 为什么代码规范要求SQL语句不要过多的join?
面试官:有操作过Linux吗? 我:有的呀 面试官:我想查看内存的使用情况该用什么命令 我:free 或者 top 面试官:那你说一下用free命令都可以看到啥信息 我:那,如下图所示 可以看到内存以 ...
- 【模拟8.10】Weed(线段树)
考试只好随便骗骗分过去啦啦啦..... 正解是玄学线段树: 以每个操作为叶子节点,我们定义几个变量ce表示层数,h表示高度,add表示所减的层数 那么问题转化为单点修改的问题输出直接是根节点答案 但是 ...
- Django(69)最好用的过滤器插件Django-filter
前言 如果需要满足前端各种筛选条件查询,我们使用drf自带的会比较麻烦,比如查询书名中包含"国"字,日期大于"2020-1-1"等等诸如此类的请求,Djan ...
- 02 SVN 与 Git 的优缺点
上一篇博客大致聊了聊关于版本控制系统的周边,这一篇我们就来继续唠唠作为近年来最受欢迎的两个版本控制系统的优缺点吧. 聊优缺点之前,先简单了解一下这两个这两个版本控制系统好了: 关于 SVN SVN 概 ...
- JavaScript(1)高阶函数filter、map、reduce
前言 需求:有这样一个数组[10, 20, 110, 200, 60, 30, 40] 1.筛选出数组中小于100的元素 2.将筛选出的每个元素的值x2 3.完成第2步之后,将数组中的所有元素加起来 ...
- 什么是DDoS引导程序IP Stresser?
1.什么是IP Stresser? IP Stresser是一款用于测试网络或服务器稳健性的工具.管理员可以运行压力测试,从而确定现有资源(带宽.CPU 等)是否足以处理附加负载. 测试个人网络或服务 ...
- Python分析【公众号】历史评论,看看大家的留言情况!
大家好,我是辰哥~~~ 辰哥玩公众号有一段时间了,这期文章分析一波读者的留言情况,不仅可以对公众号的各位铁粉一目了然,还可以通过分析的结果对公众号的经营进行更好的规划.如读者留言的内容通常是内容是什么 ...
- redis学习笔记(三)——redis的命令大全总结
总结了一些redis五种存储类型的常用命令以及一些通用操作命令,不是很全,是在学习的时候将学到的做了个汇总,使用的时候可以查一下. 笔记写在表格里面了,不好粘贴.......后面的直接截图了..... ...
- 关于mysql binlog二进制
binlog 在mysql中,当发生数据变更时,都会将变更数据的语句,通过二进制形式,存储到binlog日志文件中. 通过binlog文件,你可以查看mysql一段时间内,对数据库的所有改动. 也可以 ...