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

原文地址:https://www.cnblogs.com/fdzfd/p/5776528.html