benny周 2015-03-26 09:24 采纳率: 0%
浏览 1665

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;

}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:37
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入