参考了https://my.oschina.net/u/866380/blog/501082的内容
下面直接贴代码
pom.xml
<!-- Ehcache实现,用于参考 -->
<dependency>
<artifactId>ehcache</artifactId>
<groupId>net.sf.ehcache</groupId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-jgroupsreplication</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>3.0.9.Final</version>
</dependency>
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect">
<diskStore path="java.io.tmpdir" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory"
properties="jgroups_tcp.xml" />
<defaultCache maxElementsInMemory="1000" eternal="false"
timeToLiveSeconds="120" overflowToDisk="false" />
<cache name="myCache" maxElementsInMemory="100" eternal="false"
timeToLiveSeconds="5" overflowToDisk="false" diskPersistent="false">
<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"
properties="bootstrapAsynchronously=false" />
</cache>
</ehcache>
需要缓存的cache加上红色标签内容
jgroups_tcp.xml<TCP bind_port="7800" />
<TCPPING timeout="3000"
initial_hosts="192.168.1.80:7800,192.168.1.81:7800,192.168.1.82:7800"
port_range="10"
num_initial_members="3"/>
<VERIFY_SUSPECT timeout="1500" />
<pbcast.NAKACK2 use_mcast_xmit="false" gc_lag="100"
retransmit_timeout="300,600,1200,2400,4800"
discard_delivered_msgs="true"/>
<pbcast.STABLE stability_delay="1000" desired_avg_gossip="50000"
max_bytes="400000"/>
<pbcast.GMS print_local_addr="true" join_timeout="3000" shun="false"
view_bundling="true"/>
inittaial_hosts是我的三台服务器ip
第一次运行没成功,原因是我的服务器没有关闭防火墙,关了之后就可以了
遇到异常的可以看看我另一篇博客http://blog.csdn.net/haiyang4988/article/details/54288754,或许能帮到你