一:配置环境
  本文是在测试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. 【转】memcached工作原理介绍

    FROM: http://my.oschina.net/flynewton/blog/8984 官方主页: http://memcached.org/ 面临的问题  对于高并发高访问的Web应用程序来 ...

  2. linux和windows文件名称长度限制

    Linux文件名称的长度限制是255个字符 windows下全然限定文件名称必须少于260个字符,文件夹名必须小于248个字符. linux下文件数.文件夹数.文件名称长度的各种限制 下面測试都是在没 ...

  3. Graph(2014辽宁ACM省赛)

    问题 F: Graph 时间限制: 1 Sec  内存限制: 128 MB 提交: 30  解决: 5 [cid=1073&pid=5&langmask=0" style=& ...

  4. [RxJS] AsyncSubject

    AsyncSubject emit the last value of a sequence only if the sequence completed. This value is then ca ...

  5. Android(java)学习笔记137:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器

    1.SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表, ...

  6. mapping 详解3(Meta-Fields)

    文档标识相关元数据字段 _index 当执行多索引查询时,可能需要添加特定的一些与文档有关联的索引的子句. _index 字段可以用在 term.terms 查询,聚合(aggregations)操作 ...

  7. vsftpd给root设置访问权限

    1:Linux下安装vsftpd之后,默认的配置是匿名用户可以登录,匿名帐户有两个:用户名:anonymous密码:空 用户名:ftp密码:ftp 2:如果要用匿名进行上传删除等操作需要配置其它参数. ...

  8. Linux操作系统的简单认识

    1:Linux操作系统的历史 Linux操作系统是由unix操作系统发展而来的,但是Unix是收费的系统,而Linux操作系统的免费的,开源的,所以使用比较广泛,但是它是基于命令行的,不提供图形化用户 ...

  9. lex/flex 笔记

    Lex的匹配策略: 1. 按最长匹配原则确定被选中的单词 2. 如果一个字符串能被若干正规式匹配,则先匹配排在前面的正规式. lex源程序的写法:Lex源程序必须按照Lex语言的规范来写,其核心是一组 ...

  10. nodejs中package.json文件模块依赖的版本格式

    version 完全匹配 >version 大于这个版本 >=version大于或等于这个版本 <version 小于这个版本 <=version 小于等于这个版本 ~vers ...