Java项目性能监控和调优工具-Javamelody .

  JavaMelody能够在运行环境监测Java或Java EE应用程序服务器。并以图表的形式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求、sql请求、jsp页面与业务接口方法(EJB3、Spring、Guice)的执行数量,平均执行时间,错误百分比等。图表可以按天,周,月,年或自定义时间段查看。 


监控效果图见本文结尾部分.


配置方式:
1.下载地址:http://code.google.com/p/javamelody/downloads/list
目前为止最新版本是1.47.0,下载地址:http://code.google.com/p/javamelody/downloads/detail?name=javamelody-1.47.0.zip&can=2&q=
2.下载后可以解压找到javamelody-1.47.0.jar和jrobin-1.5.9.1.jar,引用到项目中.

3.在web.xml中配置filter:

或者maven依赖:

<dependency>
<groupId>net.bull.javamelody</groupId>
<artifactId>javamelody-core</artifactId>
<version>1.56.0</version>
</dependency>

[html] view plaincopyprint?

  1. <filter>  

  2.         <filter-name>monitoring</filter-name>  

  3.         <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>  

  4.         <init-param>  

  5.             <param-name>log</param-name>  

  6.             <param-value>true</param-value>  

  7.         </init-param>  

  8.     </filter>  

  9.     <filter-mapping>  

  10.         <filter-name>monitoring</filter-name>  

  11.         <url-pattern>/*</url-pattern>  

  12.     </filter-mapping>  

  13.     <listener>  

  14.         <listener-class>net.bull.javamelody.SessionListener</listener-class>  

  15.     </listener>  

<filter>
		<filter-name>monitoring</filter-name>
		<filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
		<init-param>
			<param-name>log</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>monitoring</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>net.bull.javamelody.SessionListener</listener-class>
	</listener>

4.重启项目:打开链接:http://<host>:<port>/projectname/monitoring
例如:http://127.0.0.1:8080/myproj/monitoring

至此,基本配置已完成,可以进行简单的性能监控,下面配置是javamelody与struts2和spring3的集成配置:


5.javamelody和struts2集成时配置:

[html] view plaincopyprint?

  1. <package name="base" namespace="/" extends="struts-default">  

  2.         <result-types>  

  3.             <result-type name="json" class="org.apache.struts2.json.JSONResult" />  

  4.         </result-types>  

  5.         <interceptors>  

  6.             <!-- javamelody -->  

  7.             <interceptor name="monitoring" class="net.bull.javamelody.StrutsInterceptor" />  

  8.               

  9.             <interceptor-stack name="myStack">  

  10.                 <interceptor-ref name="monitoring" />  

  11.                   

  12.                 <!-- 

  13.                     struts默认的拦截器 

  14.                 -->  

  15.                 <interceptor-ref name="defaultStack" />  

  16.             </interceptor-stack>  

  17.         </interceptors>  

  18.         <default-interceptor-ref name="myStack" />  

<package name="base" namespace="/" extends="struts-default">
		<result-types>
			<result-type name="json" class="org.apache.struts2.json.JSONResult" />
		</result-types>
		<interceptors>
			<!-- javamelody -->
			<interceptor name="monitoring" class="net.bull.javamelody.StrutsInterceptor" />
			
			<interceptor-stack name="myStack">
				<interceptor-ref name="monitoring" />
				
				<!--
					struts默认的拦截器
				-->
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="myStack" />

其他struts配置文件继承base,如下所示:

[html] view plaincopyprint?

  1. <package name="example" namespace="/" extends="base">  

  2.         <action name="student_*" class="studentAction" method="{1}">  

  3.             <result name="input">/pub_error.jsp</result>  

  4.         </action>  

  5.     </package>  

<package name="example" namespace="/" extends="base">
		<action name="student_*" class="studentAction" method="{1}">
			<result name="input">/pub_error.jsp</result>
		</action>
	</package>


6.javamelody与Spring集成:
spring的配置文件中配置:

[html] view plaincopyprint?

  1. <bean id"facadeMonitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor" >  

  2.          <property name ="pointcut" >  

  3.                  <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut" >  

  4.                          <property name ="pattern" value="com.proj.*.service.*" /><!--正则表达式-->  

  5.                  </bean>  

  6.          </property>  

  7. </bean>  

<bean id= "facadeMonitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor" >
         <property name ="pointcut" >
                 <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut" >
                         <property name ="pattern" value="com.proj.*.service.*" /><!--正则表达式-->
                 </bean>
         </property>
</bean>

然后配置:
方式一,web.xml中加入如下配置:
<context-param>
     <param-name> contextConfigLocation</param-name>
             <param-value>
                  classpath:applicationContext*.xml<!--Spring配置文件-->
                  classpath:net/bull/javamelody/monitoring-spring.xml<!--和spring集成时添加-->
             </param-value>
</context-param>


方式二,在spring.xml中加入如下配置:
<import resource="classpath:net/bull/javamelody/monitoring-spring.xml" />


7.如果你的所有spring管理的bean都实现了接口,用步骤6中的配置一切OK,但如果有些bean没有实现接口,使用步骤6中的配置将会出错.
则将步骤6中的配置改为(这样将使用cglib代理):
spring配置文件中配置:
<bean id= "facadeMonitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor" >
         <property name ="pointcut" >
                 <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut" >
                         <property name ="pattern" value="com.proj.*.service.*" /><!--正则表达式-->
                 </bean>
         </property>
</bean>


方式一,web.xml中加入如下配置:
<context-param>
<param-name> contextConfigLocation</param-name>
             <param-value>
                  classpath:applicationContext*.xml<!--Spring配置文件-->
                  classpath:net/bull/javamelody/monitoring-spring-aspectj.xml<!--和spring集成时添加-->
             </param-value>
</context-param>


方式二,在spring.xml中加入如下配置:
<import resource="classpath:net/bull/javamelody/monitoring-spring-aspectj.xml" />

8.在项目中经常这样得到泛型类:

[java] view plaincopyprint?

  1. private Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass()  

  2.             .getGenericSuperclass()).getActualTypeArguments()[0];  

private Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass()
			.getGenericSuperclass()).getActualTypeArguments()[0];

使用步骤7配置的cglib代理后会以上代码会出错,可以改为下面这种方式得到泛型类:

[java] view plaincopyprint?

  1. private Class<T> entityClass = (Class<T>) getSuperClassGenricType(this  

  2.             .getClass(), 0);  

  3. public static Class<?> getSuperClassGenricType(Class<?> clazz, int index)  

  4.             throws IndexOutOfBoundsException {  

  5.         Type genType = clazz.getGenericSuperclass();  

  6.   

  7.         if (!(genType instanceof ParameterizedType)) {  

  8.             return Object.class;  

  9.         }  

  10.   

  11.         Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  

  12.   

  13.         if (index >= params.length || index < 0) {  

  14.             return Object.class;  

  15.         }  

  16.         if (!(params[index] instanceof Class<?>)) {  

  17.             return Object.class;  

  18.         }  

  19.         return (Class<?>) params[index];  

  20.     }  

private Class<T> entityClass = (Class<T>) getSuperClassGenricType(this
			.getClass(), 0);
public static Class<?> getSuperClassGenricType(Class<?> clazz, int index)
			throws IndexOutOfBoundsException {
		Type genType = clazz.getGenericSuperclass();

		if (!(genType instanceof ParameterizedType)) {
			return Object.class;
		}

		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

		if (index >= params.length || index < 0) {
			return Object.class;
		}
		if (!(params[index] instanceof Class<?>)) {
			return Object.class;
		}
		return (Class<?>) params[index];
	}


9.javamelody缓存文件的清除:默认路径一般在tomcat_home/temp/javamelody中.

10.如果需要将结果导出为pdf,只要在项目中引用iText.jar即可自动加上导出pdf的链接.

11.javamelody与Quartz集成需要配置:

[html] view plaincopyprint?

  1. <bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  

  2.                 <property name="exposeSchedulerInRepository" value="true" />  

  3.                 ...  

  4.         </bean>  

<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
                <property name="exposeSchedulerInRepository" value="true" />
                ...
        </bean>

12.javamelody中配置可选参数:
web.xml中配置,以日志记录为例,如下配置将开启info级别的日志记录:

[html] view plaincopyprint?

  1. <filter>  

  2.                 <filter-name>monitoring</filter-name>  

  3.                 <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>  

  4.                 <init-param>  

  5.                         <param-name>log</param-name>  

  6.                         <param-value>true</param-value>  

  7.                 </init-param>  

  8.         </filter>  

<filter>
                <filter-name>monitoring</filter-name>
                <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
                <init-param>
                        <param-name>log</param-name>
                        <param-value>true</param-value>
                </init-param>
        </filter>

其他可选参数:
system-actions-enabled:默认为true,控制启动或停止system actions garbage collector, http sessions, heap dump, memory histogram, process list, jndi tree, opened jdbc connections, database.

url-exclude-pattern:可配置正则表达式,排除一些url不监控.

http-transform-pattern,sql-transform-pattern,ejb-transform-pattern, spring-transform-pattern, guice-transform-pattern, error-transform-pattern, log-transform-pattern, job-transform-pattern, jsf-transform-pattern, struts-transform-pattern and jsp-transform-pattern :可配置正则表达式,用于合并javamelody统计数据.

storage-directory: 默认为以javamelody,tomcat为例,默认存储路径为TOMCAT_HOME/temp/javamelody

monitoring-path:默认为:/monitoring,可以更改为其他地址,例如改为/mo,则监控地址改为:http://<host>:<port>/projectname/mo

no-database:默认false,配置为true将不监控和数据有关的行为.

disabled:默认false,配置为true将不再监控.


更多内容请参考:http://code.google.com/p/javamelody/wiki/UserGuide


效果图:

Java项目性能监控和调优工具-Javamelody ._第1张图片


你可能感兴趣的:(Java项目性能监控和调优工具-Javamelody .)