Ehcache Distributed cache cluster Environment configuration
Ehcache provides three network connectivity strategies to achieve clustering, Rmi,jgroup and JMS. At the same time, Ehcache can realize the cluster by multicast, and can also manually specify cluster host sequence to realize cluster.
Ehcache support for distributed cache support has three kinds of rmi,jgroups,jms, here are the following MRI and jgrpups two ways, Ehcache use version 1.5.0, for additional information on Ehcache please refer to http:// Ehcache.sourceforge.net/ehcacheuserguide.html,
Please refer to http://www.jgroups.org/manual/html_single/index.html for information about jgroups.
The environment is two machines server1 ip:192.168.2.154,server2 ip:192.168.2.23 1. RMI Mode:
RMI Way to configure the essentials (below are the configuration on the Server1, server2 only need to convert the IP)
A. Configure Peerprovider:
XML code
<cachemanagerpeerproviderfactory class= "Net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties= "Peerdiscovery=manual,rmiurls=//192.168.2.23:40001/usercache|//192.168.2.23:40001/resourcecache"/ >
The Usercache and Resourcecache in the Sever2 are synchronized manually in the configuration.
B. Configure Cachemanagerpeerlistener:
XML code
<cachemanagerpeerlistenerfactory class= "Net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
Properties= "hostname=192.168.2.154, port=40001,sockettimeoutmillis=2000"/>
Server1 listens for native 40001 ports in the configuration.
C. Add Cacheeventlistener to each cache, examples are as follows:
XML code
<cache name= "Usercache" maxelementsinmemory= "10000" eternal= "true" overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0" diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties= " Replicateasynchronously=true, Replicateputs=true, replicateupdates=true,replicateupdatesviacopy= false, replicateremovals= true "/>
</cache>
Attribute Explanation:
Must attribute:
Name: Sets the names of the caches, which are used to flag caching, the only
Maxelementsinmemory: The maximum number of objects in memory
Maxelementsondisk: The maximum number of objects in Diskstore, such as 0, is not restricted
Eternal: Sets whether the element is permanent, and if it is permanent, timeout ignores
Overflowtodisk: Whether to save to disk when the number in the memory reaches a limit
Optional properties:
Timetoidleseconds: Set the idle time before an element expires
Timetoliveseconds: Set the active time before an element expires
Diskpersistent: Whether the disk store is persisted when the virtual machine is started. Default to False
Diskexpirythreadintervalseconds: Time to run disk finalization thread defaults to 120 seconds
Memorystoreevictionpolicy: Strategies for Eviction
Cache child elements:
Cacheeventlistenerfactory: Registers the corresponding cache listener class for handling cached events, such as put,remove,update, and expire
Bootstrapcacheloaderfactory: Specifies the appropriate bootstrapcacheloader for the initialization of the cache, and for automatic setting.
Refer to another study note http://wozailongyou.javaeye.com/blog/230252, there are also clusters of notes 2. JGroups Way:
Ehcache 1.5.0 a way to support the version, configuration is simpler, the main points:
A. Configure the Peerprovider, using TCP, as the following example:
XML code
<cachemanagerpeerproviderfactory class= " Net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory "
Properties= "Connect=tcp (start_port=7800):
Tcpping (initial_hosts=192.168.2.154[7800],192.168.2.23[7800];p ort_range=10;timeout=3000;
Num_initial_members=3;up_thread=true;down_thread=true):
Verify_suspect (Timeout=1500;down_thread=false;up_thread=false):
Pbcast. Nakack (down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
Pbcast. GMS (Join_timeout=5000;join_retry_timeout=2000;shun=false;
Print_local_addr=false;down_thread=true;up_thread=true) "
Propertyseparator= "::"/>
B. Add Cacheeventlistener for each cache:
XML code
<cache name= "Usercache" maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
Jgroup configuration files on the two server configuration file, if you have more than one server, add the server IP in initial_hosts.
A complete Ehcache.xml file:
XML code
<?xml version= "1.0" encoding= "UTF-8"?>
<ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "Http://ehcache.sf.net/ehcache.xsd" >
<diskstore path= "Java.io.tmpdir"/>
<cachemanagerpeerproviderfactory class= " Net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory "
Properties= "Connect=tcp (start_port=7800):
Tcpping (initial_hosts=192.168.2.154[7800],192.168.2.23[7800];p ort_range=10;timeout=3000;
Num_initial_members=3;up_thread=true;down_thread=true):
Verify_suspect (Timeout=1500;down_thread=false;up_thread=false):
Pbcast. Nakack (down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
Pbcast. GMS (Join_timeout=5000;join_retry_timeout=2000;shun=false;
Print_local_addr=false;down_thread=true;up_thread=true) "
Propertyseparator= "::"/>
<defaultcache maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</defaultCache>
<cache name= "Velcrocache" maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
<cache name= "Usercache" maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
<cache name= "Resourcecache" maxelementsinmemory= "10000"
Eternal= "true" overflowtodisk= "true" timetoidleseconds= "0"
timetoliveseconds= "0" diskpersistent= "false"
Diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
</ehcache>
Ehcache Distributed cache cluster Environment configuration
Ehcache provides three network connectivity strategies to achieve clustering, Rmi,jgroup and JMS. At the same time, Ehcache can realize the cluster by multicast, and can also manually specify cluster host sequence to realize cluster.
Ehcache support for distributed cache support has three kinds of rmi,jgroups,jms, here are the following MRI and jgrpups two ways, Ehcache use version 1.5.0, for additional information on Ehcache please refer to http:// Ehcache.sourceforge.net/ehcacheuserguide.html,
Please refer to http://www.jgroups.org/manual/html_single/index.html for information about jgroups.
The environment is two machines server1 ip:192.168.2.154,server2 ip:192.168.2.23 1. RMI Mode:
RMI Way to configure the essentials (below are the configuration on the Server1, server2 only need to convert the IP)
A. Configure Peerprovider:
XML code
<cachemanagerpeerproviderfactory class= "Net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties= "Peerdiscovery=manual,rmiurls=//192.168.2.23:40001/usercache|//192.168.2.23:40001/resourcecache"/ >
The Usercache and Resourcecache in the Sever2 are synchronized manually in the configuration.
B. Configure Cachemanagerpeerlistener:
XML code
<cachemanagerpeerlistenerfactory class= "Net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
Properties= "hostname=192.168.2.154, port=40001,sockettimeoutmillis=2000"/>
Server1 listens for native 40001 ports in the configuration.
C. Add Cacheeventlistener to each cache, examples are as follows:
XML code
<cache name= "Usercache" maxelementsinmemory= "10000" eternal= "true" overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0" diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.RMICacheReplicatorFactory" properties= " Replicateasynchronously=true, Replicateputs=true, replicateupdates=true,replicateupdatesviacopy= false, replicateremovals= true "/>
</cache>
Attribute Explanation:
Must attribute:
Name: Sets the names of the caches, which are used to flag caching, the only
Maxelementsinmemory: The maximum number of objects in memory
Maxelementsondisk: The maximum number of objects in Diskstore, such as 0, is not restricted
Eternal: Sets whether the element is permanent, and if it is permanent, timeout ignores
Overflowtodisk: Whether to save to disk when the number in the memory reaches a limit
Optional properties:
Timetoidleseconds: Set the idle time before an element expires
Timetoliveseconds: Set the active time before an element expires
Diskpersistent: Whether the disk store is persisted when the virtual machine is started. Default to False
Diskexpirythreadintervalseconds: Time to run disk finalization thread defaults to 120 seconds
Memorystoreevictionpolicy: Strategies for Eviction
Cache child elements:
Cacheeventlistenerfactory: Registers the corresponding cache listener class for handling cached events, such as put,remove,update, and expire
Bootstrapcacheloaderfactory: Specifies the appropriate bootstrapcacheloader for the initialization of the cache, and for automatic setting.
Refer to another study note http://wozailongyou.javaeye.com/blog/230252, there are also clusters of notes 2. JGroups Way:
Ehcache 1.5.0 a way to support the version, configuration is simpler, the main points:
A. Configure the Peerprovider, using TCP, as the following example:
XML code
<cachemanagerpeerproviderfactory class= " Net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory "
Properties= "Connect=tcp (start_port=7800):
Tcpping (initial_hosts=192.168.2.154[7800],192.168.2.23[7800];p ort_range=10;timeout=3000;
Num_initial_members=3;up_thread=true;down_thread=true):
Verify_suspect (Timeout=1500;down_thread=false;up_thread=false):
Pbcast. Nakack (down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
Pbcast. GMS (Join_timeout=5000;join_retry_timeout=2000;shun=false;
Print_local_addr=false;down_thread=true;up_thread=true) "
Propertyseparator= "::"/>
B. Add Cacheeventlistener for each cache:
XML code
<cache name= "Usercache" maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
Jgroup configuration files on the two server configuration file, if you have more than one server, add the server IP in initial_hosts.
A complete Ehcache.xml file:
XML code
<?xml version= "1.0" encoding= "UTF-8"?>
<ehcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "Http://ehcache.sf.net/ehcache.xsd" >
<diskstore path= "Java.io.tmpdir"/>
<cachemanagerpeerproviderfactory class= " Net.sf.ehcache.distribution.jgroups.JGroupsCacheManagerPeerProviderFactory "
Properties= "Connect=tcp (start_port=7800):
Tcpping (initial_hosts=192.168.2.154[7800],192.168.2.23[7800];p ort_range=10;timeout=3000;
Num_initial_members=3;up_thread=true;down_thread=true):
Verify_suspect (Timeout=1500;down_thread=false;up_thread=false):
Pbcast. Nakack (down_thread=true;up_thread=true;gc_lag=100;retransmit_timeout=3000):
Pbcast. GMS (Join_timeout=5000;join_retry_timeout=2000;shun=false;
Print_local_addr=false;down_thread=true;up_thread=true) "
Propertyseparator= "::"/>
<defaultcache maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</defaultCache>
<cache name= "Velcrocache" maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
<cache name= "Usercache" maxelementsinmemory= "10000" eternal= "true"
Overflowtodisk= "true" timetoidleseconds= "0" timetoliveseconds= "0"
Diskpersistent= "false" diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
<cache name= "Resourcecache" maxelementsinmemory= "10000"
Eternal= "true" overflowtodisk= "true" timetoidleseconds= "0"
timetoliveseconds= "0" diskpersistent= "false"
Diskexpirythreadintervalseconds= ">"
<cacheeventlistenerfactory class= "Net.sf.ehcache.distribution.jgroups.JGroupsCacheReplicatorFactory"
Properties= "Replicateasynchronously=true, Replicateputs=true,
Replicateupdates=true, Replicateupdatesviacopy=false, replicateremovals=true "/>
</cache>
</ehcache>