<?xmlversion="1.0"encoding="UTF-8"?>

<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="ehcache.xsd"

updateCheck="true"monitoring="autodetect"

dynamicConfig="true">

<cacheManagerPeerProviderFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=automatic,

multicastGroupAddress=230.0.0.1,

multicastGroupPort=4446

timeToLive=1"/>

<cacheManagerPeerListenerFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>

<cachename="Cache1"

maxElementsInMemory="100"

eternal="true"

overflowToDisk="false"

memoryStoreEvictionPolicy="LFU">

<cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>

</cache>

</ehcache>


PS:

a、配置簡單,每個tomcat使用完全相同的ehcache配置;

b、通過多播( multicast )來維護集群中的所有有效節點。這也是最為簡單而且靈活的方式,與手工模式不同的是,每個節點上的配置信息都相同,大大方便了節點的部署,避免人為的錯漏出現。

c、timeToLive的值指的是數據包可以傳遞的域或是范圍。約定如下:

0是限制在同一個服務器

1是限制在同一個子網

32是限制在同一個網站

64是限制在同一個region

128是限制在同一個大洲

255是不限制

在Java實現中默認值是1,也就是在同一個子網中傳播。改變timeToLive屬性可以限制或是擴展傳播的范圍。

d、自動的peer discovery與廣播息息相關。廣播可能被路由阻攔,像Xen和VMWare這種虛擬化的技術也可以阻攔廣播(阿里云主機好像也不提供廣播,阿里云服務器上部署時可采用手動配置成員發現)。

如果這些都打開了,你可能還在要將你的網卡的相關配置打開。一個簡單的辦法可以告訴廣播是否有效,那就是使用ehcache remote debugger來看“心跳”是否可用。

 

方式二:手動配置發現集群成員

ehcache.xml配置如下:


<?xmlversion="1.0"encoding="UTF-8"?>

<ehcachexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="ehcache.xsd"

updateCheck="true"monitoring="autodetect"

dynamicConfig="true">

<!–RMI方式二:手動成員發現配置:ip 端口號,手動指定需要同步的server和cachename–>

<cacheManagerPeerProviderFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=manual,

rmiUrls=

//192.168.178.101:50001/Cache1|

//192.168.178.101:50001/Cache2|

//192.168.178.102:40001/Cache1|

//192.168.178.102:40001/Cache2|

//192.168.178.102:50001/Cache1|

//192.168.178.102:50001/Cache2"/>

<cacheManagerPeerListenerFactory

class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

properties="hostName=192.168.178.101,port=40001,socketTimeoutMillis=2000"/>

<cachename="Cache1"

maxElementsInMemory="1000"

eternal="true"

overflowToDisk="false"

memoryStoreEvictionPolicy="LFU">

<cacheEventListenerFactory

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

properties="

replicateAsynchronously=true,

replicatePuts=true,

replicateUpdates=true,

replicateUpdatesViaCopy=false,

replicateRemovals=true"/>

<!–用于在初始化緩存,以及自動設置–>

<bootstrapCacheLoaderFactoryclass="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>

</cache>

<cachename="Cache2"

maxElementsInMemory="2000"

eternal="true"

overflowToDisk="false"

memoryStoreEvictionPolicy="LFU">

<cacheEventListenerFactory

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

properties="

replicateAsynchronously=true,

replicatePuts=true,

replicateUpdates=true,

replicateUpdatesViaCopy=false,

replicateRemovals=true"/>

<!–用于在初始化緩存,以及自動設置–>

<bootstrapCacheLoaderFactoryclass="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>

</cache>

</ehcache>


PS:

a、每個tomcat的配置文件都不一樣,部署集群時有點繁瑣;

b、配置cacheManagerPeerProviderFactory,rmiUrls配置需要同步的各個集群節點列表(不包括本服務器節點);

上面示例配置文件是tomcat1的配置,rmiUrls列表配置tomcat2,tomcat3,tomcat4需要同步的緩存項,每個緩存項的配置格式://server:port/cacheName

同理,tomcat2的配置中,rmiUrls列表就需要配置tomcat1,tomcat3,tomcat4的緩存項;tomcat3,tomcat4以此類推;

c、配置cacheManagerPeerListenerFactory,本節點的緩存監聽配置,屬性中需指定本節點IP或域名、監聽端口號、socket通信超時時間

hostName=192.168.178.101,port=40001,socketTimeoutMillis=2000

同理:tomcat2配置:hostName=192.168.178.101,port=50001,socketTimeoutMillis=2000

tomcat3配置:hostName=192.168.178.102,port=40001,socketTimeoutMillis=2000

tomcat4配置:hostName=192.168.178.102,port=50001,socketTimeoutMillis=2000

d、配置具體的cache,需要配置cacheEventListenerFactory,指定哪些操作時需要replicate cache(同步復制緩存)

