ehcache学习之ehcache-monitor缓存监控

2016-01-04 15:36:25   JAVA   被浏览


这是ehcache系列的第三篇,该篇主要通过一个test case,估算ehcache会占用多大的内存。

我们线上的环境,配置表中有几十万条的记录,而之前我们从来没放这么多的数据到配置表,也没担心过ehcache是不是会被撑爆,使得jdk报类似于Out Of Memory的错误。所以在上线之前,我做了这样一个测试:一条配置大概在500B以内,我将在cache中循环添加50w条数据,通过jvisualvm来观察内存的变化。通过ehcache-monitor来看,是不是被管理到了cache中去了。

首先,来配置一下ehcache-monitor,步骤如下:
1、下载ehcache-monitor模块,ehcache-monitor-kit-1.0.3-distribution.tar
现在下载都要注册了,开发测试免费,但是监控服务器的话,每间隔三天就要重启监控应用服务器,不然无法监控。
2、解压ehcache-monitor-kit-1.0.3-distribution.tar
把ehcache-monitor-kit-1.0.3-distribution\lib\ehcache-probe-1.0.3.jar复制到要监控的应用WEB-INF\lib\下
3、修改ehcache.xml文件,添加监控配置

   <cacheManagerPeerListenerFactory
        class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
        properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true" />
4、项目需要slf4j日志包
slf4j-api-1.7.7.jar
slf4j-simple-1.7.7.jar
5、在\ehcache-monitor-kit-1.0.3\etc\ehcache-monitor.conf中可以配置要监控的服务器ip和端口号。
6、启动被监控的web application和ehcache-monitor-kit-1.0.3\bin目录下的startup.bat(在windows环境下)
7、启动被监控应用的tomcat服务器
8、在浏览器中输入 http://localhost:9889/monitor/ 即可开始监控。
如果出现连接jetty.xml超时,则注释startup.bat中的行:-j %PRGDIR%\etc\jetty.xml ^

准备的测试代码
	public OutBean testCachePerformance(ParamBean param){
		SqlBean sql = new SqlBean();
		sql.and("S_FLAG", Constant.YES_INT);
		sql.limit(5000);
		
		List<Bean> configs = ServDao.finds(ServMgr.SY_COMM_CONFIG, sql);
		for (int i=0;i<100;i++) {
			for (Bean config: configs) {
				Bean newBean = config.copyOf();
				
				String key = Lang.getUUID();
				
				CacheMgr.getInstance().set(key, newBean, "TEST_CACHE_PERF");
			}
		}
		return new OutBean().setOk();
	}
执行批量添加之前jvisualvm的状态


执行批量添加之后jvisualvm的状态


ehcache-monitor中的状态