最近一个项目采用ehcache做为缓存技术,由于负载须要,使用两台服务器作负载均衡,因此须要作缓存的集群处理,综合各方面因素,决定使用JGroups的方式,接下来是连续3天的折磨,今天终于搞定,把这个过程总结出来分享,但愿相似须要的朋友别再重蹈个人曲折。html
一、首先不要一上来就搜索如何配置,要把基础环境搭好,这也是网上90%的相似文章中不涉及的。像 http://blog.csdn.net/kindy1022/article/details/6681299 这样的文章才真正有用,但仍然不够详细。接下来是详细内容:nginx
(1)我使用nginx + tomcat7 + jdk7;bootstrap
(2)ehcache版本为2.10,建议你们直接使用ehcache-2.10.jar而不要用ehcache-core-xxx.jar+ehcache-terracotta-xxx.jar;缓存
(3)jgroups使用最新的jgroups-3.6.4FINAL.jar,这个容易被忽略,网上不多有人提到,由于有ehcahce-jgroupsreplication-xxx.jar,因此会觉得这就够了,关键启动还不报错。另外没必要降版本;tomcat
(4)ehcache-jgroupsreplication-1.7.jar(就是查看这里面的源码时,发现JGroupsCacheReceiver须要jgroups jar包的支持)服务器
二、再说配置文件,这是网上传讹最多的,一是不讲版本,直接贴配置,二是配置自己也有错误。建议你们去jgroups和ehcache的官网上看相关的配置,注意是相关的配置,ehcache官网上给出的jgroups也不完整,也没标版本。这里必定要注意。建议你们将jgroups的配置使用单独的配置文件,这样更合理一些。mybatis
(1)echache配置文件ehcache.xml,首先增长peerproviderapp
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory" properties="jgroups_tcp.xml" />
(2)为每个须要同步的cache配置listener,固然asynchronousReplicationIntervalMillis不是必须的,默认是1000,bootstrapCacheLoaderFactory也能够不要负载均衡
<cache name="mybatis_common" overflowToDisk="true" eternal="true" timeToIdleSeconds="300" timeToLiveSeconds="600" maxElementsInMemory="10000" maxElementsOnDisk="100" diskPersistent="true" diskExpiryThreadIntervalSeconds="300" diskSpoolBufferSizeMB="50" memoryStoreEvictionPolicy="LRU"> <cacheEventListenerFactory class="net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory" properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true, replicateUpdatesViaCopy=false, replicateRemovals=true", asynchronousReplicationIntervalMillis=500/> <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.jgroups.JGroupsBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=false"/> </cache>
(3)jgroups_tcp.xml以下:参考 http://www.jgroups.org/manual/index.html#_tcpeclipse
<TCP bind_port="7800" /> <TCPPING timeout="3000" initial_hosts="app1_IP[7800],app2_IP[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"/>
究竟是bind_port仍是start_port,官网给出的是bind_port。
三、通常状况下,这样就足够了,可是事有例外,若是仍然不行,看看下面的可能性:
(1)集群的服务器能不能连通,有没有防火墙之类
(2)每台服务器是否是有完整的、惟一的hostname,若是你的hostname有中文,建议改为英文,若是你刚好使用mac电脑开发测试,那它的电脑名和hostname是两回事,默认的hostname是localhost,这个不行,要改为正经的。
(3)如今的eclipse能够反编译class文件,而且能够在class上打断点debug,在ehcahce-jgroupsreplication-xxx.jar里找到listener类和JGroupsCacheReceiver,加上断点,看发送和接收消息是否都被触发。
Good Luck。