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

[经验分享] spring与memcache的集成

[复制链接]

尚未签到

发表于 2015-8-31 07:35:21 | 显示全部楼层 |阅读模式
1.安装memcache




1)下载memcached服务端memcached-1.2.6-win32-bin.zip,地址:http://code.jellycan.com/memcached/
2)下载java版客户端 java_memcached-release_2.6.1.zip
3)解压缩memcached-1.2.6-win32-bin.zip到指定目录,例如:D:\memcached-1.2.6-win32 ,
在终端(即cmd命令行界面),执行'D:\memcached-1.2.6-win32\memcached.exe -d install'
安装,再执行:'D:\memcached\memcached.exe -d start'启动,这样memcache就会作为windows系统服务在每 次开机时启动memcache服务。

  


2. 新建配置文件(spring级别)
  新建名为spring-memcache.xml的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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize"
destroy-method="shutDown">
<constructor-arg>
<value>neeaMemcachedPool</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>neeaMemcachedPool</value>
</constructor-arg>
</bean>
</beans>
  


3.Web.xml文件中配置新建的文件



<!-- 配置spring的监听器,加载Spring配置文件-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/applicationContext-common.xml,classpath:/spring/spring-memcache.xml</param-value>
</context-param>
  

4. 修改spring配置文件
  修改applicationContext-common.xml配置文件。
  1).添加properties配置文件(memcache.properties)去配置memcache的属性。
  



<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:memcache.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
  
  2).添加bean去初始化我们自己的一个spring工具类,一会进行详细解释。



<bean id="springContextHolder" class="com.hxrainbow.crm.util.SpringContextHolder"/>
  

5. Memcache配置文件
  memcache.properties文件内容如下:



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

6. 获得spring容器的工具类



/**
* 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.
**/
public class SpringContextHolder implementsApplicationContextAware{
private static ApplicationContext applicationContext;
/**
* 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
*/
public voidsetApplicationContext(ApplicationContext applicationContext) {
SpringContextHolder.applicationContext= applicationContext;
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public staticApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
@SuppressWarnings("unchecked")
public static<T> T getBean(String name) {
checkApplicationContext();
return (T) applicationContext.getBean(name);
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
* 如果有多个Bean符合Class, 取出第一个.
*/
@SuppressWarnings("unchecked")
public static<T> T getBean(Class<T> clazz) {
checkApplicationContext();
Map beanMaps = applicationContext.getBeansOfType(clazz);
if (beanMaps!=null&& !beanMaps.isEmpty()) {
return(T) beanMaps.values().iterator().next();
} else{
return null;
}
}
private static voidcheckApplicationContext() {
if (applicationContext == null) {
throw newIllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
}
  
  首先说一下ApplicationContextAware这个接口,这个接口中有一个方法:



void setApplicationContext(ApplicationContext applicationContext)
  
  下面是这个方法的简单说明:
  Set the ApplicationContext that this object runs in.Normally this call will be used to initialize the object.
  我们在配置文件中配置了bean的初始化,然后他就可以用于获得spring容器中的东西了。

7.memcache的工具类




public class MemcacheUtil {
    public static MemCachedClient getMemCachedClient() {
        return SpringContextHolder.getBean("memcachedClient");
}
}

8.junit测试类




public class MemcacheUtilTest {
    static MemCachedClient memcachedClient;
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"/spring/applicationContext-common.xml","/spring/spring-memcache.xml"});
    }
    @Test
    public void s() {
        MemCachedClient m=SpringContextHolder.getBean("memcachedClient");
        m.set("name", "yunhui");
        System.out.println(m.get("name"));
    }
}

9. Java对memcache调用的实现
  Java中对memcache的实现有3种比较出名,分别是memcached client for java(也就是我们上面使用的)、spymemcached以及xmemcache
  下面是我从csdn看到的一个对比他们性能的文章:




较早之前的一些比较主要是集中在java memcached client和spymemcached之间,普遍的结论是:spymemcached校之java memcached client有更高的性能,但却没有java memcached client稳定。随着java memcached client新版本的发布,一些新的对比测试标明java memcached client在性能上并不比spymemcached逊色多少,再加上java memcached client被广泛使用,表现稳定,因此在一般情况下java memcached client是首选的memcache client.
除上述两个产品之外,还有一个由中国人编写的名为XMemcached的后起之秀,据该产品官网上给出的性能对比,这个产品的性能表现是非常优秀的。但在使用的普遍性和项目未来的可维护上,在选型上需要慎重考虑。
以下是相关产品官网发布的性能测试对比,结果似乎不是那么一致,甚至有些相左,只当参考吧,毕竟基于各自的立场和不同的对比环境,有出入是正常的。
java memcached client官方发布的性能对比:https://github.com/gwhalin/Memcached-Java-Client/wiki/PERFORMANCE
XMemcached官方发布的性能对比:http://xmemcached.googlecode.com/svn/trunk/benchmark/benchmark.html
综合考虑,java memcached client是一个稳妥的选择。

运维网声明 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-106500-1-1.html 上篇帖子: memcache高并发控制 下篇帖子: 如何对memcache的数据(key-value)进行遍历操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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