zoukankan      html  css  js  c++  java
  • 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:
    "//+url.toString() 
            +"
    "+manager.getName() 
            +"
    "+cache.getName() 
            +" 's size = "+cache.getSize() 
            +"
    "+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

    完成!

    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他 们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其 实我是一个程序员
  • 相关阅读:
    工作流flowable官方文档阅读笔记2
    bladex代码生成表单字典(非普通字典)改造
    bladex代码生成改造字典(表管理字段带入)
    bladex前端页面设置
    重链剖分
    CodeForces 311B
    洛谷 P6302
    AtCoder abc164_f
    ISIJ2020 不知道算不算游记
    AtCoder abc165
  • 原文地址:https://www.cnblogs.com/kms1989/p/5534035.html
Copyright © 2011-2022 走看看