ehcache集群的配置
一:配置环境
本文是在测试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集群的配置的更多相关文章
- EHCache分布式缓存集群环境配置
EHCache分布式缓存集群环境配置 ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms.同时ehcache可以可以实现多播的方式实现集群,也可以手动指定集群主机序列实现集群 ...
- [转]RMI方式Ehcache集群的源码分析
RMI方式Ehcache集群的源码分析 Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示: Ehcache支持 ...
- RMI方式Ehcache集群的源码分析
Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示: Ehcache支持多种集群方式,下面以RMI通信方式为例,来具体分 ...
- 【Big Data】HADOOP集群的配置(一)
Hadoop集群的配置(一) 摘要: hadoop集群配置系列文档,是笔者在实验室真机环境实验后整理而得.以便随后工作所需,做以知识整理,另则与博客园朋友分享实验成果,因为笔者在学习初期,也遇到不少问 ...
- 【Big Data】HADOOP集群的配置(二)
Hadoop集群的配置(二) 摘要: hadoop集群配置系列文档,是笔者在实验室真机环境实验后整理而得.以便随后工作所需,做以知识整理,另则与博客园朋友分享实验成果,因为笔者在学习初期,也遇到不少问 ...
- CentOS下Hadoop-2.2.0集群安装配置
对于一个刚开始学习Spark的人来说,当然首先需要把环境搭建好,再跑几个例子,目前比较流行的部署是Spark On Yarn,作为新手,我觉得有必要走一遍Hadoop的集群安装配置,而不仅仅停留在本地 ...
- Redis集群的配置
[转]Redis集群的配置 一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [red ...
- [推荐]Hadoop+HBase+Zookeeper集群的配置
[推荐]Hadoop+HBase+Zookeeper集群的配置 Hadoop+HBase+Zookeeper集群的配置 http://wenku.baidu.com/view/991258e881c ...
- hadoop集群默认配置和常用配置【转】
转自http://www.cnblogs.com/ggjucheng/archive/2012/04/17/2454590.html 获取默认配置 配置hadoop,主要是配置core-site.xm ...
随机推荐
- C#-设置label的字体颜色和大小
在进行label的设置的过程中,常常会遇到需要设定label的字体颜色和字体的大小,这就需要用到label的属性:
- powershell 查看程序的tcp网络连接
在运维工作中,常常查看某个业务的网络连接状况,在这里借用netstat来实现查找连接.用hash特性避免反复. $add=@{} while(1){ ps|?{$_.path -match 'E:\\ ...
- android Animation 动画效果介绍
Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转动 ...
- iOS开发——数据持久化Swift篇&通用文件存储
通用文件存储 import UIKit class ViewController: UIViewController { @IBOutlet weak var textField: UITextFie ...
- java使用Thumbnailator操作图片
Thumbnailator 是一个用来生成图像缩略图.裁切.旋转.添加水印等操作的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图. Thumbnailato ...
- Asp.Net 之 汉字转拼音
1.利用微软提供的拼音库,计算出汉字的拼音的方法,此方法支持多音字符 下载 Visual Studio International Pack类库,该类库扩展了.NET Framework对全球化软件开 ...
- swift和oc区别----属性部分(参考官方swift2.1文档)
对于实用过OC的人来说实用swift上手时非常容易的,swift包括了oc的大部分功能,但是swift毕竟是一门新的编程语言,它和OC还是 有很多不同的地方,而且提供了不少新功能,所以本人在读swif ...
- 全新的手势,侧滑返回、全局右滑返回都OUT啦!
前言 Android快速开发框架-ZBLibrary 最近将以前的 全局右滑返回 手势功能改成了 底部左右滑动手势. 为什么呢?为了解决滑动返回手势的问题. 目前有3种滑动返回手势 一.侧滑返回 代表 ...
- C++的4种编程范型
基于过程procedural-based 基于对象object-based 面向对象object-oriented 泛型技术generics
- posix thread条件变量
概述 等待条件变量总是返回锁住的互斥量. 条件变量的作用是发送信号,而不是互斥. 与条件变量相关的共享数据是“谓词”,如队列满或队列空条件. 一个条件变量应该与一个谓词相关.如果一个条件变量与多个谓词 ...