Java Ehcache 集群 - 分布式缓存
Ehcache 有几种方式集群。这里将介绍RMI方式
原因如下:
1、rmi是Java 默认的远程机制
2、可以调优tcp选项
3、Elements因为要存到磁盘,所以肯定是早已序列化。所以不需要借助xml格式化什么的
4、通过配置可以通过防火墙
机器1:地址:192.168.1.103::40001 缓存节点名:sampleCache1
先配置ehcache.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect"> <!-- --> <diskStore path="./target/tmp" /> <!---缓存成员发现工厂,管理cacheManager对象 properties的值 必须为逗号(,)分隔的键值对形式 peerDiscovery:节点发现模式 manual手工发现,automatic自动发现 rmiUrls设置为另一台服务器的IP、端口和缓存名 多个地址用| eg://192.168.1.103:40001/sampleCache1|//192.168.0.13:4567/oschina_cache --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//192.168.1.103:40002/sampleCache1" /> <!-- 构建RMI监听器 监听端口 --> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=192.168.1.103,port=40001,socketTimeoutMillis=2000" propertySeparator="," /> <!-- defult cache config 默认缓存配置 --> <defaultCache maxElementsInMemory="10000" maxElementsOnDisk="0" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="true" /> <!-- 自定义缓存配置1 --> <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="120" > <!-- properties配置对象 在查看源码是 说明会有这些值 --> <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicatePutsViaCopy=true, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true, asynchronousReplicationIntervalMillis=200" /> <!-- 启动时同步数据 --> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> </cache> <!-- 自定义缓存配置2 --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" diskPersistent="true" memoryStoreEvictionPolicy="FIFO" /> </ehcache>
同步程序如下:
import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.Iterator; import javax.management.MBeanServer; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.management.ManagementService; public class TestMain { public static void main(String[] argv) throws IOException{ //从class目录中自动查找ehcache.xml配置文件 CacheManager cacheManager = CacheManager.getInstance(); //添加元素 MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); //从配置文件中获取实例 Cache cache = cacheManager.getCache("sampleCache1"); System.in.read(); Iterator iterator = cache.getKeys().iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); System.in.read(); } cacheManager.shutdown(); System.out.println("OK"); } }
机器2:地址:192.168.1.103::40002 缓存节点名:sampleCache1
先配置ehcache.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="autodetect"> <!-- --> <diskStore path="F:/ehcache/diskStore" /> <!---缓存成员发现工厂,管理cacheManager对象 properties的值 必须为逗号(,)分隔的键值对形式 peerDiscovery:节点发现模式 manual手工发现,automatic自动发现 rmiUrls设置为另一台服务器的IP、端口和缓存名 多个地址用| eg://192.168.0.12:4567/oschina_cache|//192.168.0.13:4567/oschina_cache --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=manual,rmiUrls=//192.168.1.103:40001/sampleCache1" /> <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" properties="hostName=192.168.1.103,port=40002,socketTimeoutMillis=2000" propertySeparator="," /> <!-- defult cache config 默认缓存配置 --> <defaultCache maxElementsInMemory="10000" maxElementsOnDisk="0" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" diskPersistent="true" /> <!-- 自定义缓存配置1 --> <cache name="sampleCache1" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" diskPersistent="true" diskExpiryThreadIntervalSeconds="120" > <!-- properties配置对象 在查看源码是 说明会有这些值 --> <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> <!-- 自定义缓存配置2 --> <cache name="sampleCache2" maxElementsInMemory="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" diskPersistent="true" memoryStoreEvictionPolicy="FIFO" /> </ehcache>
同步程序如下:
import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import net.sf.ehcache.management.ManagementService; import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.Random; import javax.management.MBeanServer; import net.sf.ehcache.Cache; /** * Hello world! * */ public class App { public static void main( String[] args ) throws IOException { CacheManager cacheManager = CacheManager.getInstance(); //添加元素 MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ManagementService.registerMBeans(cacheManager, mBeanServer, true, true, true, true); //从配置文件中获取实例 Cache cache = cacheManager.getCache("sampleCache1"); Random random = new Random(); while(true){ int number = random.nextInt(10000); cache.put(new Element(number,"2")); System.out.println(cache.get(number)); System.in.read(); } } }
先运行机器2.就可以看到同步了
Java JavaScript ECMAScript 小严

浙公网安备 33010602011771号