JGroups 也提供了基于 TCP 的单播 ( Unicast ) 和基于 UDP 的多播 ( Multicast ) ,对应 RMI 的手工配置和自动发现。使用单播方式需要指定其它节点的主机地址和端口,下面是两个节点,并使用了单播方式的配置:
<!-- 使用单播方式配置 --> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory" properties="connect=TCP(bind_addr=127.0.0.1;bind_port=4000): TCPPING(initial_hosts=127.0.0.1[4000],127.0.0.1[5000];port_range=1;timeout=5000;num_initial_members=2): MERGE2(min_interval=3000;max_interval=5000): FD_ALL(interval=5000;timeout=20000): FD(timeout=5000;max_tries=48;): VERIFY_SUSPECT(timeout=1500): pbcast.NAKACK(retransmit_timeout=100,200,300,600,1200,2400,4800;discard_delivered_msgs=true): pbcast.STABLE(stability_delay=1000;desired_avg_gossip=20000;max_bytes=0): pbcast.GMS(print_local_addr=true;join_timeout=5000)" propertySeparator="::" />
使用多播方式配置如下:
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory" properties="connect=UDP(mcast_addr=224.1.1.1;mcast_port=45678;ip_ttl=32;mcast_send_buf_size=120000;mcast_recv_buf_size=80000): PING(timeout=2000;num_initial_members=2): MERGE2(min_interval=5000;max_interval=10000): FD_SOCK:VERIFY_SUSPECT(timeout=1500): pbcast.NAKACK(retransmit_timeout=3000): UNICAST(timeout=5000): pbcast.STABLE(desired_avg_gossip=20000): FRAG: pbcast.GMS(join_timeout=5000;print_local_addr=true)" propertySeparator="::" />cache缓存节点配置
<!-- 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.jgroups.JGroupsCacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true" /> <!-- 用于在初始化缓存,以及自动设置 --> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsBootstrapCacheLoaderFactory" /> </cache>详情参考demo源码: http://download.csdn.net/detail/tianwei7518/9231745
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-jgroupsreplication</artifactId> <version>1.7</version> </dependency>
参考文章:
1.EhCache集群方案-JGroups
2.ehcache配置文件译文
3.EhCache集群