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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)