欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Ehcache一臺服務器多個Tomcat組成的集群間的緩存同步實踐

Tomcat集群部署情況說明

成都創(chuàng)新互聯(lián)公司長期為上千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為隴西企業(yè)提供專業(yè)的成都網(wǎng)站建設、成都網(wǎng)站制作,隴西網(wǎng)站改版等技術服務。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

兩臺Web服務器,每臺服務器部署兩個tomcat

server1 192.168.178.101

tomcat1 tomcat2

server2 192.168.178.102

tomcat3 tomcat4

采用Ehcache的RMI方式來實現(xiàn)緩存同步復制

方式一:自動發(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">

<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 )來維護集群中的所有有效節(jié)點。這也是最為簡單而且靈活的方式,與手工模式不同的是,每個節(jié)點上的配置信息都相同,大大方便了節(jié)點的部署,避免人為的錯漏出現(xiàn)。

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

0是限制在同一個服務器

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

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

64是限制在同一個region

128是限制在同一個大洲

255是不限制

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

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

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

方式二:手動配置發(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方式二:手動成員發(fā)現(xiàn)配置: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配置需要同步的各個集群節(jié)點列表(不包括本服務器節(jié)點);

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

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

c、配置cacheManagerPeerListenerFactory,本節(jié)點的緩存監(jiān)聽配置,屬性中需指定本節(jié)點IP或域名、監(jiān)聽端口號、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 – 當一個已經(jīng)在緩存中存在的元素被覆蓋更新時是否要進行復制。默認是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.

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

引入jar包:ehcache.jar

放入緩存的對象必須是可序列化的,即必須實現(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);

}

}

}

/**

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

}


網(wǎng)頁名稱:Ehcache一臺服務器多個Tomcat組成的集群間的緩存同步實踐
本文地址:http://chinadenli.net/article48/cgpghp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)搜索引擎優(yōu)化Google電子商務網(wǎng)站導航品牌網(wǎng)站設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

猜你還喜歡下面的內(nèi)容

綿陽服務器托管

App開發(fā)知識

行業(yè)網(wǎng)站建設