hibernate+ehcache 集群配置

经常在黑云中出没 2016-04-21 05:57:40
照着网上的方法,配了一下ehcache的集群。但发现,不起效果。改了一边,另外一台服务器上的缓存没改。

我贴一下配置。帮我看下什么问题,还是说因为我这个cache name,直接对应了一个实体,是不行的,需要实体里,再去写缓存处理的代码。rmiUrls里要指定缓存名称,那么defaultCache的怎么办。


<?xml version="1.0"?>
<ehcache updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir" />
<cache name="com.wxbusiness.entities.Distributor" maxElementsInMemory="3000" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true"
maxElementsOnDisk="10000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false,
replicateRemovals=true "/>
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
</cache>
<defaultCache name="dCache" maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false"
maxElementsOnDisk="10000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" >
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false,
replicateRemovals=true "/>
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
</defaultCache>
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="
socketTimeoutMillis=2000,
peerDiscovery=manual,
rmiUrls=//192.168.1.104:40002/com.wxbusiness.entities.Distributor|//192.168.1.104:40002/dCache"
/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.104,port=40001,socketTimeoutMillis=120000"/>

</ehcache>
...全文
455 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 23 楼 tianfang 的回复:
hibernate直接把多种/多个实体 放在一个ehcache的cache中。 不需要多个ehcache 的cache,代码中也不需要自己写对象到cache读写 简单说 hibernate+ehcache只是配置(含 annotation),基于hibernate的应用层代码中没有变化
嗯,好的,我现在把实体上的缓存注解中,都加一个region就好了
tianfang 2016-05-12
  • 打赏
  • 举报
回复
hibernate直接把多种/多个实体 放在一个ehcache的cache中。 不需要多个ehcache 的cache,代码中也不需要自己写对象到cache读写 简单说 hibernate+ehcache只是配置(含 annotation),基于hibernate的应用层代码中没有变化
tianfang 2016-05-12
  • 打赏
  • 举报
回复
hibernate+ehcache 集群 你要使用ehcache给hibernate做二级缓存吗?如果是,则要配置hibernate,然后再hibernate的entity对象层的做测试。 ehcache,hibernate的官方手册 http://www.ehcache.org/documentation/2.8/integrations/hibernate.html http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#performance-cache 第三方介绍,有测试代码 http://www.journaldev.com/2980/hibernate-ehcache-second-level-caching-example-tutorial
  • 打赏
  • 举报
回复
引用 13 楼 tianfang 的回复:
你多个节点的defaultCache 发一下啊 ,其中: cacheManagerPeerProviderFactory 配其他机器IP cacheManagerPeerListenerFactory 配置自己的IP,不要用localhost 看看你是不是这么配的
那是不是每个需要使用缓存的实体,都要ehcache.xml里去写一遍?我本来觉得,那些实体,只需要JAVA类中加了@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)。他们都会去用defaltCache。集群同步也会默认同步。
  • 打赏
  • 举报
回复
引用 18 楼 tianfang 的回复:
我一直没搞清楚你问题的要点 http://www.ehcache.org/ehcache.xml Default Cache configuration. These settings will be applied to caches created programmatically using CacheManager.add(String cacheName). This element is optional, and using CacheManager.add(String cacheName) when its not present will throw CacheException The defaultCache has an implicit name "default" which is a reserved cache name. defaultCache 是为CacheManager.add(String cacheName) 配置缺省参数的,不需要同步
那我就有点疑惑了,就是,我这边,只配了两个实体类的缓存,会同步的。其他的,都直接用默认的了,那就不用同步了。那么,我如果有20个实体,都用了缓存,我需要在ehcache.xml中,都写一遍吗?
  • 打赏
  • 举报
回复
引用 15 楼 tianfang 的回复:
你两个节点没有互相监听 当然不能同步了 192.168.1.104这个节点 cacheManagerPeerProviderFactory  192.168.1.123 cacheManagerPeerListenerFactory 192.168.1.104 192.168.1.123这个节点 cacheManagerPeerProviderFactory  192.168.1.104 cacheManagerPeerListenerFactory 192.168.1.123
这个互相监听配了的,要不然,其他两个cache怎么可以同步的,现在就是defaultCache不知道怎么同步
tianfang 2016-05-05
  • 打赏
  • 举报
回复
我一直没搞清楚你问题的要点 http://www.ehcache.org/ehcache.xml Default Cache configuration. These settings will be applied to caches created programmatically using CacheManager.add(String cacheName). This element is optional, and using CacheManager.add(String cacheName) when its not present will throw CacheException The defaultCache has an implicit name "default" which is a reserved cache name. defaultCache 是为CacheManager.add(String cacheName) 配置缺省参数的,不需要同步
tianfang 2016-05-04
  • 打赏
  • 举报
回复
你两个节点没有互相监听 当然不能同步了

192.168.1.104这个节点
cacheManagerPeerProviderFactory  192.168.1.123
cacheManagerPeerListenerFactory 192.168.1.104

192.168.1.123这个节点
cacheManagerPeerProviderFactory  192.168.1.104
cacheManagerPeerListenerFactory 192.168.1.123
tianfang 2016-05-04
  • 打赏
  • 举报
回复
你两个节点没有互相监听 当然不能同步了

