ehcache 分布式集群同步数据实例

本文使用rmi方式,借鉴百度能搜到的文章,但是均不能做到数据同步,做了些改动完全没问题,更详细说明介绍百度即可。直奔主题,可运行的demo实例!

创建一个maven项目,配置pom

pom.xml

<dependencies>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.8</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-jgroupsreplication</artifactId>
            <version>1.7</version>
        </dependency>
    </dependencies>

服务器A 配置

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

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

    <!-- 指定除自身之外的网络群体中其他提供同步的主机列表,多台机器配置 用'|'分割 -->
    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,rmiUrls=//192.168.1.74:4005/demoCache">
    </cacheManagerPeerProviderFactory>
    
    <cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostName=192.168.1.23,port=4005,socketTimeoutMillis=120000" /> 
    
    <!-- 
    多播方式配置
    搜索某个网段上的缓存 timeToLive 
    0是限制在同一个服务器 
    1是限制在同一个子网 
    32是限制在同一个网站 
    64是限制在同一个region 
    128是限制在同一个大洲 255是不限制 
    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=automatic, multicastGroupAddress=224.1.1.1,
        multicastGroupPort=40000, timeToLive=32" /> -->
        
    <!-- 默认缓存 -->
    <defaultCache maxElementsInMemory="1000" eternal="true"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="true" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <!-- demo缓存 -->
    <cache name="demoCache" maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
        <!-- 用于在初始化缓存,以及自动设置 -->
        <bootstrapCacheLoaderFactory
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
    </cache>
</ehcache>

测试代码。

Mytest.java

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class Mytest {
    public static void main(String[] args) throws InterruptedException {
        CacheManager manager = new CacheManager("src/test/resources/ehcache.xml");   

        //get Cache        
        Cache cache = manager.getCache("demoCache");     
        Thread.sleep(10000); 
        Element element = new Element("key","test");  
        cache.put(element);  

        System.out.println("Initial:\n"//+url.toString() 
        +"\n"+manager.getName() 
        +"\n"+cache.getName() 
        +" 's size = "+cache.getSize() 
        +"\n"+element.toString());     


        Element element01 = cache.get("key");        
        System.out.println(element01.getValue());  
        System.out.println("主机测试等待中.............");  
        
        while(true){ 
        Thread.sleep(1000); 
        } 
    } 
}

服务器B

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

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

    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,rmiUrls=//192.168.1.23:4005/demoCache">
    </cacheManagerPeerProviderFactory>
    
    <cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="hostName=192.168.1.74,port=4005,socketTimeoutMillis=120000" /> 
    
    <!-- 
    多播方式配置
    搜索某个网段上的缓存 timeToLive 
    0是限制在同一个服务器 
    1是限制在同一个子网 
    32是限制在同一个网站 
    64是限制在同一个region 
    128是限制在同一个大洲 255是不限制 
    <cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=automatic, multicastGroupAddress=224.1.1.1,
        multicastGroupPort=40000, timeToLive=32" /> -->
        
    <!-- 默认缓存 -->
    <defaultCache maxElementsInMemory="1000" eternal="true"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="true" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <!-- demo缓存 -->
    <cache name="demoCache" maxElementsInMemory="1000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
        <!-- 用于在初始化缓存,以及自动设置 -->
        <bootstrapCacheLoaderFactory
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />
    </cache>
</ehcache>

测试代码

MyTest.java

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class Mytest {
    public static void main(String[] args) throws InterruptedException {
        CacheManager manager = new CacheManager("src/test/resources/ehcache.xml");   

        //get Cache        
        Cache cache = manager.getCache("demoCache");     
        Thread.sleep(10000); 


        while(true){ 
            System.out.println("搜索中...");  
            System.out.println("当前资源数:" + cache.getSize());  
            Element element = cache.get("key");  
            if (element != null) {  
                System.out.println(element.getValue());   
                break;  
            } 
        Thread.sleep(1000); 
        } 
    } 
}

 先运行服务器A,在运行服务器B。

效果:

服务器A

服务器B

 

完成!

posted @ 2016-05-11 14:15  茫洋  阅读(4091)  评论(0编辑  收藏  举报