6. 分布式緩存集群環境配置 轉

          http://www.cnblogs.com/hoojo/archive/2012/07/19/2599534.html
          http://www.cnblogs.com/hellowood23/p/5210267.html
          http://blog.csdn.net/ni_hao_ya/article/details/9344779
          http://www.cnblogs.com/hellowood23/p/5210267.html

          這篇我將探討ehcache的集群配置,并寫兩個分布在不同邏輯機器上的程序去測試是不是達到了集群效果。我的目標就是得到踏踏實實的配置成功的感覺。

          一.集群配置方式

          ehcache提供三種網絡連接策略來實現集群,rmi,jgroup還有jms。這里只說rmi方式。同時ehcache可以可以實現多播的方式實現集群。也可以手動指定集群主機序列實現集群,本例應用手動指定。

          這里說點題外話,本來看著分發包中的原來的例子配置是一件不怎么難的事情,應該很容易就能實現。但是一開始,我是在我的linux主機上和我的主操作系統windows上實現集群配置。結果反過來弄過去,都沒有成功。然后在網上找一些別人的配置經驗,竟然都是配置片段,沒有完整的實例文件。結果配置半天沒成功。但我懷疑是我的linux系統有些地方可能沒有配置好,于是先不管他。有開啟了我的另一個windows主機。然后把程序部署上去,竟然一次試驗成功。高興的同時,我得發句話“不要把代碼片段稱作實例,這很不負責任”。同時還存在一個問題,在linux下沒有部署成功的原因有待查明。

          具體說明:配置cacheManagerPeerListenerFactory是配宿主主機配置監聽程序,來發現其他主機發來的同步請求

          配置cacheManagerPeerProviderFactory是指定除自身之外的網絡群體中其他提供同步的主機列表,用“|”分開不同的主機。

          下面的例子的測試過程是:主機B緩存開啟,并從名為UserCache的緩存中循環抓取鍵值為“key1”的元素,直到取到,才退出循環。主機A緩存啟動,并在名為UserCache的緩存中放入鍵值為“key1”的元素。顯然,如果主機B取到的元素,那么就證明同步成功,也就是集群成功。

          所以在測試過程中先啟動主機B的測試程序,在啟動主機A的測試程序。

          下面具體說配置文件以及測試程序:

          1. 主機A的配置文件以及測試源代碼

          config/ehcache_cluster.xml

          XML/HTML代碼
          1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
          2.     xsi:noNamespaceSchemaLocation="ehcache.xsd">      
          3.     <cacheManagerPeerProviderFactory      
          4.         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"      
          5.         properties="peerDiscovery=manual       
          6.         rmiUrls=//192.168.1.254:40000/UserCache" />      
          7.       
          8.     <cacheManagerPeerListenerFactory      
          9.         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"      
          10.         properties="hostName=192.168.1.126,port=40000,socketTimeoutMillis=120000" />      
          11.       
          12.     <defaultCache maxElementsInMemory="10000" eternal="false"      
          13.         timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"      
          14.         diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"      
          15.         diskPersistent="false" diskExpiryThreadIntervalSeconds="120"      
          16.         memoryStoreEvictionPolicy="LRU">      
          17.         <cacheEventListenerFactory      
          18.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />      
          19.     </defaultCache>      
          20.       
          21.     <cache name="UserCache" maxElementsInMemory="1000" eternal="false"      
          22.         timeToIdleSeconds="100000" timeToLiveSeconds="100000"      
          23.         overflowToDisk="false">      
          24.         <cacheEventListenerFactory      
          25.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />      
          26.     </cache>      
          27. </ehcache>    

          tutorial/UsingCacheCluster

          Java代碼
          1. package tutorial;       
          2.       
          3. import java.net.URL;       
          4.       
          5. import net.sf.ehcache.Cache;       
          6. import net.sf.ehcache.CacheManager;       
          7. import net.sf.ehcache.Element;       
          8.       
          9. public class UsingCacheCluster       
          10.       
          11.     public static void main(String[] args) throws Exception       
          12.         URL url UsingCacheCluster.class.getClassLoader().getResource(       
          13.                 "config/ehcache_cluster.xml");       
          14.         CacheManager manager new CacheManager(url);       
          15.         //取得Cache       
          16.         Cache cache manager.getCache("UserCache");       
          17.         Element element new Element("key1""value1");       
          18.         cache.put(element);       
          19.       
          20.         Element element1 cache.get("key1");       
          21.         System.out.println(element1.getValue());       
          22.           
          23.       
          24.     

          2.  主機B上的配置文件以及測試代碼

          config/ehcache_cluster.xml

          XML/HTML代碼
          1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
          2.     xsi:noNamespaceSchemaLocation="ehcache.xsd">      
          3.     <cacheManagerPeerProviderFactory      
          4.         class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"      
          5.         properties="peerDiscovery=manual       
          6.         rmiUrls=//192.168.1.126:40000/UserCache" />      
          7.       
          8.     <cacheManagerPeerListenerFactory      
          9.         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"      
          10.         properties="hostName=192.168.1.254,port=40000, socketTimeoutMillis=120000" />      
          11.       
          12.     <defaultCache maxElementsInMemory="10000" eternal="false"      
          13.         timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"      
          14.         diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"      
          15.         diskPersistent="false" diskExpiryThreadIntervalSeconds="120"      
          16.         memoryStoreEvictionPolicy="LRU">      
          17.         <cacheEventListenerFactory      
          18.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />      
          19.     </defaultCache>      
          20.       
          21.     <cache name="UserCache" maxElementsInMemory="1000" eternal="false"      
          22.         timeToIdleSeconds="100000" timeToLiveSeconds="100000"      
          23.         overflowToDisk="false">      
          24.         <cacheEventListenerFactory      
          25.             class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />      
          26.     </cache>      
          27. </ehcache>    

          tutorial/UsingCacheCluster

          Java代碼
          1. package tutorial;       
          2.       
          3. import java.net.URL;       
          4.       
          5. import net.sf.ehcache.Cache;       
          6. import net.sf.ehcache.CacheManager;       
          7. import net.sf.ehcache.Element;       
          8.       
          9. public class UsingCacheCluster       
          10.       
          11.     public static void main(String[] args) throws Exception       
          12.         URL url UsingCacheCluster.class.getClassLoader().getResource(       
          13.                 "config/ehcache_cluster.xml");       
          14.         CacheManager manager new CacheManager(url);       
          15.         //取得Cache       
          16.         Cache cache manager.getCache("UserCache");       
          17.       
          18.         while(true      
          19.             Element cache.get("key1");       
          20.             if(e != null      
          21.                 System.out.println(e.getValue());       
          22.                 break      
          23.                   
          24.             Thread.sleep(1000);       
          25.               
          26.           
          27.       
          28.     

          posted on 2016-06-19 00:22 youngturk 閱讀(178) 評論(0)  編輯  收藏 所屬分類: 筆試題

          <2016年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導航

          統計

          公告

          this year :
          1 jQuery
          2 freemarker
          3 框架結構
          4 口語英語

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          EJB學習

          Flex學習

          learn English

          oracle

          spring MVC web service

          SQL

          Struts

          生活保健

          解析文件

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 涪陵区| 渭南市| 东阳市| 丰县| 安龙县| 巴林左旗| 金乡县| 永州市| 洪江市| 社旗县| 内乡县| 吴桥县| 临泽县| 广德县| 汾阳市| 桂平市| 海城市| 吉隆县| 泸溪县| 同仁县| 桐庐县| 尼玛县| 东乌珠穆沁旗| 彭阳县| 长春市| 洪湖市| 湄潭县| 应用必备| 铁力市| 武宣县| 东兴市| 盐亭县| 娄烦县| 吕梁市| 门源| 保亭| 连南| 宜良县| 阿图什市| 晋州市| 绿春县|