<?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