转载请注明出处哈:http://carlosfu.iteye.com/blog/2237511
一、使用JMX监控和管理Ehcache
ehcache-core.jar中的net.sf.ehcache.management包提供了基于JMX标准的MBeans和ManagementService用于对ehcache进行监控和管理。
1. net.sf.ehcache.management包下类具体如下图:
2. Ehcache MBeans
CacheManager: Cache管理,可以对应多个Cache。
Cache: Cache的状态,类型等等。
CacheConfiguration: Cache的配置
CacheStatistics: Cache的统计信息
3. 使用Ehcache的jmx监控:
(1) 加入方法:
CacheManager cacheManager = CacheManager.newInstance(BaseTest.class.getClassLoader().getResourceAsStream("ehcache.xml"));
//获取MBeanServer
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
//将Ehcache的MBean注册到MBeanServer
ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true);
(2) ManagementService示意图:
因为ManagementService实现了CacheManagerEventListener接口,所以可以对Ehcache进行统计
(3) ManagementService.registerMBeans参数说明:
* @param cacheManager the CacheManager to listen to : 要监听的CacheManager
* @param mBeanServer the MBeanServer to register MBeans to: MBeans要注册的MBeanServer
* @param registerCacheManager Whether to register the CacheManager MBean: 是否注册CacheManager MBean
* @param registerCaches Whether to register the Cache MBeans: 是否注册Cache MBean
* @param registerCacheConfigurations Whether to register the CacheConfiguration MBeans: 是否注册CacheConfiguration MBeans
* @param registerCacheStatistics Whether to register the CacheStatistics MBeans: 是否注册CacheStatistics MBeans
/**
* This method causes the selected monitoring options to be be registered
* with the provided MBeanServer for caches in the given CacheManager.
*
* While registering the CacheManager enables traversal to all of the other
* items, this requires programmatic traversal. The other options allow entry
* points closer to an item of interest and are more accessible from JMX
* management tools like JConsole. Moreover CacheManager and Cache are not
* serializable, so remote monitoring is not possible for CacheManager or
* Cache, while CacheStatistics and CacheConfiguration are.
* Finally, CacheManager and Cache enable management operations to be performed.
*
* Once monitoring is enabled caches will automatically added and removed from the
* MBeanServer as they are added and disposed of from the CacheManager. When the
* CacheManager itself shutsdown all registered MBeans will be unregistered.
*
* @param cacheManager the CacheManager to listen to
* @param mBeanServer the MBeanServer to register MBeans to
* @param registerCacheManager Whether to register the CacheManager MBean
* @param registerCaches Whether to register the Cache MBeans
* @param registerCacheConfigurations Whether to register the CacheConfiguration MBeans
* @param registerCacheStatistics Whether to register the CacheStatistics MBeans
*/
public static void registerMBeans(
net.sf.ehcache.CacheManager cacheManager,
MBeanServer mBeanServer,
boolean registerCacheManager,
boolean registerCaches,
boolean registerCacheConfigurations,
boolean registerCacheStatistics) throws CacheException
4. 实际效果
使用JConsole或者VisualVm就看到对应的MBeans:(net.sf.ehcache)
5. 附赠一张jmx基本架构图:
6. JMX监控内容详细说明:
详见:BigMemroy系列文章--5. Ehcache配置和统计数据
二、Ehcache节点同步(转自:http://raychase.iteye.com/blog/1545906 第11节)
缓存数据复制方面,Ehcache允许两个地理位置各异的节点在广域网下维持数据一致性,同时它提供了这样几种方案(注:下面的示例都只绘制了两个节点的情形,实际可以推广到N个节点):
第一种方案:Terracotta Active/Mirror Replication。
这种方案下,服务端包含一个活跃节点,一个备份节点;各个应用节点全部靠该活跃节点提供读写服务。这种方式最简单,管理容易;但是,需要寄希望于理想的网络状况,服务器之间和客户端到服务器之间都存在走WAN的情况,这样的方案其实最不稳定。
第二种方案:Transactional Cache Manager Replication。
这种方案下,数据读取不需要经过WAN,写入数据时写入两份,分别由两个cache manager处理,一份在本地Server,一份到其他Server去。这种方案下读的吞吐量较高而且延迟较低;但是需要引入一个XA事务管理器,两个cache manager写两份数据导致写开销较大,而且过WAN的写延迟依然可能导致系统响应的瓶颈。
第三种方案:Messaging based (AMQ) replication。
这种方案下,引入了批量处理和队列,用以减缓WAN的瓶颈出现,同时,把处理读请求和复制逻辑从Server Array物理上就剥离开,避免了WAN情况恶化对节点读取业务的影响。这种方案要较高的吞吐量和较低的延迟,读/复制的分离保证了可以提供完备的消息分发保证、冲突处理等特性;但是它较为复杂,而且还需要一个消息总线。