设为首页 收藏本站
查看: 438|回复: 0

[经验分享] tomcat 集群下ehcache 整合spring cache-annotation

[复制链接]

尚未签到

发表于 2017-1-31 12:31:40 | 显示全部楼层 |阅读模式
系统环境:win7
部署环境:apache2.2 tomcat4
应用程序版本:spring3.2 ehcache2.6
1:tomcat 负载和集群配置
1.1 模型图
[img]
DSC0000.png
[/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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-335734-1-1.html 上篇帖子: Tomcat虚拟目录的设定【分析与解决】 下篇帖子: zz如何改变tomcat的jvm内存大小
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表