Ehcache与terracotta集群配置

阅读更多
原文地址如下:
http://bluewind1521.iteye.com/blog/1636283
从启动结果中可以看到NodeID[XXXXXX] joined the cluster,节点已加入集群,代表terracotta结群建立成功,下面运行测试代码:
A服务器:
Java代码  收藏代码
import net.sf.ehcache.Cache; 
import net.sf.ehcache.CacheManager; 
import net.sf.ehcache.Element; 
 
public class EhcacheDemo { 
     
    private static final CacheManager CACHE_MANAGER = CacheManager.create(); 
     
    public static Cache getCache(String cacheName){ 
        return CACHE_MANAGER.getCache(cacheName); 
    } 
     
    public static Object getObjectValue(String key){ 
         Element e = getCache("demoCache").get(key); 
         Object val = null; 
         if(e == null){ 
                System.out.println("缓存中不存在,读取数据"); 
                /**用于测试集群是否正常工作,当缓存中的数据超时时加入新数据,同时将B服务器中的terracotta服务器关闭,测试B服务器中的程序是否能够获取数据
                 * 
                 * */ 
                EhcacheDemo.put("name", "11111"); 
                EhcacheDemo.put("password", "333"); 
         }else{ 
             val = e.getObjectValue(); 
         } 
         return val; 
    } 
     
    public static void put(String key, Object val){ 
        Element e = new Element(key, val); 
        getCache("demoCache").put(e); 
    } 
     
    public static void delete(String key){ 
        getCache("demoCache").remove(key); 
    } 
     
     
    public void close(){ 
        CACHE_MANAGER.shutdown(); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        EhcacheDemo.put("name", "sssssss"); 
        EhcacheDemo.put("password", "22222"); 
        Thread t = new Thread(new Runnable() { 
             
            @Override 
            public void run() { 
                while(true){ 
                    try { 
                        Thread.currentThread().sleep(10000L); 
                    } catch (InterruptedException e) { 
                        e.printStackTrace(); 
                    } 
                    System.out.println(EhcacheDemo.getObjectValue("name") +"   "+ getObjectValue("password")); 
                } 
                 
            } 
        }); 
 
        t.start(); 
    } 
 

 

B服务器:
Java代码  收藏代码
import net.sf.ehcache.Cache; 
import net.sf.ehcache.CacheManager; 
import net.sf.ehcache.Element; 
 
public class EhcacheDemo { 
     
    private static final CacheManager CACHE_MANAGER = CacheManager.create(); 
     
    public static Cache getCache(String cacheName){ 
        return CACHE_MANAGER.getCache(cacheName); 
    } 
     
    public static Object getObjectValue(String key){ 
         Element e = getCache("demoCache").get(key); 
         Object val = null; 
         if(e == null){ 
                System.out.println("缓存中不存在,读取数据"); 
         }else{ 
             val = e.getObjectValue(); 
         } 
         return val; 
    } 
     
    public static void put(String key, Object val){ 
        Element e = new Element(key, val); 
        getCache("demoCache").put(e); 
    } 
     
    public static void delete(String key){ 
        getCache("demoCache").remove(key); 
    } 
     
     
    public void close(){ 
        CACHE_MANAGER.shutdown(); 
    } 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        EhcacheDemo.put("name", "sssssss"); 
        EhcacheDemo.put("password", "22222"); 
        Thread t = new Thread(new Runnable() { 
             
            @Override 
            public void run() { 
                while(true){ 
                    try { 
                        Thread.currentThread().sleep(10000L); 
                    } catch (InterruptedException e) { 
                        e.printStackTrace(); 
                    } 
                    System.out.println(EhcacheDemo.getObjectValue("name") +"   "+ getObjectValue("password")); 
                } 
                 
            } 
        }); 
 
        t.start(); 
    } 
 

 


A与B服务器中的程序有一点区别,详见注释部分,主要为了测试集群是否工作正常,当一个节点挂掉之后,B服务器上的测试程序是否能获取新的缓存数据。
同时运行2个测试程序后,我们可以观察terracotta是否正常工作,运行ehcache\terracotta\bin下的dev-console.bat并连接到2个服务器的terracotta服务器:

你可能感兴趣的:(Ehcache与terracotta集群配置)