系統(tǒng)環(huán)境:win7
部署環(huán)境:apache2.2 tomcat4
應(yīng)用程序版本:spring3.2 ehcache2.6
1:tomcat 負載和集群配置
1.1 模型圖
[img]
[/img]
tomcat負載需要apache協(xié)助,apache啟的作用就是,接受client請求,調(diào)度tomcat.
各個tomcat分載請求,均衡流量,降低web server壓力。通過tomcat cluster 可以實現(xiàn)各tomcat數(shù)據(jù)同步和共享。
1.2 配置
??? 解壓兩個統(tǒng)一版本的tomcat 分別命名為apache-tomcat-6080 和apache-tomcat-7080
?? 修改6080tomcat srever。xml
修改7080tomcat srever。xml
?? 配置apache
?? 將mod_jk-1.2.31-httpd-2.2.3.so 復(fù)制到apache moudles 中。
??? 在conf中創(chuàng)建mod_jk.conf 和workers.properties。
??? mod_jk.conf? 主要定義mod_jk模塊的位置以及mod_jk模塊的連接日志設(shè)置,還有定義worker.properties文件的位置。
???
??
??? workers.properties 定義worker的參數(shù),主要是連接tomcat主機的地址和端口信息。如果Tomcat與apache不在同一臺機器上,或者需要做多臺機器上tomcat的負載均衡只需要更改workers.properties文件中的相應(yīng)定義即可。
???
??
將conf/httpdf.conf 文件末尾追加:
2 ehcache 與 spring-cache 整合
? 2.1 添加ehcache.xml
cacheManagerPeerProviderFactory 其他tomcat 多個url 可以用,分割
cacheManagerPeerListenerFactory 當前tomcat
部署時,需要根據(jù)tomcat 集群監(jiān)聽端口 修改各自的ehcache.xml配置.
applictionContext.xml 配置
spring 注解緩存使用
部署環(huán)境:apache2.2 tomcat4
應(yīng)用程序版本:spring3.2 ehcache2.6
1:tomcat 負載和集群配置
1.1 模型圖
[img]
[/img]
tomcat負載需要apache協(xié)助,apache啟的作用就是,接受client請求,調(diào)度tomcat.
各個tomcat分載請求,均衡流量,降低web server壓力。通過tomcat cluster 可以實現(xiàn)各tomcat數(shù)據(jù)同步和共享。
1.2 配置
??? 解壓兩個統(tǒng)一版本的tomcat 分別命名為apache-tomcat-6080 和apache-tomcat-7080
?? 修改6080tomcat srever。xml
<Server port="6005" shutdown="SHUTDOWN">
<Connector port="6080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="6443"
useBodyEncodingForURI ="true"
URIEncoding="UTF-8"
/>
<Connector port="6009" protocol="AJP/1.3" redirectPort="6443" />
<!--jvmRoute 是apache 檢測通道名-->
<Engine name="Catalina" defaultHost="localhost"
jvmRoute="tomcat1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<!--port 不同tomcat 設(shè)置不同-->
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4001"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
修改7080tomcat srever。xml
<Server port="7005" shutdown="SHUTDOWN">
<Connector port="7080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="7443"
useBodyEncodingForURI ="true"
URIEncoding="UTF-8"
/>
<Connector port="7009" protocol="AJP/1.3" redirectPort="7443" />
<!--jvmRoute 是apache 檢測通道名-->
<Engine name="Catalina" defaultHost="localhost"
jvmRoute="tomcat1">
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<!--port 不同tomcat 設(shè)置不同-->
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4002"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
?? 配置apache
?? 將mod_jk-1.2.31-httpd-2.2.3.so 復(fù)制到apache moudles 中。
??? 在conf中創(chuàng)建mod_jk.conf 和workers.properties。
??? mod_jk.conf? 主要定義mod_jk模塊的位置以及mod_jk模塊的連接日志設(shè)置,還有定義worker.properties文件的位置。
???
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JkWorkersFile conf/workers.properties
JkMount /*.* controller
??
??? workers.properties 定義worker的參數(shù),主要是連接tomcat主機的地址和端口信息。如果Tomcat與apache不在同一臺機器上,或者需要做多臺機器上tomcat的負載均衡只需要更改workers.properties文件中的相應(yīng)定義即可。
???
#server
worker.list = controller,tomcat1,tomcat2
#========tomcat1========
worker.tomcat1.port=6009
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=7009
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========controller,負載均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1
??
將conf/httpdf.conf 文件末尾追加:
Include conf/mod_jk.conf
2 ehcache 與 spring-cache 整合
? 2.1 添加ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="F:/eclipse_workspace/springmvc3.2/cache"/>
<defaultCache
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="1200"
overflowToDisk="true"/>
<cache name="user" maxElementsInMemory="150" eternal="false" timeToLiveSeconds="36000" timeToIdleSeconds="3600" overflowToDisk="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,replicateUpdatesViaCopy= true, replicateRemovals= true " />
<!-- <cacheEventListenerFactory class="cn.com.dwsoft.test.ehcache.EventFactory" /> -->
</cache>
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//127.0.0.1:40002/user"/>
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=127.0.0.1,port=40001,socketTimeoutMillis=2000"/>
</ehcache>
cacheManagerPeerProviderFactory 其他tomcat 多個url 可以用,分割
cacheManagerPeerListenerFactory 當前tomcat
部署時,需要根據(jù)tomcat 集群監(jiān)聽端口 修改各自的ehcache.xml配置.
applictionContext.xml 配置
<!-- cacheManager工廠類 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
p:shared="false" />
<!-- 聲明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory"/>
spring 注解緩存使用
package com.cn.ld.modules.user.service.impl;
import java.util.Arrays;
@EnableCaching
@Service
public class UserServiceImpl implements UserService ,UserDetailsService{
private User[] users = null;
{
users = new User[5];
users[0] = new User("java小生0", "111111", 21, '男');
users[1] = new User("java小生1", "222222", 22, '男');
users[2] = new User("java小生2", "333333", 23, '男');
users[3] = new User("java小生3", "444444", 24, '男');
users[4] = new User("java小生4", "555555", 25, '女');
}
@Cacheable(value = "user" , condition="#id < 3" ,unless="#result.age == 25")
public User findById(int id) {
System.out.println("--------findById start-----------");
System.out.println("agrs{id:"+id+"}");
User u = Arrays.asList(users).get(id);
System.out.println("--------findById end-----------");
return u;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
{
User user = new User(username, "111111", 25, '男');
return user ;
}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

