Coherence企业级缓存(五)与Hibernate集成(1)

Oracle Coherence 是一个面向企业级应用的分布式缓存框架,看过它的简单介绍后,感觉是:很好很强大。
Hibernate 封装了统一的 Cache接口 CacheProvider ,可以方便的集成实现该接口的第三方Cache框架。

本文就不详细介绍 Coherence 的特点和优势了,感兴趣或要使用的直接到Oracle网站去查询。 [url=http://www.oracle.com/products/middleware/coherence/index.html]http://www.oracle.com/products/middleware/coherence/index.html[/url]
如何与Hibenate进行集成, Coherence的userguide只简单说了一下原理性的东东,没有具体实例。我仔细做了一个从头到尾的过程,直到Hibernate应用跑起来,并观察日志,确认Coherence缓存起作用为止。 以下是我记录下来的配置运行过程,供需要的同学参考。

[b]1. 运行前的说明[/b]
本 demo 客户端是Hibernate的数据操作代码,无外乎 session.update(),query.list() 等等。

测试准备两台物理机器 M1, M2 。M1 上跑Hibernate应用, 和1个Node; M2上准备跑 2 个Coherence 缓存节点(Node). 这里3个Node分散到两个 机器,主要是演示Coherence集群的自管理的强大功能。


[img]/upload/attachment/42720/e4c427b3-1651-3092-a207-fe6da4f102bf.jpg" alt="[/img]
原理简要说明:M2上的Node启动后,将自动加入Coherence 缓存集群(Cluster); 客户端Hibernate应用启动,执行后,由于也使用了Coherence, 它的查询操作将到 集群 上的三个Node的Cache中去取数据;只是第一次query会从数据库取数据,并放到Node的缓存中,以后各次query,findByPk 等操作,都会到Node的缓存中取。

[b]2. Cache服务端配置[/b]
2.1) 启动Node bat文件准备
Coherence开发包的目录很简单,只有 bin,doc,lib,examples 四个目录,原以为只要启动 coherence/bin/coherence.cmd 就可以了,后来发现没那么简单。

要让M2上的Node缓存Hibernate应用的数据,需要 org.hibernate.cache.QueryKey 等类,因此需要将 hibernate3.jar 加入classpath;
要缓存hibernate应用中的 com.xxx.system.perm.PermVO 等VO类,还必须将自己应用的 jar包也放到classpath中。
其他还需要一些相关的jar,如:dom4j,common-logging 等,最终编写了一个 runCache.bat 文件,做好必要的初始化工作:

runCache.bat

[quote]
rem Register cache region: org.hibernate.cache.StandardQueryCache
rem Register cache region: HIBERNATE_QUERY_CACHE

set java_opts=-Dtangosol.coherence.cacheconfig=hibernate-cache-config.xml

set classpath=lib/hibernate3.jar
set classpath=%classpath%;lib/hibernate3-sqlquerycache-fix.jar
set classpath=%classpath%;lib/dom4j-1.6.jar
set classpath=%classpath%;lib/commons-lang-2.1.jar
set classpath=%classpath%;lib/commons-logging.jar
set classpath=%classpath%;lib/cglib-2.1.jar
set classpath=%classpath%;lib/xxx-project.jar

coherence.cmd
[/quote]


我的coherence工作目录是

[quote]d:\coherenceNode\[/quote]


其中在我的工作目录下专门建了一个 lib 目录,存放需要的新的jar包,并将配置文件从 coherence-hibernate.jar/config 目录下移到工作目录下,目录结构是:

[quote]
lib
hibernate-cache-config.xml
runCache.bat
[/quote]

[img]/upload/attachment/42577/11dfaeba-8a74-39e3-9c3d-d60cb47dd2b8.jpg" alt="[/img]

这要,运行 runCache.bat 就可以启动一个 Coherence Node实例。本文在M2机器上开2个 cmd 窗口,启动2个实例, 在 M1 上启动一个 Node 实例。启动后的结果大致如:
[img]/upload/attachment/42575/57ba5d87-1175-38fb-ac4c-d7902d61bb9a.jpg" alt="" width="512" height="304[/img]

[quote]Group{Address=224.3.3.1, Port=33389, TTL=4}

MasterMemberSet
(
ThisMember=Member(Id=3, Timestamp=2008-10-14 17:41:25.062, Address
OldestMember=Member(Id=1, Timestamp=2008-10-14 17:39:30.125, Addre
ActualMemberSet=MemberSet(Size=3, BitSetCount=2
Member(Id=1, Timestamp=2008-10-14 17:39:30.125, Address=172.20.3
Member(Id=2, Timestamp=2008-10-14 17:41:03.234, Address=172.20.3
Member(Id=3, Timestamp=2008-10-14 17:41:25.062, Address=172.20.3
)
RecycleMillis=120000
RecycleSet=MemberSet(Size=0, BitSetCount=0
)[/quote]


2.2) 缓存配置
hibernate-cache-config.xml 是一个必要的缓存配置,本例使用 HibernateReplicatedCache 复制模式。
通过

[quote]set java_opts=-Dtangosol.coherence.cacheconfig=hibernate-cache-config.xml [/quote]
加入Java命令的启动参数中, coherence 会替换默认的配置。

还要修改 coherence 的缺省启动命令 coherence.cmd, 将java_opts 加入到原有的变量中,将

[quote]set java_opts="-Xms%memory% -Xmx%memory% -Dtangosol.coherence.distributed.localstorage=%storage_enabled%"[/quote]

修改为

[quote]set java_opts="-Xms%memory% -Xmx%memory% -Dtangosol.coherence.distributed.localstorage=%storage_enabled% %java_opts%"
[/quote]


并将 coherence-hibernate.jar 加入classpath中, 将

[quote]-cp "%coherence_home%\lib\coherence.jar"[/quote]

修改为

[quote]-cp "%coherence_home%\lib\coherence.jar;%coherence_home%\lib\coherence-hibernate.jar"
[/quote]

[url=http://raymond2006k.iteye.com/blog/256831]Coherence企业级缓存(一) 特点[/url]
[url=http://raymond2006k.iteye.com/blog/257376]Coherence企业级缓存(二) QuickStart和编程[/url]
[url=http://raymond2006k.iteye.com/blog/257384]Coherence企业级缓存(三) 四种缓存类型[/url]
[url=http://raymond2006k.iteye.com/blog/260406]Coherence企业级缓存(四) 数据管理模式 [/url]
[url=http://raymond2006k.iteye.com/blog/253262]Coherence企业级缓存(五)与Hibernate集成(1)[/url]
[url=http://raymond2006k.iteye.com/blog/252817]Coherence企业级缓存(五)与Hibernate集成(2) [/url]
[url=http://raymond2006k.iteye.com/blog/260420]Coherence企业级缓存(六) JMX 管理和监控[/url]
[url=http://raymond2006k.iteye.com/blog/260432]Coherence企业级缓存(七) 性能调优[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值