replicatePuts=true | false – 當一個新元素增加到緩存中的時候是否要同步復制到其他的peers. 默認是true。
replicateUpdates=true | false – 當一個已經在緩存中存在的元素被覆蓋更新時是否要進行復制。默認是true。
replicateRemovals= true | false – 當元素移除的時候是否進行復制。默認是true。
replicateAsynchronously=true | false – 復制方式是異步的(指定為true時)還是同步的(指定為false時)。默認是true。
replicateUpdatesViaCopy=true | false – 當一個元素被拷貝到其他的cache中時是否進行復制(指定為true時為復制),默認是true。

Ehcahce官方文檔中的描述:

Thefactoryrecognisesthefollowingproperties:

replicatePuts=true|false-whethernewelementsplacedinacachearereplicatedtoothers.Defaultstotrue.

replicateUpdates=true|false-whethernewelementswhichoverrideanelementalreadyexistingwiththesamekeyarereplicated.Defaultstotrue.

replicateRemovals=true-whetherelementremovalsarereplicated.Defaultstotrue.

replicateAsynchronously=true|false-whetherreplicationsareasyncrhonous(true)orsynchronous(false).Defaultstotrue.

replicateUpdatesViaCopy=true|false-whetherthenewelementsarecopiedtoothercaches(true),orwhetheraremovemessageissent.Defaultstotrue.

 

調用Ehcahe提供的API,編碼緩存的加載及獲取,更新

引入jar包:ehcache.jar

放入緩存的對象必須是可序列化的,即必須實現接口Serializable

 

示例代碼:


publicclassImeiWhiteListCacheHelper{

privatestaticfinalStringIMEI_CACHE="IMEICache";

privatestaticImeiWhiteListDAOimeiDao=newImeiWhiteListDAO();

privatestaticCachecache;

static{

cache=CacheManager.getInstance().getCache(IMEI_CACHE);

}

/**

*重新加載IMEI白名單到緩存中

*

*@throwsSQLException

*/

publicstaticvoidreloadImeiWhiteListToCache()throwsSQLException{

synchronized(cache){

cache.removeAll();

List<ImeiWhiteListDTO>list=imeiDao.getAllImeiWhiteList();

for(ImeiWhiteListDTOimeiWhiteListDTO:list){

Elemente=newElement(imeiWhiteListDTO.getImei(),imeiWhiteListDTO);

cache.put(e);

}

}

}

/**

*從緩存中獲取某個IMEI的信息。如緩存獲取失敗,從DB中讀取,再放入緩存

*

*@paramimei

*@return

*@throwsSQLException

*/

publicstaticImeiWhiteListDTOgetImeiInfo(Stringimei)throwsSQLException{

ImeiWhiteListDTOimeiInfo=null;

synchronized(cache){

Elementelement=cache.get(imei);

if(element!=null){

//System.out.println("hitcount:" element.getHitCount());

imeiInfo=(ImeiWhiteListDTO)element.getObjectValue();

}else{

imeiInfo=imeiDao.getModelByIMEI(imei);

if(imeiInfo!=null){

cache.put(newElement(imeiInfo.getImei(),imeiInfo));

}

}

}

returnimeiInfo;

}


 

更多關于云服務器域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn

贊(0)
聲明:本網站發布的內容(圖片、視頻和文字)以原創、轉載和分享網絡內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。郵箱:3140448839@qq.com。本站原創內容未經允許不得轉載,或轉載時需注明出處:三五互聯知識庫 » Ehcache 一臺服務器多個Tomcat組成的集群間的緩存同步實踐

登錄

找回密碼

注冊

主站蜘蛛池模板: 日韩人妻精品中文字幕| 蜜芽久久人人超碰爱香蕉| 精品av无码国产一区二区| 2019国产精品青青草原| 亚洲性夜夜天天天| 精品午夜福利在线视在亚洲| 国产精品天干天干综合网| 97一期涩涩97片久久久久久久| 永吉县| 国产精品区视频中文字幕| 国产精品白丝一区二区三区 | 日韩中文字幕有码午夜美女| xbox免费观看高清视频的软件| 久青草视频在线观看免费| 新疆| 丁香婷婷在线观看| 被灌满精子的波多野结衣| 亚洲 中文 欧美 日韩 在线| 麻豆一区二区三区精品蜜桃| xbox免费观看高清视频的软件 | 少妇粗大进出白浆嘿嘿视频| 日本伊人色综合网| 国内精品久久久久久久coent| 午夜国产福利片在线观看| 扒开双腿猛进入喷水高潮叫声| 欧美 亚洲 日韩 在线综合| 亚洲日本韩国欧美云霸高清| 制服丝袜国产精品| 真人性囗交视频| 国产老熟女视频一区二区| 国产精品 自在自线| 依安县| 国产很色很黄很大爽的视频| 日韩人妻精品中文字幕| 国产日产欧产美韩系列麻豆| 亚洲老熟女一区二区三区| 国产免费一区二区三区在线观看 | 天堂av资源在线免费| 国产成AV人片久青草影院| 日韩亚洲国产中文永久| 日本一卡二卡不卡视频查询|