ssm 框架集成memcached 缓存服务器分析

你猜 阅读:269 2021-03-31 21:54:36 评论:0


第一步:安装Memcached 服务端

1、CentOs(6.4)服务器安装Memcached 服务端(可以查看:cenos 6.4 安装memcached 服务端

2、下载java版客户端 java_memcached-release_2.6.1.zip

3、CentOs(6.4)服务器启动Memcached 服务端

ssm框架基础jar包

aopalliance.jar 
aspectjrt.jar 
aspectjweaver.jar 
commons-beanutils-1.9.2.jar 
commons-codec-1.9.jar 
commons-collections-3.2.1.jar 
commons-dbcp-1.4.jar 
commons-fileupload-1.3.1.jar 
commons-io-2.4.jar 
commons-lang-2.6.jar 
commons-logging-1.2.jar 
commons-net-3.1.jar 
commons-pool-1.6.jar 
commons-pool2-2.2.jar 
druid-1.0.9.jar 
fastjson-1.1.39.jar 
freemarker-2.3.19.jar 
hamcrest-core-1.3.jar 
jackson-all-1.9.5.jar 
jboss-logging-3.1.0.CR2.jar 
jettison-1.0.1.jar 
jstl-1.1.2.jar 
junit-4.11.jar 
log4j-1.2.17.jar 
log4j-over-slf4j-1.7.7.jar 
mybatis-3.2.6.jar 
mybatis-spring-1.2.2.jar 
mysql-connector-java-5.1.30-bin.jar 
servlet-api.jar 
slf4j-api-1.7.7.jar 
slf4j-ext-1.7.7.jar 
spring-aop-4.0.2.RELEASE.jar 
spring-aspects-4.0.2.RELEASE.jar 
spring-beans-4.0.2.RELEASE.jar 
spring-context-4.0.2.RELEASE.jar 
spring-context-support-4.0.2.RELEASE.jar 
spring-core-4.0.2.RELEASE.jar 
spring-expression-4.0.2.RELEASE.jar 
spring-jdbc-4.0.2.RELEASE.jar 
spring-oxm-4.0.2.RELEASE.jar 
spring-test-4.0.2.RELEASE.jar 
spring-tx-4.0.2.RELEASE.jar 
spring-web-4.0.4.RELEASE.jar 
spring-webmvc-4.0.2.RELEASE.jar 
standard-1.1.2.jar
memcached 关联jar 

java_memcached-release_2.6.6.jar



第二步:配置memcached.properties文件

memcache.server=120.25.56.93:11211 
memcache.initConn=20 
memcache.minConn=10 
memcache.maxConn=50 
memcache.maintSleep=3000 
memcache.nagle=false 
memcache.socketTO=3000 


第三步:spring配置文件集成memcached 

<?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:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
                        http://www.springframework.org/schema/context   
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd   
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
                        http://www.springframework.org/schema/aop  
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
                        http://www.springframework.org/schema/mvc   
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
 
 
 
	<!-- 引入配置文件 --> 
	<bean id="propertyConfigurer" 
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<!--<property name="location" value="classpath:jdbc.properties" />--> 
		<property name="locations"> 
			<list>   
                <value>classpath:jdbc.properties</value>   
                <value>classpath:memcache.properties</value>   
            </list>  
		</property> 
	</bean> 
 
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
		init-method="init" destroy-method="close"> 
		<!-- 基本属性 url、user、password --> 
		<property name="url" value="${url}" /> 
		<property name="username" value="${username}" /> 
		<property name="password" value="${password}" /> 
 
		<!-- 配置初始化大小、最小、最大 --> 
		<property name="initialSize" value="1" /> 
		<property name="minIdle" value="1" /> 
		<property name="maxActive" value="20" /> 
 
		<!-- 配置获取连接等待超时的时间 --> 
		<property name="maxWait" value="60000" /> 
 
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
		<property name="timeBetweenEvictionRunsMillis" value="60000" /> 
 
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
		<property name="minEvictableIdleTimeMillis" value="300000" /> 
 
		<property name="validationQuery" value="SELECT 'x'" /> 
		<property name="testWhileIdle" value="true" /> 
		<property name="testOnBorrow" value="false" /> 
		<property name="testOnReturn" value="false" /> 
 
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
		<property name="poolPreparedStatements" value="true" /> 
		<property name="maxPoolPreparedStatementPerConnectionSize" 
			value="20" /> 
 
		<!-- 配置监控统计拦截的filters --> 
		<property name="filters" value="stat" /> 
	</bean> 
 
 
	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
		<property name="dataSource" ref="dataSource" /> 
		<!-- 自动扫描mapping.xml文件 --> 
		<property name="mapperLocations" value="classpath:com/wlsq/oauth/mapper/*.xml"></property> 
	</bean> 
 
	<!-- DAO接口所在包名,Spring会自动查找其下的类 --> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
		<property name="basePackage" value="com.wlsq.oauth.dao" /> 
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 
	</bean> 
 
	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource" /> 
	</bean> 
 
	<!-- 通知 --> 
	<tx:advice id="tx" transaction-manager="transactionManager"> 
		<tx:attributes> 
			<tx:method name="delete*" propagation="REQUIRED" /> 
			<tx:method name="insert*" propagation="REQUIRED" /> 
			<tx:method name="update*" propagation="REQUIRED" /> 
			<tx:method name="find*" read-only="true" /> 
			<tx:method name="get*" read-only="true" /> 
			<tx:method name="select*" read-only="true" /> 
		</tx:attributes> 
	</tx:advice> 
 
	<aop:config> 
		<aop:pointcut id="pc" 
			expression="execution(* com.wlsq.oauth.service.*.*(..))" /> 
		<!--把事务控制在Service层 --> 
		<aop:advisor pointcut-ref="pc" advice-ref="tx" /> 
	</aop:config> 
 
	<!--spring 集成缓存服务器(memcached) --> 
	 <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" 
        factory-method="getInstance" init-method="initialize" 
        destroy-method="shutDown"> 
 
        <constructor-arg> 
            <value>memCachedPool</value> 
        </constructor-arg> 
         
        <property name="servers"> 
            <list> 
                <value>${memcache.server}</value> 
            </list> 
        </property> 
         
        <property name="initConn"> 
            <value>${memcache.initConn}</value> 
        </property> 
         
        <property name="minConn"> 
            <value>${memcache.minConn}</value> 
        </property> 
 
        <property name="maxConn"> 
            <value>${memcache.maxConn}</value> 
        </property> 
 
        <property name="maintSleep"> 
            <value>${memcache.maintSleep}</value> 
        </property> 
 
        <property name="nagle"> 
            <value>${memcache.nagle}</value> 
        </property> 
 
        <property name="socketTO"> 
            <value>${memcache.socketTO}</value> 
        </property> 
    </bean> 
 
    <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient"> 
        <constructor-arg> 
            <value>memCachedPool</value> 
        </constructor-arg> 
    </bean> 
 
</beans>


第四步:memcached 功能测试

package com.wlsq.oauth.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
 
import com.danga.MemCached.MemCachedClient; 
 
@Controller  
public class MemcachedController { 
		@Autowired 
		private MemCachedClient memCachedClient; 
	 
		@RequestMapping("/memcached") 
		public void setMemcachedServer(){ 
			Boolean target=memCachedClient.set("1", "123456"); 
			System.out.println("memcached 加载是否成功:"+target); 
			String result =(String)memCachedClient.get("1"); 
			System.out.println("memcached 获取的结果:"+result);		 
			 
		} 
 
} 

执行的结果如下截图所示:



声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号