The ehcache cluster uses the rmi method.

Source: Internet
Author: User

The ehcache cluster uses the rmi method.

There are several methods for ehcache clusters: rmi, jgroup, and jms. Here we will talk about the use of ehcache.

The reason why ehcache uses rmi to copy the cache is as follows:

1. rmi is the default Remote Mechanism of java.

2. You can optimize tcp options.

3. Because Elements must be stored on a disk, it must have been serialized. Therefore, you do not need to use xml to format anything.

4. You can use the Firewall

Cluster interaction diagram in rmi mode:

The rmi method of Ehcache is a point-to-point protocol, so it will generate a lot of Intranet Communication. Of course, Ehcache will solve this problem through an asynchronous batch replication mechanism.

To configure ehcache, You need to configure the elements.

PeerProvider

CacheManagerPeerListener

Configure the Provider. There are two methods: automatic discovery and manual configuration.

Automatic mode: the automatic discovery mode uses tcp broadcast to create and contain a broadcast group. It features a minimum configuration and automatic addition and Management of member groups. No server has a priority. The peer node sends heartbeat to the broadcast group every second. If a peer node is not sent within five seconds, the peer node is deleted. If a new peer node is added to the cluster

cacheManagerPeerProviderFactoryProperties has the following Configuration:

PeerDiscovery = automatic

MulticastGroupAddress = 230.0.0.1
MulticastGroupPort = 40001
TimeToLive = 0-255
HostName = hostname

PeerDiscovery mode: atutomatic is automatic; mulicastGroupAddress broadcast group address: 230.0.0.1; mulicastGroupPort broadcast group port: 40001; timeToLive indicates that the search range: 0 is the same server, and 1 is the same subnet, 32 refers to the same site, 64 refers to the same region, 128 is the same continent, and there is another 256, I will not talk about it; hostName: Host Name or ip address, interface used to receive or send information

In my experiment, the details are as follows:

 

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,    multicastGroupPort=4446, timeToLive=32,hostName=192.168.1.101" />

Of course, there is also a way to manually configure and paste the example, but it is not described.

Server1

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//server2:40001/sampleCache11|//server2:40001/sampleCache12"/>
Server2

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=manual,rmiUrls=//server1:40001/sampleCache11|//server1:40001/sampleCache12"/>

After configuring the method, you need to configure listener. Next, let's talk about Listener.

Listener is used to listen for information sent from the cluster.

The Listenner has two attributes: class and propertis.

Class A complete factory class name

Properties are well-separated attributes that are useful for facoprocessor.

The specific configuration of this experiment is as follows:

<cacheManagerPeerListenerFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="hostName=192.168.1.101, port=40001,socketTimeoutMillis=2000" />
HostName refers to the local host. Note that if localhost is used, it will only be valid for the local host. Use the IP address or host name in the subnet, port 40001, socketTimeoutMillis refers to the timeout time of the socket sub-module. The default value is 2000 ms. Note that the port can be the same between the two hosts. It is best to share the same, personal suggestion

Then configure the cache copy Replicators:

The local configuration is as follows:

<cache name="myCache" maxEntriesLocalHeap="10" eternal="false"timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="false"><cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true,asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory                  class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> </cache>
Name indicates the cache name. maxEntriesLocalHeap: specifies the maximum number of elements that can reside in the memory. The lifecycle of timeToLiveSeconds is 10000 s. overflowToDisk: Indicates whether to enable the disk when the memory is insufficient. This parameter is set to false, then it is asynchronous. In the put, update, copy, and remove operations, whether to copy data or not, and then the synchronization time is 1 s. The bootstrapCacheLoaderFactory indicates that data is synchronized at startup.

Complete configurations are as follows:

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"monitoring="autodetect" dynamicConfig="true"><diskStore path="D:/ehcache/diskStore" /><cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,    multicastGroupPort=4446, timeToLive=32,hostName=192.168.1.101" /><cacheManagerPeerListenerFactoryclass="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"properties="hostName=192.168.1.101, port=40001,socketTimeoutMillis=2000" /><cache name="myCache" maxEntriesLocalHeap="10" eternal="false"timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="false"><cacheEventListenerFactoryclass="net.sf.ehcache.distribution.RMICacheReplicatorFactory"properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, replicateRemovals=true,asynchronousReplicationIntervalMillis=1000" /> <bootstrapCacheLoaderFactory                  class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/> </cache></ehcache>

