java ehcache 分布式緩衝配置執行個體

來源:互聯網
上載者:User

引用: http://www.open-open.com/lib//view/open1342696876495.html   ehcache介紹 

下面我們動手通過項目來實踐下吧.[RMI方式];

基本環境:A 分別建立兩個web項目,C1和C2 分別倒入echcache的jar包;

                    B 本例使用了兩個tomcat 分別部署C1和C2 

項目配置:C1配置

      A 

ehcache.xml [ path= src ]

                   <ehcache updateCheck="false" dynamicConfig="false">
    <diskStore path="java.io.tmpdir/ehcache"/>
    <cache name="cache"
        maxElementsInMemory="19"
        eternal="false"
       timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true">
      
      <cacheEventListenerFactory
        class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
        properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true"/>
       
    </cache>
    
      <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.10.114, port=40001,socketTimeoutMillis=2000"/>
      <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,hostName=192.168.10.114,port=40002,rmiUrls=//192.168.10.114:40001/cache|//192.168.10.114:40002/cache,timeT
oLiv e=32" /> 
    <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
      />
    
</ehcache>

         這樣就配置了192.168.10.114:40001和192.168.10.114:40002兩個ehcache緩衝執行個體 並且每當其中一個緩衝放生改變時通過在

properties="replicateAsynchronously=true,replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false,replicateRemovals=true" 這些配置策略
可以同步反應到另一個緩衝中.從而達到分布式緩衝同步的效果.

B web.xml

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version="2.4">

<display-name>EhCache Cluster Demo</display-name>

<!-- listeners -->
<display-name>EhCache Cluster Demo</display-name>
<context-param>
<param-name>eccache</param-name>
<param-value>ehcache.xml</param-value>
</context-param>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

C. test.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<%@page import="net.sf.ehcache.CacheManager"%>
<%@page import="net.sf.ehcache.Element"%>
<%@page import="net.sf.ehcache.Cache"%>
<h1>EhCache Cluster Tester</h1>

<%
CacheManager m =CacheManager.create();
Cache c = m.getCache("cache");
c.put(new Element("999", "yy"));
out.print(c.getSize()+"__"+c.getKeys().toString());
for(Object o:c.getKeys()){
Object v = c.get(o).getValue();
out.println(o+":"+v);
}
%>


C2配置同P1一致.區別如下:

<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.10.114, port=40002,socketTimeoutMillis=2000"/>
    <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=manual,hostName=192.168.10.114,port=40001,rmiUrls=//192.168.10.114:40002/cache|//192.168.10.114:40001/cache,timeToLive=32" /> 


text.jsp 代碼自訂.


測試方法:

分別啟動兩個項目:localhost:8001/c1 和localhost:8002/c2 其中C2第一次向自己的緩衝中放入內容.然後你訪問c1 ,C1則直接從自己的緩衝中讀取內容顯示.

你可以不斷改變代碼進行測試.

***以上只為簡單實踐 對於其中參數的配置因地制宜的最佳化.如何任何錯誤。歡迎指正!

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

例外hibernate+ehcahce的配置基本同上 配置hibernate實用ehcache後 在代碼和hbf設定檔中可以指定使用緩衝[setCached(true)]

spring也提供了對ehcache的整合 .給予應用層級 可以緩衝調用方法的簽名(key).以及其放回結果(value)

ehcahe+Terracotta 可能提供了比較完整的解決方案[

Terracotta 收購了ehcache 而且本身Terracotta 提供了JVM層級的分布式緩衝]

這些都是後話.以後有計劃繼續實踐.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.