运行环境:
系统:Windows 7
web服务器:jboss-4.2.2.GA,apache2.2(做负载均衡)
java运行环境:jdk1.6 64位
节点主机:31.0.240.55(node1)和31.0.240.56(node2)
开发依赖库:
缓存Ehcache:ehcache1.5.0.jar
示例项目:window.war
部署:
1.将jboss分别安装到节点主机的固定位置,保证集群环境可以正常运行,包括session复制均已配好。【集群不作详解】
2.将window.war分别拷贝到%JBOSS_HOME%\server\all\deploy【%JBOSS_HOME%为jboss-4.2.2.GA所在目录位置】
3.在window.war中修改WEB-INF目录下的web.xml文件,需要添加<distributable/>示例:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Window</display-name>
<distributable/>
。。。。。。。。。
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/frame/error_500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/frame/error_500.jsp</location>
</error-page>
<jsp-config>
<taglib>
<taglib-uri>webwork</taglib-uri>
<taglib-location>/WEB-INF/webwork.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
在window.war中修改WEB-INF目录下添加jboss-web.xml文件,示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web
PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>
<context-root>/</context-root>
<replication-config>
<replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-trigger>
<replication-granularity>SESSION</replication-granularity>
<replication-field-batch-mode>true</replication-field-batch-mode>
</replication-config>
</jboss-web>
【注:两个节点配置都做相应配置】(此是window.war集群配置的部分)
4.开始配置ehcache,在class文件夹下找到ehcache.xml文件,(使用单播方式)示例如下:
a.此为节点31.0.240.55中window.war中ehcache.xml文件配置。
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="defaultCacheDefine" maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals=true"/>
</cache>
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="10000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals=true"/>
</cache>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals=true"/>
</cache>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=31.0.240.55,port=4080,socketTimeoutMillis=8000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//31.0.240.56:4080/defaultCacheDefine|//31.0.240.56:4080/org.hibernate.cache.UpdateTimestampsCache|//31.0.240.56:4080/org.hibernate.cache.StandardQueryCache" />
</ehcache>
b.此为节点31.0.240.56 中window.war中ehcache.xml文件配置。
<ehcache>
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />
<cache name="defaultCacheDefine" maxElementsInMemory="10000" eternal="false"
overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals=true"/>
</cache>
<!--
class – 使用net.sf.ehcache.distribution.RMICacheReplicatorFactory
这个工厂支持以下属性:
replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers. 默认是true。
replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。
replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。
replicateAsynchronously=true | false – 复制方式是异步的(指定为true时)还是同步的(指定为false时)。默认是true。
replicatePutsViaCopy=true | false – 当一个新增元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制(指定为true时为复制),默认是true。
-->
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="10000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals=true"/>
</cache>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000" eternal="false" overflowToDisk="true"
timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=false, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals=true"/>
</cache>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=31.0.240.56,port=4080,socketTimeoutMillis=8000" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//31.0.240.55:4080/defaultCacheDefine|//31.0.240.55:4080/org.hibernate.cache.UpdateTimestampsCache|//31.0.240.55:4080/org.hibernate.cache.StandardQueryCache" />
<!--
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=31.0.240.55,
multicastGroupPort=40802, timeToLive=32" />
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=224.0.0.1, multicastGroupPort=4446,timeToLive=255"/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost,socketTimeoutMillis=2000"/>-->
</ehcache>
5.由于ehcache分布式需要使用RMI服务,默认情况下只能本地访问RMI服务,远程机器无法访问,通过设置jboss启动参数可以设置:
示例启动方式:打开cmd,进入jboss的bin目录下,执行:run -c all -Djava.rmi.server.hostname=31.0.240.55 -Dremoting.bind_by_host=false
如果不加这个启动参数当远程调用时会出现拒绝访问的异常,只能本地访问。