192.168.1.104这个节点
cacheManagerPeerProviderFactory  192.168.1.123
cacheManagerPeerListenerFactory 192.168.1.104

192.168.1.123这个节点
cacheManagerPeerProviderFactory  192.168.1.104
cacheManagerPeerListenerFactory 192.168.1.123
  • 打赏
  • 举报
回复
引用 13 楼 tianfang 的回复:
你多个节点的defaultCache 发一下啊 ,其中: cacheManagerPeerProviderFactory 配其他机器IP cacheManagerPeerListenerFactory 配置自己的IP,不要用localhost 看看你是不是这么配的

<?xml version="1.0"?>
<ehcache updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

	<diskStore path="java.io.tmpdir" />
	<cache name="com.wxbusiness.entities.Product" maxElementsInMemory="3000" eternal="false"
		timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true"
		maxElementsOnDisk="10000" diskPersistent="false"
		diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> 
		<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
	        properties="replicateAsynchronously=true, 
			replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, 
			replicateRemovals=true "/>
		<bootstrapCacheLoaderFactory  
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=false"/>
	</cache>
	<cache name="com.wxbusiness.entities.Distributor" maxElementsInMemory="3000" eternal="false"
		timeToIdleSeconds="3600" timeToLiveSeconds="7200" overflowToDisk="true"
		maxElementsOnDisk="10000" diskPersistent="false"
		diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
		<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
	        properties="replicateAsynchronously=true, 
			replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, 
			replicateRemovals=true "/>
		<bootstrapCacheLoaderFactory  
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
	</cache>
	 
	<defaultCache name="defaultCache" maxElementsInMemory="1000" eternal="false"
		timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false"
		maxElementsOnDisk="10000" diskPersistent="false"
		diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" > 
		<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
	        properties="replicateAsynchronously=true, 
			replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, 
			replicateRemovals=true "/>
		<bootstrapCacheLoaderFactory  
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=false"/>
	</defaultCache>
	<cacheManagerPeerProviderFactory 
	    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" 
	    properties="
	    socketTimeoutMillis=20000,
	    peerDiscovery=manual,
	    rmiUrls=//192.168.1.123:40002/defaultCache|//192.168.1.123:40002/com.wxbusiness.entities.Product|//192.168.1.123:40002/com.wxbusiness.entities.Distributor"
	    propertySeparator="," 
	/>
	
	<cacheManagerPeerListenerFactory 
		class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
	 	properties="hostName=192.168.1.123,port=40001,socketTimeoutMillis=120000"/> 
</ehcache>
tianfang 2016-05-03
  • 打赏
  • 举报
回复
你多个节点的defaultCache 发一下啊 ,其中: cacheManagerPeerProviderFactory 配其他机器IP cacheManagerPeerListenerFactory 配置自己的IP,不要用localhost 看看你是不是这么配的
  • 打赏
  • 举报
回复
引用 11 楼 xiaoxiaoshuaige 的回复:
ehcache、habernate这种东西基本都不用了
因为以前的一个项目,现在要加一下集群。也懒得去换框架了。
  • 打赏
  • 举报
回复
引用 8 楼 tianfang 的回复:
把两个配置的defalut cache 贴出来看看

<defaultCache name="defaultCache" maxElementsInMemory="1000" eternal="false"
		timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false"
		maxElementsOnDisk="10000" diskPersistent="false"
		diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" > 
		<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
	        properties="replicateAsynchronously=true, 
			replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy=false, 
			replicateRemovals=true "/>
		<bootstrapCacheLoaderFactory  
                class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" properties="bootstrapAsynchronously=false"/>
	</defaultCache>
xiaoxiaoshuaige 2016-05-01
  • 打赏
  • 举报
回复
ehcache、habernate这种东西基本都不用了
tianfang 2016-04-26
  • 打赏
  • 举报
回复
cacheManagerPeerProviderFactory 配其他机器IP cacheManagerPeerListenerFactory 配置自己的IP,不要用localhost
tianfang 2016-04-26
  • 打赏
  • 举报
回复
把两个配置的defalut cache 贴出来看看
  • 打赏
  • 举报
回复
引用 3 楼 tianfang 的回复:
你直接把ehcache配置成hibernate的二级cache,各个服务器之间的ehcache使用jgroups同步
我现在自定义的cache可以同步了,defalut cache,也能自己同步吗?
  • 打赏
  • 举报
回复
引用 5 楼 tianfang 的回复:
hibernate配置文件中的配置: <property name="hibernateProperties"> <props> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.EhCacheRegionFactory </prop> bean的cahe设置: @Entity @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class Obj{
这些都没有问题啊,关键是ehcache.xml中的defalut cache,我现在同步不了,不知道要怎么配置过。自己定义的<cache name="xxx" 这种都是可以同步的。
tianfang 2016-04-24
  • 打赏
  • 举报
回复
还有rmi接口是否联通也要测试一下
tianfang 2016-04-24
  • 打赏
  • 举报
回复
hibernate配置文件中的配置: <property name="hibernateProperties"> <props> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.region.factory_class"> net.sf.ehcache.hibernate.EhCacheRegionFactory </prop> bean的cahe设置: @Entity @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class Obj{
加载更多回复(3)

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