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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个示例 ehcache3 配置类: ```java import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.MemoryUnit; import org.ehcache.core.config.DefaultConfiguration; import org.ehcache.xml.XmlConfiguration; import java.net.URL; public class EhCacheConfig { public static final String CACHE_NAME = "myCache"; public CacheManagerBuilder cacheManagerBuilder() { return CacheManagerBuilder.newCacheManagerBuilder() .withCache(CACHE_NAME, CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(100, MemoryUnit.MB))); } public DefaultConfiguration defaultConfiguration() { return new DefaultConfiguration(CACHE_NAME); } public XmlConfiguration xmlConfiguration() { URL myUrl = getClass().getResource("/ehcache.xml"); return new XmlConfiguration(myUrl); } } ``` 这个配置类主要用于创建和配置 ehcache3 的缓存管理器。 在这个示例中,我们定义了三个方法: - `cacheManagerBuilder()` 方法创建了一个缓存管理器生成器,并使用 `CacheConfigurationBuilder` 类定义了一个名为 "myCache" 的缓存。 - `defaultConfiguration()` 方法创建了一个默认配置,用于创建一个只有一个缓存的缓存管理器。 - `xmlConfiguration()` 方法使用一个 XML 文件来创建一个缓存管理器。 使用配置类的主要原因是为了能够更方便地配置缓存管理器。通过将配置逻辑抽取到单独的类中,可以更好地维护和管理缓存管理器的配置。 此外,将 ### 回答2: ehcache是一个流行的Java缓存框架,ehcache3是其最新版本。在ehcache3中,可以通过编写配置类来定义和配置缓存。 以下是一个ehcache3配置类的示例: ```java import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.ResourcePools; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; public class EhcacheConfig { public static CacheManager getCacheManager() { // 创建缓存配置 CacheConfiguration<String, Object> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder( String.class, Object.class, ResourcePoolsBuilder.heap(100)) // 使用堆内存作为缓存的存储介质,最大容量100 .build(); // 创建缓存管理器并添加缓存配置 CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", cacheConfiguration) .build(true); // 启动缓存管理器,并开启缓存 return cacheManager; } } ``` 这个配置类的写法有几个优点: 1. 模块化:通过将缓存相关的配置封装在一个类中,可以方便地管理、修改和重用缓存配置。在需要使用缓存的地方,只需要调用`getCacheManager`方法即可得到缓存管理器。 2. 可配置性:通过使用ehcache3提供的配置构建器,我们可以轻松地配置不同的缓存策略和存储介质。在示例中,我们选择了堆内存作为缓存的存储介质,并设置了最大容量为100。根据需求,可以根据实际情况调整这些配置。 3. 可扩展性:ehcache3支持插件式的架构,可以根据需要添加额外的插件或扩展其功能。在这个配置类中,我们只使用了最基本的配置,但可以根据具体需求添加更多的配置,例如持久化、集群等。 因此,这个ehcache3配置类的写法简洁、模块化,提供了灵活的配置选项和可扩展性,方便管理和使用缓存。 ### 回答3: ehcache3 是一个用于Java的开源缓存框架,它提供了可靠、高效的缓存解决方案。以下是一个 ehcache3 配置类的示例,并解释了为什么要以这种方式编写: ```java import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; public class EhcacheConfig { public CacheManager cacheManager() { CacheConfiguration<String, Object> cacheConfig = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, Object.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(1000, EntryUnit.ENTRIES) .offheap(500, MemoryUnit.MB)) .build(); CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", cacheConfig) .build(true); return cacheManager; } } ``` 以上的 ehcache3 配置类具有以下特点和优势: 1. 引入了必需的 ehcache3 类库,确保代码能够编译和运行。 2. 使用了 Builder 模式,可实现更简洁、可读性更强的代码编写。通过链式调用,能够按照先后顺序串联配置参数。 3. 在 `cacheManager` 方法中,首先创建了一个 `CacheConfiguration` 实例。该实例定义了缓存的键和值类型,并配置了缓存的内存大小。在示例中,缓存的配置为1000个堆内存项和500MB的离堆内存。 4. 之后,通过 `CacheManagerBuilder` 创建了 `CacheManager` 的实例,并提供了两个参数。`withCache` 方法用于指定缓存的名称,此处使用了 "myCache"。`build` 方法用于构建 `CacheManager` 实例,并传入 `true` 参数,表示在构建时直接初始化该实例。 5. 最后,将构建好的 `CacheManager` 实例返回。 这样编写 ehcache3 配置类的好处在于: 1. 采用了 Builder 模式,使得代码更易于扩展和维护。可以根据实际需求,通过链式调用,方便地配置缓存参数。 2. 通过将缓存配置和缓存管理分离,提高了代码的可读性和可维护性。缓存配置和缓存管理分别放在不同的方法中,使得代码结构更清晰。 3. 可以根据实际需求,灵活地配置缓存的大小和类型。在示例中,将缓存设置为1000个堆内存项和500MB的离堆内存,可根据实际情况进行调整。 4. 返回了初始化好的 `CacheManager` 实例,方便其他部分使用。缓存管理器可被注入到其他类中,从而在整个应用程序中方便地进行缓存管理操作。 总之,以上是一个 ehcache3 配置类的示例,并解释了为什么要以这种方式编写。通过使用 Builder 模式和分离配置与管理的方式,能够提高代码的可读性、可维护性和灵活性,同时还能方便地整合到应用程序中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值