JAVAWEB部署在Terracotta集群上

                                             JAVAWEB部署在Terracotta集群上

          把web项目部署在Terracotta集群上时要考虑这个项目是否使用缓存。如果使用缓存了。就要先做好缓存跟Terracotta的集成。
          一般的缓存架构如Ehcache,Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
          所以这里我先做Ehcache + Terracotta的集成。
          在这里要提到的就是由于我使用的框架是ssm里面已经集成了shiro权限框架。shiro里提供了使用缓存的接口。所以只要使用好spring配置文件就可以做好相关类的配置。
          在这里其实要实现的就是shiro的分布式缓存。在Shiro中使用Enchache+Terracotta提供分布式缓存解决方案已相当成熟。所以大家可以自行百度学习一下。在配置缓存时需要做一些Terracotta配置。
缓存配置文件如
<?xml version="1.0" encoding="UTF-8"?>
 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
    xsi:noNamespaceSchemaLocation="ehcache.xsd">     
    <!--将内存放入缓存中-->
    <diskStore path="java.io.tmpdir" />

   <cacheManagerEventListenerFactory class="" properties=""/>  
   <!--terracotta服务器配置,默认端口为9510,多个服务器用,分隔  -->  
   <terracottaConfig url="localhost:9510"/>   
    <!-- 默认缓存 -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        overflowToDisk="true"
        diskSpoolBufferSizeMB="30"
        diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">  
    </defaultCache>

    <!--正式的缓存配置-->
<!--     <cache name="cache_sacm"
        maxElementsInMemory="100000"
        eternal="false"  
        memoryStoreEvictionPolicy="LRU"
        timeToIdleSeconds="100000"
        timeToLiveSeconds="100000"
        overflowToDisk="false"
        maxElementsOnDisk="0">  
        <terracotta clustered="true"/> 开启集群  
    </cache> -->
   <!--  系统缓存 -->
    <cache name="sysCache"
        maxElementsInMemory="100000"
        eternal="false"  
        memoryStoreEvictionPolicy="LRU"
        timeToIdleSeconds="100000"
        timeToLiveSeconds="100000"
        overflowToDisk="false"
        maxElementsOnDisk="0">  
        <terracotta clustered="true"/> <!-- 开启集群 -->  
    </cache>
            <!--  用户缓存 -->
        <cache name="userCache"
        maxElementsInMemory="100000"
        eternal="false"  
        memoryStoreEvictionPolicy="LRU"
        timeToIdleSeconds="100000"
        timeToLiveSeconds="100000"
        overflowToDisk="false"
        maxElementsOnDisk="0">  
        <terracotta clustered="true"/> <!-- 开启集群 -->  
    </cache>

    <!-- 工作流模块缓存 -->
        <cache name="actCache"
        maxElementsInMemory="100000"
        eternal="false"  
        memoryStoreEvictionPolicy="LRU"
        timeToIdleSeconds="100000"
        timeToLiveSeconds="100000"
        overflowToDisk="false"
        maxElementsOnDisk="0">  
        <terracotta clustered="true"/> <!-- 开启集群 -->  
    </cache>
    <!-- 内容管理模块缓存 -->
        <cache name="cmsCache"
        maxElementsInMemory="100000"
        eternal="false"  
        memoryStoreEvictionPolicy="LRU"
        timeToIdleSeconds="100000"
        timeToLiveSeconds="100000"
        overflowToDisk="false"
        maxElementsOnDisk="0">  
        <terracotta clustered="true"/> <!-- 开启集群 -->  
    </cache>
    <!-- 系统活动会话缓存 -->
        <cache name="activeSessionsCache" maxEntriesLocalHeap="10000"
        overflowToDisk="false" eternal="false" timeToLiveSeconds="100000"
        timeToIdleSeconds="0" diskPersistent="false"
        diskExpiryThreadIntervalSeconds="600">
         <terracotta clustered="true"/> <!-- 开启集群 -->
        </cache>
        <cache name="SimplePageCachingFilter" maxEntriesLocalHeap="100"
        eternal="false" overflowToDisk="false" timeToIdleSeconds="120" timeToLiveSeconds="120"
        memoryStoreEvictionPolicy="LFU">
        <terracotta clustered="true"/> <!-- 开启集群 -->
        </cache>
</ehcache>

如果你的Terracotta服务器端口不一样需要修改<terracottaConfig url="localhost:9510"/>的内容,写入相应的Terracotta服务器阵列的主机/端口。缓存参数里面如, activeSessionCache的diskPersistent或overflowToDisk属性都应该是false的,在群集配置中不支

持true。
相关spring配置文件如

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    default-lazy-init="true">

    <description>Spring Configuration</description>
    
    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:jeesite.properties" />
    
    <!-- 加载应用属性实例,可通过  @Value("#{APP_PROP['jdbc.driver']}") String jdbcDriver 方式引用 -->
    <util:properties id="APP_PROP" location="classpath:jeesite.properties" local-override="true"/>
    
    <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。  -->
    <context:component-scan base-package="com.thinkgem.jeesite"><!-- base-package 如果多个,用“,”分隔 -->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
     <!-- MyBatis begin -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.thinkgem.jeesite"/>
        <property name="typeAliasesSuperType" value="com.thinkgem.jeesite.common.persistence.BaseEntity"/>
        <property name="mapperLocations" value="classpath:/mappings/**/*.xml"/>
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
    </bean>
    
    <!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.thinkgem.jeesite"/>
        <property name="annotationClass" value="com.thinkgem.jeesite.common.persistence.annotation.MyBatisDao"/>
    </bean>
    
    <!-- 定义事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务  -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    <!-- MyBatis end -->
    
    <!-- 配置 JSR303 Bean Validator 定义 -->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

    <!-- 缓存配置 -->
     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:${ehcache.configFile}" />
    </bean>
    
        <!-- 缓存管理器 使用Ehcache实现 -->
  <!--   <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:${ehcache.configFile}"/>
    </bean> -->
    
    <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 -->
    <task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
    
    <!-- 数据源配置, 使用 BoneCP 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
        <property name="driverClassName" value="${jdbc.driver}" />
        
        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${jdbc.pool.init}" />
        <property name="minIdle" value="${jdbc.pool.minIdle}" />
        <property name="maxActive" value="${jdbc.pool.maxActive}" />
        
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />
        
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
        
        <property name="validationQuery" value="${jdbc.testSql}" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
        
        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat" />
    </bean>
    
    <!-- 数据源配置, 使用应用服务器的数据库连接池
    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/jeesite" />-->

    <!-- 数据源配置, 不使用连接池
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>-->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">      
        <property name="messageConverters">      
            <list >      
                <ref bean="mappingJacksonHttpMessageConverter" />      
            </list>      
        </property>      
    </bean>
    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">    
    <property name="supportedMediaTypes">    
        <list>    
            <value>application/json;charset=UTF-8</value>    
       </list>    
    </property>  
</bean>
</beans>

 这里其实还有相关的shiro的配置文件。因为shiro也是pojo所以可以很好的支持spring。配置好相关的bena.就可以实现分布式缓存了。
这里主要是讲缓存跟Terracotta的集成。所以shiro相关内容请自行学习相关知识。以上经过实际测试的例子。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值