ehcache集群数据不同步。

问题如题。

server1 : win 7,tomcat。ip:192.168.1.213

server2:win 7中安装的centos6.6虚拟机,tomcat。ip:192.168.1.211.

ehcache:2.9版本
需要缓存的App对象已经实现Serializable接口。两个server中的防火墙都已经关闭。

具体文件如下面所示。配置都是参照官网配的,不知道为什么数据不同步。小弟刚接触,望各位不吝赐教,谢谢!

ehcache.xml文件如下:

xsi:noNamespaceSchemaLocation="ehcache.xsd">

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=40001, timeToLive=1"/>

<cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=localhost, port=40001,
    socketTimeoutMillis=2000"/>


<cache name="sampleCache2"
    maxEntriesLocalHeap="10"
    eternal="false"
    timeToIdleSeconds="100"
    timeToLiveSeconds="3600"
    overflowToDisk="false">

    <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>

EhcacheUtil.java文件:

public class EhcacheUtil implements Serializable {

/**
  • */
    private static final long serialVersionUID = 1L;

    private static final String path = "/cache/ehcache.xml";

    private URL url;

    private CacheManager manager;

    private static EhcacheUtil ehCache;

    private EhcacheUtil(String path) {

    url = getClass().getResource(path);  
    manager = CacheManager.create(url);  
    

    }

    public static EhcacheUtil getInstance() {

    if (ehCache== null) {

    ehCache= new EhcacheUtil(path);

    }

    return ehCache;

    }

    public void put(String cacheName, String key, Object value) {

    Cache cache = manager.getCache(cacheName);

    Element element = new Element(key, value);

    cache.put(element);

    }

    public Object get(String cacheName, String key) {

    Cache cache = manager.getCache(cacheName);

    Element element = cache.get(key);

    return element == null ? null : element.getObjectValue();

    }

    public Cache get(String cacheName) {

    return manager.getCache(cacheName);

    }

    public void remove(String cacheName, String key) {

    Cache cache = manager.getCache(cacheName);

    cache.remove(key);

    }

    }

IndexController.java文件:

@WebServlet(description = "index", urlPatterns = { "/index" })
public class IndexController extends HttpServlet {
private static final long serialVersionUID = 1L;
// private static App app = new App();
/**
* @see HttpServlet#HttpServlet()
*/
public IndexController() {
super();
// TODO Auto-generated constructor stub
App app = new App();
EhcacheUtil.getInstance().put("sampleCache2", "APP", app);
}

/**

  • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub App obj = (App)EhcacheUtil.getInstance().get("sampleCache2","APP"); obj.num++; EhcacheUtil.getInstance().put("sampleCache2","APP",obj); response.getOutputStream().write(("num="+obj.num).getBytes()); }

/**

  • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }

App.java文件:
public class App implements Serializable{

private static final long serialVersionUID = 1L;
public static int num=0;

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问