In server2, that is, 192.168.1.116, configure this address in hostName.

The following is the test code.

package com.ehcache;import java.io.IOException;import java.io.InputStream;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheException;import net.sf.ehcache.CacheManager;import net.sf.ehcache.Element;public class Test2 {   public static void main(String[] args) throws InterruptedException {     InputStream is=null;CacheManager manager=null;try {is = Test2.class.getResourceAsStream("/ehcache.xml");   manager = CacheManager.newInstance(is);} catch (CacheException e1) {try {if(is!=null){is.close();is=null;}} catch (IOException e) {e.printStackTrace();}e1.printStackTrace();}          Cache cache = manager.getCache("myCache");           Element element = new Element("client3" + System.currentTimeMillis(), "client3");         cache.put(element);         int i=0;       while (true)         {         Element element2 = new Element("client-3-"+i,i);            cache.put(element2);           Thread.sleep(3000);             System.out.println("\n");             for (Object key : cache.getKeys())             {                 System.out.println(key + ":" + cache.get(key).getObjectValue());             }             i++;       }      }}


Run the server2 test code to change the element to client-2 -.

Then paste the following picture:


Data synchronized from client3 to client2

The following is an algorithm used to describe the cache elimination of ehcache:

LRU is the Least Recently Used algorithm for Least Recently Used;

FIFO in a queue Mode

LFUleast frequently used is the least frequently used page replacement algorithm.

This is the project file: Click to open the link


Note: The author's level is limited, and these configurations have only been used in the LAN. If there is an error, please note.

I have also written and tested the separation of Server Load balancer, tomcat clusters, session calls in the LAN, and hot standby. I hope I will have the opportunity to build a large-scale cluster in the future.


Can ehcache be used for distributed? Can memcached have multiple backups?

Distributed cache can be implemented through RMI and pluggable APIs.
 
How does a common Java project use spring?

Use the eclipse tool, right-click the project, choose Properties> Java Build Path> Libraries> Add External JARs, and Add your own spring jar package.
Spring jar is described as follows:
(1) spring-core.jar
This jar file contains the basic core tool class of the Spring framework. Other Spring components must use the class in this package, which is the basic core of other components, of course, you can also use these tool classes in your application system.
(2) spring-beans.jar
This jar file is used by all applications. It contains all classes related to accessing the configuration file, creating and managing beans, and performing Inversion of Control/Dependency Injection (IoC/DI) operations. If the application only requires basic IoC/DI support, introduce the spring-core.jar and spring-beans. jar files.
(3) spring-aop.jar
This jar file contains the classes required to use Spring's AOP features in applications. Use Spring features based on AOP, such as Declarative Transaction Management, to include the jar package in the application.
(4) spring-context.jar
This jar file provides a large number of extensions for the Spring core. You can find all the classes required to use the Spring ApplicationContext feature, all the classes required by JDNI, And the classes used to integrate the UI with the Templating engines such as Velocity, FreeMarker, and JasperReports, and Related Classes for Validation.
(5) spring-dao.jar
This jar file contains all classes for data access by Spring DAO and Spring Transaction. To use declarative transaction support, you also need to include spring-aop.jar in your own applications.
Spring-hibernate.jar
This jar file contains all classes encapsulated by Spring for Hibernate 2 and Hibernate 3.
Spring-jdbc.jar
This jar file contains all classes that encapsulate Spring's JDBC data access.
Spring-orm.jar
This jar file contains Spring's extension of the DAO feature set to support iBATIS, JDO, OJB, and TopLink. Hibernate is already an independent package and is not included in this package. Most of the classes in this jar file depend on the classes in the spring-dao.jar, You need to include the spring-dao.jar package at the same time when using this package.
Spring-remoting.jar
This jar file contains classes supporting EJB, JMS, remote call Remoting (RMI, Hessian, Burlap, Http Invoker, JAX-RPC.
(10) spring-support.jar
This jar file contains classes that support Cache (ehcache), JCA, JMX, Mail service (Java Mail, COS Mail), and Task Plan Scheduling (Timer, Quartz.
Spring-web.jar
This jar file contains the core classes required to use the Spring framework during Web application development, including the class for automatically loading the WebApplicationContext feature, the Struts and JSF integration class, and the document ...... remaining full text>

Related Article

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.