Ehcache cluster configuration

Source: Internet
Author: User

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
  1. <Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  2. Xsi: nonamespaceschemalocation = "ehcache. XSD">
  3. <Cachemanagerpeerproviderfactory
  4. Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerproviderfactory"
  5. Properties = "peerdiscovery = manual,
  6. Rmiurls = // 192.168.1.254: 40000/usercache "/>
  7. <Cachemanagerpeerlistenerfactory
  8. Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"
  9. Properties = "hostname = 192.168.1.126, Port = 40000, sockettimeoutmillis = 120000"/>
  10. <Defaultcache maxelementsinmemory = "10000" Eternal = "false"
  11. Timetoidleseconds = "120" timetoliveseconds = "120" overflowtodisk = "true"
  12. Diskspoolbuffersizemb = "30" maxelementsondisk = "10000000"
  13. Diskpersistent = "false" diskexpirythreadintervalseconds = "120"
  14. Memorystoreevictionpolicy = "LRU">
  15. <Cacheeventlistenerfactory
  16. Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
  17. </Defaultcache>
  18. <Cache name = "usercache" maxelementsinmemory = "1000" Eternal = "false"
  19. Timetoidleseconds = "100000" timetoliveseconds = "100000"
  20. Overflowtodisk = "false">
  21. <Cacheeventlistenerfactory
  22. Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
  23. </Cache>
  24. </Ehcache>

Tutorial/usingcachecluster

Java code
  1. Package tutorial;
  2. Import java.net. url;
  3. Import net. SF. ehcache. cache;
  4. Import net. SF. ehcache. cachemanager;
  5. Import net. SF. ehcache. element;
  6. Public class usingcachecluster {
  7. Public static void main (string [] ARGs) throws exception {
  8. URL url = usingcachecluster. Class. getclassloader (). getresource (
  9. "Config/ehcache_cluster.xml ");
  10. Cachemanager manager = new cachemanager (URL );
  11. // Obtain the cache
  12. Cache cache = manager. getcache ("usercache ");
  13. Element element = new element ("key1", "value1 ");
  14. Cache. Put (element );
  15. Element element1 = cache. Get ("key1 ");
  16. System. Out. println (element1.getvalue ());
  17. }
  18. }

2. configuration files and test code on host B

Config/ehcache_cluster.xml

XML/html code
  1. <Ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  2. Xsi: nonamespaceschemalocation = "ehcache. XSD">
  3. <Cachemanagerpeerproviderfactory
  4. Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerproviderfactory"
  5. Properties = "peerdiscovery = manual,
  6. Rmiurls = // 192.168.1.126: 40000/usercache "/>
  7. <Cachemanagerpeerlistenerfactory
  8. Class = "net. SF. ehcache. Distribution. rmicachemanagerpeerlistenerfactory"
  9. Properties = "hostname = 192.168.1.254, Port = 40000, sockettimeoutmillis = 120000"/>
  10. <Defaultcache maxelementsinmemory = "10000" Eternal = "false"
  11. Timetoidleseconds = "120" timetoliveseconds = "120" overflowtodisk = "true"
  12. Diskspoolbuffersizemb = "30" maxelementsondisk = "10000000"
  13. Diskpersistent = "false" diskexpirythreadintervalseconds = "120"
  14. Memorystoreevictionpolicy = "LRU">
  15. <Cacheeventlistenerfactory
  16. Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
  17. </Defaultcache>
  18. <Cache name = "usercache" maxelementsinmemory = "1000" Eternal = "false"
  19. Timetoidleseconds = "100000" timetoliveseconds = "100000"
  20. Overflowtodisk = "false">
  21. <Cacheeventlistenerfactory
  22. Class = "net. SF. ehcache. Distribution. rmicachereplicatorfactory"/>
  23. </Cache>
  24. </Ehcache>

Tutorial/usingcachecluster

Java code
  1. Package tutorial;
  2. Import java.net. url;
  3. Import net. SF. ehcache. cache;
  4. Import net. SF. ehcache. cachemanager;
  5. Import net. SF. ehcache. element;
  6. Public class usingcachecluster {
  7. Public static void main (string [] ARGs) throws exception {
  8. URL url = usingcachecluster. Class. getclassloader (). getresource (
  9. "Config/ehcache_cluster.xml ");
  10. Cachemanager manager = new cachemanager (URL );
  11. // Obtain the cache
  12. Cache cache = manager. getcache ("usercache ");
  13. While (true ){
  14. Element E = cache. Get ("key1 ");
  15. If (E! = NULL ){
  16. System. Out. println (E. getvalue ());
  17. Break;
  18. }
  19. Thread. Sleep (1000 );
  20. }
  21. }
  22. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.