<?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、配置簡(jiǎn)單,每個(gè)tomcat使用完全相同的ehcache配置;

b、通過(guò)多播( multicast )來(lái)維護(hù)集群中的所有有效節(jié)點(diǎn)。這也是最為簡(jiǎn)單而且靈活的方式,與手工模式不同的是,每個(gè)節(jié)點(diǎn)上的配置信息都相同,大大方便了節(jié)點(diǎn)的部署,避免人為的錯(cuò)漏出現(xiàn)。

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

0是限制在同一個(gè)服務(wù)器

1是限制在同一個(gè)子網(wǎng)

32是限制在同一個(gè)網(wǎng)站

64是限制在同一個(gè)region

128是限制在同一個(gè)大洲

255是不限制

在Java實(shí)現(xiàn)中默認(rèn)值是1,也就是在同一個(gè)子網(wǎng)中傳播。改變timeToLive屬性可以限制或是擴(kuò)展傳播的范圍。

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

如果這些都打開了,你可能還在要將你的網(wǎng)卡的相關(guān)配置打開。一個(gè)簡(jiǎn)單的辦法可以告訴廣播是否有效,那就是使用ehcache remote debugger來(lái)看“心跳”是否可用。

 

方式二:手動(dòng)配置發(fā)現(xiàn)集群成員

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方式二:手動(dòng)成員發(fā)現(xiàn)配置:ip 端口號(hào),手動(dòng)指定需要同步的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"/>

<!–用于在初始化緩存,以及自動(dòng)設(shè)置–>

<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"/>

<!–用于在初始化緩存,以及自動(dòng)設(shè)置–>

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

</cache>

</ehcache>


PS:

a、每個(gè)tomcat的配置文件都不一樣,部署集群時(shí)有點(diǎn)繁瑣;

b、配置cacheManagerPeerProviderFactory,rmiUrls配置需要同步的各個(gè)集群節(jié)點(diǎn)列表(不包括本服務(wù)器節(jié)點(diǎn));

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

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

c、配置cacheManagerPeerListenerFactory,本節(jié)點(diǎn)的緩存監(jiān)聽配置,屬性中需指定本節(jié)點(diǎn)IP或域名、監(jiān)聽端口號(hào)、socket通信超時(shí)時(shí)間

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,指定哪些操作時(shí)需要replicate cache(同步復(fù)制緩存)

replicatePuts=true | false – 當(dāng)一個(gè)新元素增加到緩存中的時(shí)候是否要同步復(fù)制到其他的peers. 默認(rèn)是true。
replicateUpdates=true | false – 當(dāng)一個(gè)已經(jīng)在緩存中存在的元素被覆蓋更新時(shí)是否要進(jìn)行復(fù)制。默認(rèn)是true。
replicateRemovals= true | false – 當(dāng)元素移除的時(shí)候是否進(jìn)行復(fù)制。默認(rèn)是true。
replicateAsynchronously=true | false – 復(fù)制方式是異步的(指定為true時(shí))還是同步的(指定為false時(shí))。默認(rèn)是true。
replicateUpdatesViaCopy=true | false – 當(dāng)一個(gè)元素被拷貝到其他的cache中時(shí)是否進(jìn)行復(fù)制(指定為true時(shí)為復(fù)制),默認(rèn)是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.

 

調(diào)用Ehcahe提供的API,編碼緩存的加載及獲取,更新

引入jar包:ehcache.jar

放入緩存的對(duì)象必須是可序列化的,即必須實(shí)現(xiàn)接口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);

}

}

}

/**

*從緩存中獲取某個(gè)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;

}


 

更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)三五互聯(lián)官網(wǎng):www.shinetop.cn

贊(0)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享網(wǎng)絡(luò)內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。郵箱:3140448839@qq.com。本站原創(chuàng)內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明出處:三五互聯(lián)知識(shí)庫(kù) » Ehcache 一臺(tái)服務(wù)器多個(gè)Tomcat組成的集群間的緩存同步實(shí)踐

登錄

找回密碼

注冊(cè)

主站蜘蛛池模板: 一本本月无码-| 人妻精品久久无码区 | 精品成在人线av无码免费看| 国产精品aⅴ免费视频| 国产成人无码A区在线观| 国产精品自拍午夜福利| 国产稚嫩高中生呻吟激情在线视频| 阿城市| 亚洲熟女乱色综合亚洲图片| 亚洲av一本二本三本| 开心久久综合激情五月天| 国产精品成| 午夜爽爽爽男女免费观看影院| 天天躁夜夜躁狠狠喷水| 午夜福利影院不卡影院| 久久综合狠狠综合久久激情| 久久天天躁夜夜躁一区| 人妻性奴波多野结衣无码| 国产高清不卡视频| 玩两个丰满老熟女久久网 | 中文 在线 日韩 亚洲 欧美| 国产成人亚洲精品成人区| 成人午夜福利精品一区二区| 免青青草免费观看视频在线| 精品久久一线二线三线区| 国产老头多毛Gay老年男| 国产桃色在线成免费视频| 亚洲免费观看视频| 精品一区精品二区制服| 麻豆国产va免费精品高清在线| 久久一日本道色综合久久| 久久久亚洲欧洲日产国码二区| 国产高清国产精品国产专区 | 伊人成色综合人夜夜久久| 人妻另类 专区 欧美 制服| 亚洲国产成人无码影片在线播放| 新民市| 国产一区二区三区不卡视频| 99久久99久久精品国产片| 婷婷综合缴情亚洲| 久久精品国产亚洲成人av|