1、问题背景
为了提升应用的性能我们可能需要做很多分析和优化。但具体从哪里优化,优化的效果是多少就难以判定了。
javamelody用嵌入式的监控方式,实时监控性能指标。如:页面相应时间,spring bean方法调用时间,sql语句执行时间等很多指标。我在这就不一一列举了。
我感觉比较好的两特点是:
会自动按命中次数与相应时间结合排序,这就可以知道优化哪个页面,bean方法,sql最有效果
页面,bean,sql自动关联,可钻取,点页面可以看页面中调用bean方法的情况,点bean方法可以看sql情况
2、配置步骤
a、引入依赖包
下载javamelody的jar文件,官方站点
https://code.google.com/p/javamelody/
中找到javamelody-1.40.0.jar下载下来
手动配置时还需要JRobin这个依赖,在
http://www.jrobin.org上下载
把这两这jar文件copy到你的lib中
如果采用maven配置
只需要在pom文件中加入
net.bull.javamelody
javamelody-core
1.40.0
同样的效果
b、修改web.xml文件
加入以下代码
contextConfigLocation
classpath:org/noahx/test/application-context.xml
classpath:net/bull/javamelody/monitoring-spring.xml
monitoring
net.bull.javamelody.MonitoringFilter
monitoring
/*
net.bull.javamelody.SessionListener
spring采用org.springframework.web.context.ContextLoaderListener启动 注意filter-mapping的位置尽可能靠前,因为它来测相关请求时间
在你的spring配置文件位置中再加入classpath:net/bull/javamelody/monitoring-spring.xml这个配置
c、在spring配置的xml中加入Advisor
这样就可以监控spring bean的性能
org\.noahx\.test\..*
org\.noahx\.test\..*为正则表达式,javamelody会自动匹配bean中的类名,来决定都监控哪些bean的哪些方法
我是监控了org.noahx.test下所有定义的spring bean的所有方法
d、sql与数据源监控
如果你的dateSource定义在spring中,javamelody会自动发现不需要额外配置。
e、运行测试
程序运行起来和原来并没有变化,还是和原来一样,可以多点一点你的页面。因为javamelody需要采集访问时的性能数据。然后你可以通过javamelody的url来查看你应用的性能指标。
http://host:port/你应用的上下文/monitoring。上下文后加monitoring这种格式就可以进入主面板查看结果。