tomcat 集群下ehcache 整合spring cache-annotation

1 篇文章 0 订阅
1 篇文章 0 订阅
系统环境:win7
部署环境:apache2.2 tomcat4
应用程序版本:spring3.2 ehcache2.6

1:tomcat 负载和集群配置
1.1 模型图

[img]
[img]http://dl2.iteye.com/upload/attachment/0086/4206/4922f694-3278-3bbf-9c6d-b33f9c0cac1f.png[/img]
[/img]

tomcat负载需要apache协助,apache启的作用就是,接受client请求,调度tomcat.
各个tomcat分载请求,均衡流量,降低web server压力。通过tomcat cluster 可以实现各tomcat数据同步和共享。
1.2 配置
解压两个统一版本的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 设置不同-->
<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 设置不同-->
<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 复制到apache moudles 中。
在conf中创建mod_jk.conf 和workers.properties。
mod_jk.conf 主要定义mod_jk模块的位置以及mod_jk模块的连接日志设置,还有定义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的参数,主要是连接tomcat主机的地址和端口信息。如果Tomcat与apache不在同一台机器上,或者需要做多台机器上tomcat的负载均衡只需要更改workers.properties文件中的相应定义即可。

#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
部署时,需要根据tomcat 集群监听端口 修改各自的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 ;
}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值