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

摘要:
本文使用了rmi方法,使用了百度可以搜索的文章作为参考,但无法实现数据同步。做些改变没问题。只需更详细地介绍百度。直接进入主题,一个可运行的演示实例!创建一个maven项目,Configure pompompom。xml<dependencies><dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.0</version></adependency<<dependency><groupId>org。springframework</groupId><artifactId>spring上下文支持</artifactId><version>4.1.6。RELEASE组织。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></adependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache jgroupsreplication</artifactId><version>1.7</version></aDependency>服务器A配置ehcache。xml˂!--多播模式配置在网络段上搜索缓存timeToLive0,不受限制˂!

本文使用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
         
        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
         
        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

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

服务器B

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

完成!

免责声明:文章转载自《ehcache 分布式集群同步数据实例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mac中安装Jenkins+jdkJmeter5.3分布式测试配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

设置缓存的大小

目录 1     CacheManager级别 2     Cache级别 3     大小衡量 4     配置大小示例        缓存大小的限制可以设置在CacheManager上,也可以设置在单个的Cache上。我们可以设置缓存使用内存的大小,也可以设置缓存使用磁盘的大小,但是使用堆内存的大小是必须设置的,其它可设可不设,默认不设就是无限制。在设...

Ehcache 缓存

1 packageorg.jeecgframework.core.util; 2 3 importnet.sf.ehcache.Cache; 4 importnet.sf.ehcache.CacheManager; 5 importnet.sf.ehcache.Element; 6 7 /** 8 * ehcache 缓存工具类 9 * 10 * c...

ehcache 缓存

、通过使用API来动态的添加缓存(将缓存的配置信息通过java代码来实现而非写在配置文件) 2、通过配置文件ehcache.xml创建缓存实例 packageorg.jeecgframework.cache; importorg.jeecgframework.core.util.StringExt; public classCacheInstanc...

12、MyBatis教程之缓存

13、缓存 简介 1、什么是缓存 [ Cache ]? 存在内存中的临时数据。 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题。 2、为什么使用缓存? 减少和数据库的交互次数,减少系统开销,提高系统效率。 3、什么样的数据能使用缓存? 经常查询...

springboot + ehcache

一、使用 springboot + ehcache本地堆缓存实现相应功能   1、引入ehcache的jar包    2、创建ehcache的xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSche...

EhCache缓存的使用

EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。   我们使用EhCache缓存框架主要是为了判断重复Url,每次爬取一个网页,都把Url存储到缓存中,并且每次爬某个网页之前,都去缓存中搜索下,假如存在的话,我们就不要爬取这个网页了,不存在的话,我们就爬下网页,爬取成功后,把这...