0
我試圖同步兩個WAR-s的緩存 - A.war and B.war。他們部署在一個Tomcat上。每個WAR包含下一豆與INIT - 方法:在Java類中完全描述的EhCache複製不起作用
@Autowired
public class CacheService {
enum Cache {CACHE_1, CACHE_2};
@Autowired
private String cachePort;
public void init() throws Exception {
Configuration config = new Configuration();
FactoryConfiguration providerConfig = new FactoryConfiguration();
providerConfig.setClass("net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory");
providerConfig.setPropertySeparator(",");
providerConfig.setProperties("peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446");
config.addCacheManagerPeerProviderFactory(providerConfig);
FactoryConfiguration listenerConfig = new FactoryConfiguration();
listenerConfig.setClass("net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory");
listenerConfig.setPropertySeparator(",");
listenerConfig.setProperties("hostName=localhost,port=" + cachePort);
config.addCacheManagerPeerListenerFactory(listenerConfig);
CacheConfiguration defaultConfig = new CacheConfiguration();
defaultConfig.setTimeToLiveSeconds(120);
defaultConfig.setDiskExpiryThreadIntervalSeconds(120);
defaultConfig.setMemoryStoreEvictionPolicy("LRU");
defaultConfig.setStatistics(true);
defaultConfig.setMaxBytesLocalHeap(MemoryUnit.MEGABYTES.toBytes(1));
config.setDefaultCacheConfiguration(defaultConfig);
cacheManager = new CacheManager(config);
for (Cache cache : Cache.values()) {
cacheManager.addCache(cache.name());
net.sf.ehcache.Cache createdCache = cacheManager.getCache(cache.name());
createdCache.getCacheConfiguration().setTimeToLiveSeconds(600);
createdCache.getCacheEventNotificationService().registerListener(new RMIAsynchronousCacheReplicator(true, true, true, true, true, 5000, 1000));
}
}
}
A.war:cachePort = 40001
B.war:cachePort = 40002
但緩存是不同步。可能RMICacheManagerPeerProviderFactory,RMICacheManagerPeerListenerFactory或RMIAsynchronousCacheReplicator中某處的參數是錯誤的。
請問誰能幫忙?
還有,如果可能的話,任何人都可以描述這兩個WAR-s部署在1個子網中的兩臺機器上的不同tomcat上的情況嗎?
預先感謝您。
謝謝你的回答。但最後我更喜歡用XML文件配置緩存的方式。 – 2014-11-24 07:38:35
嗯..我沒有完全明白你的意思。在你的XML文件中你有「hostName = 127.0.0.1」。這將使ehcache RMI在每個主機上監聽自己,而不是監聽其他主機,因爲每個主機都會將它們的緩存廣播爲127.0.0.1/cacheName。因此,如果在每臺服務器上將此值從127.0.0.1更改爲該主機的真實IP,並重新啓動tomcat,則服務器將開始廣播其實際地址,以便其他服務器將正確地看到它 – 2014-11-24 19:54:25
我明白了。實際上兩個應用程序都運行在同一臺服務器上我將配置從Java-class移動到_ehcache.xml_。像這樣:'<?xml version =「1.0」encoding =「UTF-8」?> ...'複製工作正確。 –
2014-11-25 09:16:18