天天看點

ssm 架構內建memcached 緩存伺服器

第一步:安裝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);		
			
		}

}
           

執行的結果如下截圖所示:

ssm 架構內建memcached 緩存伺服器