一:配置环境
  本文是在测试demo的基础上写的,服务器包括申请的两台服务器和本机,共三台服务器。demo的目标是实现三台服务器之间共享cache。
申请的两台服务器地址分别是172.19.100.150;172.19.100.151,本机是172.19.100.56。
二:配置步骤
(1)spring-mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<!-- 加载controller的时候,不加载service,因为此时事物并未生效,若此时加载了service,那么事物无法对service进行拦截 -->
<!-- 注意beans中含有cache的引用 -->
<context:component-scan base-package="*">
</context:component-scan> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcacheManager"/>
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/>
<cache:annotation-driven/>
</beans>

(2)ehcache.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache-core.xsd"
updateCheck="false" monitoring="autodetect" > <diskStore path="java.io.tmpdir" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//172.19.100.150:40001/mycache|//172.19.100.151:40001/mycache"/> <cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=172.19.100.56,port=40001" /> <defaultCache maxElementsInMemory="500" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
maxElementsOnDisk="10000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="mycache" maxElementsInMemory="500"
maxElementsOnDisk="10000" eternal="false" overflowToDisk="false"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="7200" timeToLiveSeconds="7200"
diskPersistent="false" memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true " />
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
</cache>
</ehcache>

  本例采用的是RMI方式,Ehcache的rmi方式是一种点对点的协议,因此它会产生很多局域网的内部通信,当然Ehcache会通过一种异步批处复制理机制类解决。
  这是本机中ehcache的配置,采用手工配置,其它两台服务器配置相似,只需修改相应的ip地址即可。 cacheManagerPeerProviderFactory中rmiUrls属性是对其余两台服务器的配置,每台服务器分别配置集群中其它服务器。以172.19.100.150:40001/mycache为例,172.19.100.150是其中一台服务器的ip地址,40001是端口号,mycache是集群中需要共享的cache。

(3)测试方法
     Spring为我们提供了几个注解来支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果,而使用@CacheEvict标记的方法会在方法执行前或者执行后移除Spring Cache中的某些元素。
3.1存取缓存

    @RequestMapping(params="CachePut")
@Cacheable(value="mycache",key="#value")
@ResponseBody
public String CachePut(String value){
System.out.println("value:"+value);
return value;
}

  若三台服务器中包含了相应key值的缓存,那么方法里面的输出就不会执行。
3.2清除缓存

    @RequestMapping(params="deleteCache")
@CacheEvict(value="mycache",key="#value",beforeInvocation=true)
@ResponseBody
public String deleteCache(String value){
System.out.println("delete cache key:"+value);
return "delete cache key:"+value;
}

  执行清除cache中相应key值的缓存,三台服务器中任一个再次执行3.1中方法时,CachePut中输出会被打印出来。
关于在spring中具体使用cache来写相应的测试方法,可以参考这篇博文:http://haohaoxuexi.iteye.com/blog/2123030

ehcache集群的配置的更多相关文章

  1. EHCache分布式缓存集群环境配置

    EHCache分布式缓存集群环境配置 ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms.同时ehcache可以可以实现多播的方式实现集群,也可以手动指定集群主机序列实现集群 ...

  2. [转]RMI方式Ehcache集群的源码分析

    RMI方式Ehcache集群的源码分析   Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示:   Ehcache支持 ...

  3. RMI方式Ehcache集群的源码分析

    Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示: Ehcache支持多种集群方式,下面以RMI通信方式为例,来具体分 ...

  4. 【Big Data】HADOOP集群的配置(一)

    Hadoop集群的配置(一) 摘要: hadoop集群配置系列文档,是笔者在实验室真机环境实验后整理而得.以便随后工作所需,做以知识整理,另则与博客园朋友分享实验成果,因为笔者在学习初期,也遇到不少问 ...

  5. 【Big Data】HADOOP集群的配置(二)

    Hadoop集群的配置(二) 摘要: hadoop集群配置系列文档,是笔者在实验室真机环境实验后整理而得.以便随后工作所需,做以知识整理,另则与博客园朋友分享实验成果,因为笔者在学习初期,也遇到不少问 ...

  6. CentOS下Hadoop-2.2.0集群安装配置

    对于一个刚开始学习Spark的人来说,当然首先需要把环境搭建好,再跑几个例子,目前比较流行的部署是Spark On Yarn,作为新手,我觉得有必要走一遍Hadoop的集群安装配置,而不仅仅停留在本地 ...

  7. Redis集群的配置

    [转]Redis集群的配置 一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [red ...

  8. [推荐]Hadoop+HBase+Zookeeper集群的配置

    [推荐]Hadoop+HBase+Zookeeper集群的配置 Hadoop+HBase+Zookeeper集群的配置  http://wenku.baidu.com/view/991258e881c ...

  9. hadoop集群默认配置和常用配置【转】

    转自http://www.cnblogs.com/ggjucheng/archive/2012/04/17/2454590.html 获取默认配置 配置hadoop,主要是配置core-site.xm ...

随机推荐

  1. SliderSkin

    <?xml version="1.0" encoding="utf-8"?> <s:Skin xmlns:fx="http://ns ...

  2. AdventureWorks Databases 2008 下载地址

    AdventureWorks Databases 2008 下载地址: RECOMMENDED DOWNLOAD  AdventureWorks2012_Database.zip example, 3 ...

  3. 关于Excel Networkdays方法的实现

    最近一个程序要求excel输出的日期差为Networkdays. 在网上找了下,没有找到很好的具体实现方法. 要说明的是,微软的Microsoft.Office.Interop.Excel已经实现的N ...

  4. [React Fundamentals] Accessing Child Properties

    When you're building your React components, you'll probably want to access child properties of the m ...

  5. AIR 程序开发系列 之五 保存数据的几种方式

    Local SharedObject 这种方法比较简单方便的保存少的数据到到设备中.你不用自己去管理这些数据,设备会自动管理他. SharedObject 在 flash.net 包中,继承自Even ...

  6. Qt中使用QProcess备份和恢复Mysql数据库

    分类: Qt2011-02-18 21:35 1395人阅读 评论(3) 收藏 举报 qtmysql数据库windowspathcmd . 使用Qt做MySQL数据库开发,遇到需要备份.还原数据库的问 ...

  7. debian 系统备份

    tar -zcvpf /home/full-backup.tar.gz / --exclude=/mnt/* --exclude=/proc/* --exclude=/sys/* 这个命令是把根目录下 ...

  8. java_类泛型承继方法

    package ming; class Apple3<T>{ private T info; public Apple3(){} public Apple3(T info){ this.i ...

  9. iOS简单加载一个网页

    .h文件中 @property(strong ,nonitomic) UIWebView * webView; .m文件中 -(void)viewDidLoad { self.webview = [[ ...

  10. php.ini 中开启短标签 <?=$?>

    制参数: short_open_tag = On如果设置为Off,则不能正常解析类似于这样形式的php文件:<?phpinfo()?> 而只能解析<?phpphpinfo()?> ...