EhCache 在集群环境中使用缓存系统

来源:互联网 发布:怎样在淘宝上发布图片 编辑:程序博客网 时间:2024/05/22 14:40

原文链接:http://www.cnblogs.com/daxin/p/3568687.html

EhCache 分布式缓存/缓存集群  EhCache提供了很多种解决方案 这里只介绍一种最常用而且简单的RMI方式分布式缓存决绝方案

Automatic Peer Discovery 自动成员发现方式

自动的发现方式用TCP广播机制来确定和维持一个广播组。它只需要一个简单的配置可以自动的在组中添加和移除成员。在集群中也不需要什么优化服务器的知识,这是默认推荐的。成员每秒向群组发送一个“心跳”。如果一个成员 5秒种都没有发出信号它将被群组移除。如果一个新的成员发送了一个“心跳”它将被添加进群组。任何一个用这个配置安装了复制功能的cache都将被其他的成员发现并标识为可用状态。

1 首先配置多播地址 Configuring the Peer Provider 【注意自动成员发现 server1与server2的配置相同】

 

    <cacheManagerPeerProviderFactory        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"        properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,        multicastGroupPort=4446, timeToLive=32"/>

 

 

2 然后配置CacheManagerPeerListener 用来接收其他集群服务器发来的消息

<cacheManagerPeerListenerFactory        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"        properties="hostName=192.168.1.253, port=40001,        socketTimeoutMillis=2000"/>

1 hostName=192.168.1.253 这个需要在每台服务器上配置自己的真实IP

 

3 在需要缓存的cache上配置 Configuring Cache Replicators 启动集群

复制代码
<cache name="MyCache"        maxElementsInMemory="50"        eternal="true"    >    <cacheEventListenerFactory       class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>    </cache>
复制代码

 

Manual Peer Discovery {#Manual Peer Discovery} 手动成员发现

进行手动成员配置要知道每个监听器的IP地址和端口。成员不能在运行时动态地添加和移除。在技术上很难使用广播的情况下就可以手动成员发现,例如在集群的服务器之间有一个不能传送广播报文的路由器。你也可以用手动成员发现进行单向的数据复制,只让server2知道server1,而server1不知道server2。

假设你在集群中有两台服务器。你要同步sampleCache1和sampleCache2。下面是每个服务器需要的配置:
配置server1

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//server2:40001/MyCache|//server2:40001/其他缓存的name"/>

配置server2

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//server1:40001/MyCache|//server1:40001/其他缓存的name"/>

接着配置上面红色2,3步骤就可以了。

<cacheManagerPeerListenerFactory        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"        properties="hostName=192.168.1.253, port=40001,        socketTimeoutMillis=2000"/>
复制代码
 <cache name="MyCache"        maxElementsInMemory="50"        eternal="true"    >    <cacheEventListenerFactory       class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>    </cache>
复制代码
在使用手动发现成员配置的时候有一个特别需要注意的地方。就是在配置 rmiUrls=//server1:40001/MyCahce|//server1:40001/其他缓存名称" 这里的时候需要把你希望参与到集群的缓存名称配置进去 比如下面用到的MyCache.

 

CacheManagerPeerListener 上面的步骤2的配置说明

<cacheManagerPeerListenerFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="hostName=localhost, port=40001,socketTimeoutMillis=2000"/>
复制代码
properties 有效的属性是:
hostname (可选) – 运行监听器的服务器名称。标明了做为集群群组的成员的地址,同时也是你想要控制的从集群中接收消息的接口。在CacheManager初始化的时候会检查hostname是否可用。如果hostName不可用,CacheManager将拒绝启动并抛出一个连接被拒绝的异常。如果指定,hostname将使用InetAddress.getLocalHost().getHostAddress()来得到。警告:不要将localhost配置为本地地址127.0.0.1,因为它在网络中不可见将会导致不能从远程服务器接收信息从而不能复制。在同一台机器上有多个CacheManager的时候,你应该只用localhost来配置。port – 监听器监听的端口。socketTimeoutMillis (可选) – Socket超时的时间。默认是2000ms。当你socket同步缓存请求地址比较远,不是本地局域网。你可能需要把这个时间配置大些,不然很可能延时导致同步缓存失败。
复制代码

Configuring Cache Replicators 上面配置3的说明

复制代码
<cache name="sampleCache2"  maxEntriesLocalHeap="10"  eternal="false"  timeToIdleSeconds="100"  timeToLiveSeconds="100"  overflowToDisk="false"><cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true "/></cache>
复制代码
复制代码
class – 使用net.sf.ehcache.distribution.RMICacheReplicatorFactory这个工厂支持以下属性:replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers. 默认是true。replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。replicateAsynchronously=true | false – 复制方式是异步的(指定为true时)还是同步的(指定为false时)。默认是true。replicatePutsViaCopy=true | false – 当一个新增元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制(指定为true时为复制),默认是true。你可以使用ehcache的默认行为从而减少配置的工作量,默认的行为是以异步的方式复制每件事;你可以像下面的例子一样减少RMICacheReplicatorFactory的属性配置:
复制代码
复制代码
<cache name="sampleCache4"      maxEntriesLocalHeap="10"      eternal="true"      overflowToDisk="false"      memoryStoreEvictionPolicy="LFU">   <cacheEventListenerFactory       class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/></cache>
复制代码

 

https://www.ibm.com/developerworks/cn/java/j-lo-ehcache/ 相关文章

http://www.cnblogs.com/hoojo/archive/2012/07/19/2599534.html 相关文章

http://ehcache.org/documentation/replication/rmi-replicated-caching 官方文档

原文链接:http://www.cnblogs.com/daxin/p/3568687.html


阅读全文
0 0