`

EhCache集群(一)

阅读更多

还是找出时间,把EhCache集群总结下,希望能帮到需要它的人!

另外我的技术群,里面有很多跳槽机会,还有很多美女HR,只要你技术过硬,一定能通过我的群找到更适合,以及你心中的理想工作,我的Q群号:426554356,期待你的加入~

一 首先我们需要一个EhCache的配置文件(ehcacheTask.xml)

 

<?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="E:/ehcache/diskStore" />
  
  <!-- 配置属性清单 -->
  <!-- peerDiscovery=automatic 为自动
  	   mulicastGroupAddress 广播组地址:230.0.0.1
  	   mulicastGroupPort 广播组端口:40001
  	   timeToLive是指搜索范围:0是同一台服务器,1是同一个子网,32是指同一站点,64是指同一块地域,128是同一块大陆,还有个256
  	   hostName:主机名或者ip,用来接受或者发送信息的接口 
   -->
  <cacheManagerPeerProviderFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
    properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
      multicastGroupPort=40001, timeToLive=128,hostName=172.27.10.12" />
      <!-- 在server2 也就是 192.168.1.116 在hostName配置成此地址,就行了  -->
  
  <!-- 
  	   hostName指的是本机,这里注意如果使用的localhost,则只会对本机有效,请使用子网内的ip地址或者主机名
  	   port端口 40001
  	   socketTimeoutMillis是指socket子模块的超时时间,默认是2000ms,注意port两台主机可以相同可以不同。最好相同,个人建议
   -->
  <cacheManagerPeerListenerFactory
    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
    properties="hostName=172.27.10.12, port=40001,socketTimeoutMillis=2000" />
  
  <!-- 
  	   name为cache制定名字
  	   maxEntriesLocalHeap:内存中可驻留最大Element数量
  	   timeToLiveSeconds 生存周期 10000s
  	   overflowToDisk:当内存不足,是否启用磁盘
  	   给myCache价格监听,然后是异步方式,在put,update,copy,remove操作是否复制,然后同步时间1s,bootstrapCacheLoaderFactory 工厂是指启动是指一启动就同步数据
   -->
  <cache name="myCache" maxEntriesLocalHeap="1" eternal="false"
    timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="true">
    <cacheEventListenerFactory
      class="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>

 二 我们需要用Spring把EhCache管理起来 spring-context-cfg.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	                    http://www.springframework.org/schema/context
	                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
	                    http://www.springframework.org/schema/tx 
	                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
	                    http://www.springframework.org/schema/aop
	                    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	                    http://www.springframework.org/schema/task
	                    http://www.springframework.org/schema/task/spring-task-3.0.xsd
	                    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
	                    http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
	                    http://www.springframework.org/schema/cache
	                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
	default-autowire="byName">

	<import resource="classpath*:xxxxxx.xml" />

	<!-- 开启EhCache注解功能 -->
    <ehcache:annotation-driven /> 
	<!-- 去掉过期的elements -->
    <ehcache:config cache-manager="cacheManager"> 
		<ehcache:evict-expired-elements interval="60" /> 
	</ehcache:config>
	<!-- 导入EhCache管理的FactoryBean -->
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
		<property name="configLocation" value="ehcacheTask.xml" />
	</bean>

 仅此两个步骤!

下面是测试代码

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("/spring-context-cfg.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++;  
         }    
      }  
}  

 所依赖的jar

<dependencies>
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>3.3.1.GA</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>3.4.0.GA</version>
		</dependency>

		<!-- <dependency> -->
		<!-- <groupId>org.hibernate</groupId> -->
		<!-- <artifactId>hibernate-validator</artifactId> -->
		<!-- <version>3.0.0.ga</version> -->
		<!-- </dependency> -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.13</version>
		</dependency>

		<dependency>
			<groupId>com.travelsky.cupps</groupId>
			<artifactId>oracle</artifactId>
			<version>10.2.0.1.0</version>
		</dependency>

		<!-- spring -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>
		
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>

		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.2.2</version>
		</dependency>

		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.2</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-asl</artifactId>
			<version>1.9.10</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.10</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

		<!-- Begin 先保证支持 ehcache 集群内存双机热备份 demo ,售后整理筛选重复的jar版本 -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<version>2.7.0</version>
		</dependency>

		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-web</artifactId>
			<version>2.0.4</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.6.6</version>
		</dependency>
		<!-- End 先保证支持 ehcache 集群内存双机热备份 demo ,售后整理筛选重复的jar版本 -->

		<!-- Begin 先支持spring ehcache demo 缓存到数据库,售后整理筛选重复的jar版本 -->
		<dependency>
			<groupId>com.googlecode.ehcache-spring-annotations</groupId>
			<artifactId>ehcache-spring-annotations</artifactId>
			<version>1.1.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>

		<dependency>
			<groupId>javax.transaction</groupId>
			<artifactId>jta</artifactId>
			<version>1.1</version>
		</dependency>

<!-- 		<dependency> -->
<!-- 			<groupId>org.hibernate</groupId> -->
<!-- 			<artifactId>hibernate</artifactId> -->
<!-- 			<version>3.2.1.ga</version> -->
<!-- 		</dependency> -->

	<!--??  -->
		<dependency>
	        <groupId>javax.transaction</groupId>
	        <artifactId>transaction-api</artifactId>
	        <version>1.1</version>
	    </dependency>
	    <!-- ?? -->
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>

		<dependency>
			<groupId>aspectj</groupId>
			<artifactId>aspectjlib</artifactId>
			<version>1.5.3</version>
		</dependency>

		<dependency>
			<groupId>aspectj</groupId>
			<artifactId>aspectjtools</artifactId>
			<version>1.5.4</version>
		</dependency>
		<!-- End 先支持spring ehcache demo 缓存到数据库,售后整理筛选重复的jar版本 -->

 

分享到:
评论

相关推荐

    ehcache集群同步配

    ehcache集群同步配置实例加说明文档

    Ehcache集群实例

    1. **Ehcache集群**:Ehcache集群使得多台服务器上的多个Ehcache实例能够共享数据。这通过分布式缓存实现,其中的缓存项可以在集群中的任何节点上创建和查找。当一个节点更新缓存时,其他节点会自动同步,确保一致性...

    EHCACHE集群配置-JGroup篇

    EHCAHCE基于JGROUP的集群配置方案,内含相关配置文件,及配置说明

    ehcache集群方案

    在Ehcache集群方案中,一个关键组件是JGroups。JGroups是一个用于构建集群通信的框架,它允许节点之间进行可靠的消息传递。Ehcache利用JGroups来实现节点间的通信和数据同步,确保即使在某个节点失败时,数据也能在...

    Ehcache集群使用

    要搭建一个 Ehcache 集群,我们首先需要安装并启动 Terracotta 服务器。可以从官方网站下载最新版本的 Terracotta 服务器,并按照官方文档的指引进行安装。启动 Terracotta 服务器后,可以在管理界面监控集群的状态...

    ehcache集群

    1. **负载均衡**:Ehcache集群可以将数据分布在多个节点上,从而将负载分散到各个服务器,降低单个节点的压力。 2. **高可用性**:如果一个节点失效,其他节点仍然可以提供服务,确保系统连续运行。 3. **数据冗余**...

    Ehcache集群环境配置

    ### Ehcache集群环境配置 #### 一、Ehcache简介 Ehcache 是一款开源的高性能 Java 缓存框架,广泛应用于 Java 应用程序中,以提高应用程序性能。其核心设计采用三层类层次结构,主要包括 CacheManager、Cache 和 ...

    Ehcache通过Jgroups做集群

    Ehcache是一款高效、流行的Java缓存库,它允许应用程序快速访问经常使用的数据,从而提高性能和响应速度。在分布式环境中,为了实现数据共享和高可用性,Ehcache提供了集群功能。而Jgroups则是Java中一个强大的集群...

    Spring Boot 2.x基础教程:使用EhCache缓存集群.docx

    对于每个实例,我们需要创建一个特定的配置文件,例如`ehcache-1.xml`,`ehcache-2.xml`等,以适应集群环境。在配置文件中,我们需要指定`cacheEventListenerFactory`和`cacheManagerPeerProviderFactory`来实现缓存...

    ehcache集群缓存配置

    ehcache提供三种网络连接策略来实现集群,rmi,jgroup还有jms。这里只说rmi方式。同时ehcache可以可以实现多播的方式实现集群。也可以手动指定集群主机序列实现集群,本例应用手动指定。

    ehcache rmi集群demo

    在这个“ehcache rmi集群demo”中,我们将探讨如何将Ehcache与RMI结合,实现一个跨节点的缓存集群。 首先,Ehcache的核心概念包括缓存管理器(Cache Manager)、缓存(Cache)、缓存项(Cache Entry)等。缓存管理...

    ehcache使用,以及集群配置

    **Ehcache 使用详解与集群配置** Ehcache 是一个广泛使用的开源Java缓存系统,它提供了内存和磁盘存储,以及对缓存数据的分布式处理能力。在Java应用程序中,Ehcache能够显著提高性能,减少数据库负载,通过缓存...

    Ehcache 3(ehcache-clustered-3.8.1-kit.zip)

    TSA 是一个专门用于Ehcache集群的服务器,它提供了分布式锁服务、数据复制和故障检测等功能。当一个节点更新缓存时,TSA会确保这些更改被传播到集群中的其他节点,从而保持数据一致性。 Ehcache 3 集群支持的主要...

    EhCache 集群演示程序

    由于 JGroups 的包比较大,有两兆多,因此没有放到这个zip包里,请大家自行下载 JGroups 的jar包...只需要将解压后的 webapp 目录配置到tomcat下做为一个web应用即可进行测试。 测试方法详见 webapp/readme.txt 文件

    Ehcache远程复制

    描述部分提到的“Ehcache集群配置手册帮助你梳理Ehcache集群部署的配置”,意味着本手册旨在为开发者提供一个详细的指南,指导如何通过远程复制技术配置Ehcache集群,以实现缓存数据的一致性和同步。在分布式系统中...

    Ehcache RMI Replicated Cluster(RMI集群)

    Ehcache RMI Replicated Cluster 是一种分布式缓存解决方案,它使用远程方法调用(RMI)技术在多个节点之间复制数据,以实现高可用性和负载均衡。在大型分布式系统中,缓存是提高应用程序性能的关键组件,因为它可以...

    ehcache-clustered-3.3.1-kit.zip

    在"ehcache-clustered-3.3.1-kit.zip"这个压缩包中,包含了与Ehcache集群相关的组件和文档,具体包括以下内容: 1. Ehcache核心库:Ehcache的核心jar包(如`ehcache-core.jar`)包含了缓存管理器、缓存、事件监听器...

    ehcache3集群、ehcache-clustered、terracotta资源包

    ehcache3集群相关资源,包括ehcache-clustered-3.10.0-kit、ehcache-3.10.0.jar、ehcache-clustered-3.10.0.jar、terracotta-3.7.7.tar.gz、ehcache-transactions-3.10.0.jar

    集群环境中使用_EhCache_缓存系统&Ehcache配置文件的详细说明

    总结,EhCache在集群环境中的应用能够显著提升系统性能和可用性,而正确配置`ehcache.xml`文件是实现这一目标的关键。理解并熟练掌握EhCache的各项配置选项,可以帮助我们更好地利用缓存系统,优化应用性能。在实际...

    ehcache-clustered-3.8.1-kit.zip

    下面我们将深入探讨Ehcache集群化在3.8.1版本中的特点、功能以及如何配置和使用。 1. **Ehcache集群化原理** Ehcache使用分布式缓存策略,通过网络将缓存数据复制到多个节点上,确保数据的一致性和高可用性。在...

Global site tag (gtag.js) - Google Analytics