Spring Boot中的ehcache服务器同步

时间:2018-07-23 00:46:16

标签: spring-boot ehcache

我是一个想使用ehcache的初学者。

我想用ehcache同步多台服务器上的缓存

但是它阻止了与服务器连接后立即关闭的现象。

我们试图找到并修复它,但是我们无法修复。

如果您让我知道您需要解决的问题,我将不胜感激。

ehcache.xml

<ehcache>
    <diskStore path="java.io.tmpdir">

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

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

    <cache 
        name="sessionInfoCache"
        maxElementsInMemory="10000" 
        eternal="false" 
        timeToIdleSeconds="600" 
        timeToLiveSeconds="1200" 
        overflowToDisk="false"> 

        <cacheEventListenerFactory 
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
            properties="replicateAsynchronously=true, replicatePuts=true,       
            replicateUpdates=true, 
            replicateUpdatesViaCopy=false, replicateRemovals=true ">
        </cacheEventListenerFactory> 
        <bootstrapCacheLoaderFactory 
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
            properties="bootstrapAsynchronously=true,                          
            maximumChunkSizeBytes=5000000"/>

    </cache> 

    <cache 
        name="resourceInfoCache"
        maxElementsInMemory="10000" 
        eternal="false" 
        timeToIdleSeconds="600" 
        timeToLiveSeconds="1200" 
        overflowToDisk="false"> 

        <cacheEventListenerFactory 
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 
            properties="replicateAsynchronously=true, replicatePuts=true,             
            replicateUpdates=true, 
            replicateUpdatesViaCopy=false, replicateRemovals=true ">
        </cacheEventListenerFactory>
        <bootstrapCacheLoaderFactory 
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" 
            properties="bootstrapAsynchronously=true,            
            maximumChunkSizeBytes=5000000"/>

    </cache> 


</ehcache>

使用ehcache的类

@Autowired
private EhCacheCacheManager ehCacheManager;


public void setServerResourceInfoCache(ResourceInfo resourceInfo) {
    Cache resourceInfoCache = ehCacheManager.getCache("resourceInfoCache");
    resourceInfoCache.put(resourceInfo.getServerId(), resourceInfo);
}

public void removeServerResourceInfoCache(ResourceInfo resourceInfo) {
    Cache resourceInfoCache = ehCacheManager.getCache("resourceInfoCache");
    resourceInfoCache.evict(resourceInfo.getServerId());
} 

public void getResourceInfoCache() {

    Cache resourceInfoCache = ehCacheManager.getCache("resourceInfoCache");
    @SuppressWarnings("unchecked")
    List<String> cacheKeyList = ehCacheManager.getCacheManager().getCache("resourceInfoCache").getKeys();

    System.out.println();
    if(cacheKeyList!=null && cacheKeyList.size()>0){
        for (String keyString : cacheKeyList) {
            System.out.println(keyString + " - " +resourceInfoCache.get(keyString).get().toString());
        }
    } else {
        System.out.println("cacheKeyList = 0");
    }
    System.out.println();
    System.out.println("---------------------------------------");

}

}

0 个答案:

没有答案