In this article, I will discuss the cluster configuration of ehcache and write two programs distributed on different logic machines to test whether the cluster effect has been achieved. My goal is to get the feeling that the configuration is successful.
I. cluster configuration
Ehcache provides three network connection policies to implement clusters, RMI, jgroup, and JMS. Here, we only use the RMI method. At the same time, ehcache can implement multicast to implement clusters. You can also manually specify the cluster host sequence to implement the cluster, which is manually specified in this example.
Let's talk about the problem here. It is not very difficult to look at the original example configuration in the distribution package. It should be easy to implement. But at the beginning, I implemented cluster configuration on my Linux host and my main operating system windows. In turn, the results failed. Then I found some other people's configuration experience on the Internet, which is actually a configuration segment without a complete instance file. The configuration fails for half a day. However, I suspect that my Linux system may not be properly configured in some places, so I will ignore it first. You have enabled another Windows host. Then deploy the program, and a test is successful. While I'm happy, I have to say, "Don't call code snippets an instance. This is very irresponsible ". At the same time, there is still a problem. The reason why the deployment is not successful in Linux needs to be identified.
Details: Configure cachemanagerpeerlistenerfactory to configure a listener for the host to discover synchronization requests sent from other hosts.
Configuring cachemanagerpeerproviderfactory is to specify a list of other hosts that provide synchronization in the network group other than itself, and separate different hosts with "|.
In the following example, the test process is as follows: the host B cache is enabled, and the elements whose key value is "key1" are cyclically crawled from the cache named usercache until the elements are obtained. Host a cache is started, and an element with the key value "key1" is put in the cache named usercache. Obviously, if the element obtained by host B proves that the synchronization is successful, that is, the cluster is successful.
Therefore, in the test process, start the test program of host B and start the test program of host.
The configuration file and test program are described as follows:
1. configuration file and test source code of host
Config/ehcache_cluster.xml
XML/html code
- <Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: nonamespaceschemalocation = "ehcache. XSD">
- <Cachemanagerpeerproviderfactory
- Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerproviderfactory"
- Properties = "peerdiscovery = manual,
- Rmiurls = // 192.168.1.254: 40000/usercache "/>
- <Cachemanagerpeerlistenerfactory
- Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"
- Properties = "hostname = 192.168.1.126, Port = 40000, sockettimeoutmillis = 120000"/>
- <Defaultcache maxelementsinmemory = "10000" Eternal = "false"
- Timetoidleseconds = "120" timetoliveseconds = "120" overflowtodisk = "true"
- Diskspoolbuffersizemb = "30" maxelementsondisk = "10000000"
- Diskpersistent = "false" diskexpirythreadintervalseconds = "120"
- Memorystoreevictionpolicy = "LRU">
- <Cacheeventlistenerfactory
- Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
- </Defaultcache>
- <Cache name = "usercache" maxelementsinmemory = "1000" Eternal = "false"
- Timetoidleseconds = "100000" timetoliveseconds = "100000"
- Overflowtodisk = "false">
- <Cacheeventlistenerfactory
- Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
- </Cache>
- </Ehcache>
Tutorial/usingcachecluster
Java code
- Package tutorial;
- Import java.net. url;
- Import net. SF. ehcache. cache;
- Import net. SF. ehcache. cachemanager;
- Import net. SF. ehcache. element;
- Public class usingcachecluster {
- Public static void main (string [] ARGs) throws exception {
- URL url = usingcachecluster. Class. getclassloader (). getresource (
- "Config/ehcache_cluster.xml ");
- Cachemanager manager = new cachemanager (URL );
- // Obtain the cache
- Cache cache = manager. getcache ("usercache ");
- Element element = new element ("key1", "value1 ");
- Cache. Put (element );
- Element element1 = cache. Get ("key1 ");
- System. Out. println (element1.getvalue ());
- }
- }
2. configuration files and test code on host B
Config/ehcache_cluster.xml
XML/html code
- <Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: nonamespaceschemalocation = "ehcache. XSD">
- <Cachemanagerpeerproviderfactory
- Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerproviderfactory"
- Properties = "peerdiscovery = manual,
- Rmiurls = // 192.168.1.126: 40000/usercache "/>
- <Cachemanagerpeerlistenerfactory
- Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"
- Properties = "hostname = 192.168.1.254, Port = 40000, sockettimeoutmillis = 120000"/>
- <Defaultcache maxelementsinmemory = "10000" Eternal = "false"
- Timetoidleseconds = "120" timetoliveseconds = "120" overflowtodisk = "true"
- Diskspoolbuffersizemb = "30" maxelementsondisk = "10000000"
- Diskpersistent = "false" diskexpirythreadintervalseconds = "120"
- Memorystoreevictionpolicy = "LRU">
- <Cacheeventlistenerfactory
- Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
- </Defaultcache>
- <Cache name = "usercache" maxelementsinmemory = "1000" Eternal = "false"
- Timetoidleseconds = "100000" timetoliveseconds = "100000"
- Overflowtodisk = "false">
- <Cacheeventlistenerfactory
- Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
- </Cache>
- </Ehcache>
Tutorial/usingcachecluster
Java code
- Package tutorial;
- Import java.net. url;
- Import net. SF. ehcache. cache;
- Import net. SF. ehcache. cachemanager;
- Import net. SF. ehcache. element;
- Public class usingcachecluster {
- Public static void main (string [] ARGs) throws exception {
- URL url = usingcachecluster. Class. getclassloader (). getresource (
- "Config/ehcache_cluster.xml ");
- Cachemanager manager = new cachemanager (URL );
- // Obtain the cache
- Cache cache = manager. getcache ("usercache ");
- While (true ){
- Element E = cache. Get ("key1 ");
- If (E! = NULL ){
- System. Out. println (E. getvalue ());
- Break;
- }
- Thread. Sleep (1000 );
- }
- }
- }