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 ...
随机推荐
- some requirement checks failed
1.执行安装数据库软件时报错(./runInstaller): 解决:(1)su - root 执行: x host+ 然后 su - oracle 执行:./runIstal ...
- [.NET大牛之路 001] .NET 其名
本文来自『.NET大牛之路』星球的分享 大家好,这是 .NET 大牛这路的第 1 篇文章.大家期待已久的课程今天正式开始了.既然我们整个体系课程都将围绕 .NET 展开,那我们今天就先聊一聊 .NET ...
- ClickHouse学习系列之五【系统库system说明】
背景 之前介绍过ClickHouse相关的系列文章,现在ClickHouse已经能正常使用起来了,包括副本和分片.因为ClickHouse已经可以提供服务了,现在需要关心的就是服务期间该数据库的各项性 ...
- DHCP的简单介绍与配置
一.DHCP简介 二.DHCP报文类型 三.DHCP工作原理 四.实例操作 一.DHCP简介 DHCP(Dynamic Host Configuration Protocol),动态主机配置协议,是一 ...
- 关于基于Nexus3和Docker搭建私有Nuget服务的探索
背景简介 NuGet是Microsoft开发平台的程序集包管理器,它由客户端工具和服务端站点组成,客户端工具提供给用户管理和安装/卸载软件程序包,以及打包和发布程序包到NuGet服务端站点等功能,服务 ...
- 流程自动化RPA,Power Automate Desktop系列 - 发布文档中心
一.背景 内网中有一个基于VuePress搭建的静态文档中心,但是每次修改后都需要重新Build一次才行,之前都是手动执行命令,现在可以基于Power Automate Desktop来创建任务了. ...
- 源码搭建Zabbix4.0.23LTS监控系统
实验环境 centos 7.5 主机名 IP地址 配置 应用 controlnode 172.16.1.120/24 4核/8G/60G java-1.8.0-openjdk zabbix serve ...
- 友华新光猫PT924G破解telnet之路
最近去找电信要了个新的千兆光猫(电信宽带300兆配100兆光猫真鸡贼),背后一看不是华为了,是友华PT924G,在http://192.168.1.1:8080/里看到了熟悉的电信界面 用teleco ...
- 导入项目发现没得右边没得maven
使用ctrl + shift+A点Add Maven Project 就行了 参考:https://www.cnblogs.com/Juff-code/p/13390356.html
- 面试题二:JVM
JVM垃圾回收的时候如何确定垃圾? 有2种方式: 引用计数 每个对象都有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1,计数为0时可以回收: 缺点:无法解决对象循环引用的问题: 可达性分 ...