XMemcached&Spring集成

今天研究Memcached的Java的Client,使用XMemcached 1.3.5,做个简单的测试,并介绍如何与Spring集成。 

一、Memcached Client简要介绍 
Memcached Client目前有3种: 
Memcached Client for Java 
SpyMemcached 
XMemcached 
这三种Client一直存在各种争议: 
Memcached Client for Java 比 SpyMemcached更稳定、更早、更广泛; 
SpyMemcached 比 Memcached Client for Java更高效; 
XMemcached 比 SpyMemcache并发效果更好。 

用数据来说话,参考官方性能对比: 
Memcached Client for Java:https://github.com/gwhalin/Memcached-Java-Client/wiki/PERFORMANCE 
XMemcached:http://xmemcached.googlecode.com/svn/trunk/benchmark/benchmark.html 

二、XMemcached特性 
XMemcached特性: 

  • 高性能
  • 支持完整的memcached文本协议,二进制协议。
  • 支持JMX,可以通过MBean调整性能参数、动态添加/移除server、查看统计等。
  • 支持客户端统计
  • 支持memcached节点的动态增减。
  • 支持memcached分布:余数分布和一致性哈希分布。
  • 更多的性能调整选项。

此外,XMemcached更容易与Spring集成。而且,属于中国原创!   

三、XMemcached简单实现 

MemcachedClientBuilder是MemcachedClient核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。 
XMemcachedClientBuilder一般通过构造方法配置地址列表,通常还要配置权重,代码如下: 
Java代码   收藏代码
  1. public XMemcachedClientBuilder(List<InetSocketAddress> addressList) {  
  2.     if (addressList != null) {  
  3.         for (InetSocketAddress addr : addressList) {  
  4.             this.addressMap.put(addr, null);  
  5.         }  
  6.     }  
  7. }  
  8.   
  9. public XMemcachedClientBuilder(List<InetSocketAddress> addressList,  
  10.         int[] weights) {  
  11.     if (addressList != null) {  
  12.         for (InetSocketAddress addr : addressList) {  
  13.             this.addressMap.put(addr, null);  
  14.         }  
  15.     }  
  16.     this.weights = weights;  
  17. }  

不过这哥们如果用this()可以省点代码,  还有给加点注释吧!  
此外,还需要设置连接池大小,使用二进制协议/文本协议等。 
通过build()方法获得 MemcachedClient 
然后就可以通过Memcached进行set、get、replace、delete等Memcached操作了!  
上代码: 
Java代码   收藏代码
  1. import static junit.framework.Assert.*;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.concurrent.TimeoutException;  
  5.   
  6. import net.rubyeye.xmemcached.MemcachedClient;  
  7. import net.rubyeye.xmemcached.MemcachedClientBuilder;  
  8. import net.rubyeye.xmemcached.XMemcachedClientBuilder;  
  9. import net.rubyeye.xmemcached.command.BinaryCommandFactory;  
  10. import net.rubyeye.xmemcached.exception.MemcachedException;  
  11. import net.rubyeye.xmemcached.utils.AddrUtil;  
  12.   
  13. import org.junit.Test;  
  14.   
  15. /** 
  16.  * @author 梁栋   
  17.  *  
  18.  */  
  19. public class MemcachedClientTest {  
  20.     @Test  
  21.     public void test() {  
  22.         MemcachedClientBuilder builder = new XMemcachedClientBuilder(  
  23.                 AddrUtil.getAddresses("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211"),  
  24.                 new int[] { 1111 });  
  25.           
  26.         // 设置连接池大小,即客户端个数  
  27.         builder.setConnectionPoolSize(50);  
  28.   
  29.         // 宕机报警  
  30.         builder.setFailureMode(true);  
  31.   
  32.         // 使用二进制文件  
  33.         builder.setCommandFactory(new BinaryCommandFactory());  
  34.   
  35.         MemcachedClient memcachedClient = null;  
  36.         try {  
  37.             memcachedClient = builder.build();  
  38.             try {  
  39.                 // 设置/获取  
  40.                 memcachedClient.set("zlex"36000"set/get");  
  41.                 assertEquals("set/get", memcachedClient.get("zlex"));  
  42.   
  43.                 // 替换  
  44.                 memcachedClient.replace("zlex"36000"replace");  
  45.                 assertEquals("replace", memcachedClient.get("zlex"));  
  46.   
  47.                 // 移除  
  48.                 memcachedClient.delete("zlex");  
  49.                 assertNull(memcachedClient.get("zlex"));  
  50.             } catch (TimeoutException e) {  
  51.                 // TODO Auto-generated catch block  
  52.                 e.printStackTrace();  
  53.             } catch (InterruptedException e) {  
  54.                 // TODO Auto-generated catch block  
  55.                 e.printStackTrace();  
  56.             } catch (MemcachedException e) {  
  57.                 // TODO Auto-generated catch block  
  58.                 e.printStackTrace();  
  59.             }  
  60.   
  61.         } catch (IOException e) {  
  62.             // TODO Auto-generated catch block  
  63.             e.printStackTrace();  
  64.         } finally {  
  65.             if (memcachedClient != null) {  
  66.                 try {  
  67.                     memcachedClient.shutdown();  
  68.                 } catch (IOException e) {  
  69.                     // TODO Auto-generated catch block  
  70.                     e.printStackTrace();  
  71.                 }  
  72.             }  
  73.         }  
  74.     }  
  75. }  

四、XMemcached与Spring集成 
XMemcached与Spring集成可以参考 http://code.google.com/p/xmemcached/wiki/Spring_Integration,这里只说最常用的方法。 
memcached.properties做基本配置: 
Properties代码   收藏代码
  1. #连接池大小即客户端个数  
  2. memcached.connectionPoolSize=50  
  3. memcached.failureMode=true  
  4. #server1  
  5. memcached.server1.host=10.11.155.26  
  6. memcached.server1.port=11211  
  7. memcached.server1.weight=4  
  8. #server2  
  9. memcached.server2.host=10.11.155.41  
  10. memcached.server2.port=11211  
  11. memcached.server2.weight=3                
  12. #server3  
  13. memcached.server3.host=10.10.76.31  
  14. memcached.server3.port=11211  
  15. memcached.server3.weight=2                    
  16. #server4  
  17. memcached.server4.host=10.10.76.35  
  18. memcached.server4.port=11211  
  19. memcached.server4.weight=1         
  20.        

XML配置文件: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:p="http://www.springframework.org/schema/p"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  9.     <!-- http://code.google.com/p/xmemcached/wiki/Spring_Integration -->  
  10.     <context:property-placeholder location="memcached.properties" />  
  11.     <bean  
  12.         id="memcachedClientBuilder"  
  13.         class="net.rubyeye.xmemcached.XMemcachedClientBuilder"  
  14.         p:connectionPoolSize="${memcached.connectionPoolSize}"  
  15.         p:failureMode="${memcached.failureMode}">  
  16.         <!-- XMemcachedClientBuilder have two arguments.First is server list,and   
  17.             second is weights array. -->  
  18.         <constructor-arg>  
  19.             <list>  
  20.                 <bean class="java.net.InetSocketAddress">  
  21.                     <constructor-arg>  
  22.                         <value>${memcached.server1.host}</value>  
  23.                     </constructor-arg>  
  24.                     <constructor-arg>  
  25.                         <value>${memcached.server1.port}</value>  
  26.                     </constructor-arg>  
  27.                 </bean>  
  28.                 <bean class="java.net.InetSocketAddress">  
  29.                     <constructor-arg>  
  30.                         <value>${memcached.server2.host}</value>  
  31.                     </constructor-arg>  
  32.                     <constructor-arg>  
  33.                         <value>${memcached.server2.port}</value>  
  34.                     </constructor-arg>  
  35.                 </bean>  
  36.                 <bean class="java.net.InetSocketAddress">  
  37.                     <constructor-arg>  
  38.                         <value>${memcached.server3.host}</value>  
  39.                     </constructor-arg>  
  40.                     <constructor-arg>  
  41.                         <value>${memcached.server3.port}</value>  
  42.                     </constructor-arg>  
  43.                 </bean>  
  44.                 <bean class="java.net.InetSocketAddress">  
  45.                     <constructor-arg>  
  46.                         <value>${memcached.server4.host}</value>  
  47.                     </constructor-arg>  
  48.                     <constructor-arg>  
  49.                         <value>${memcached.server4.port}</value>  
  50.                     </constructor-arg>  
  51.                 </bean>  
  52.             </list>  
  53.         </constructor-arg>  
  54.         <constructor-arg>  
  55.             <list>  
  56.                 <value>${memcached.server1.weight}</value>  
  57.                 <value>${memcached.server2.weight}</value>  
  58.                 <value>${memcached.server3.weight}</value>  
  59.                 <value>${memcached.server4.weight}</value>  
  60.             </list>  
  61.         </constructor-arg>  
  62.         <property name="commandFactory">  
  63.             <bean class="net.rubyeye.xmemcached.command.TextCommandFactory" />  
  64.         </property>  
  65.         <property name="sessionLocator">  
  66.             <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />  
  67.         </property>  
  68.         <property name="transcoder">  
  69.             <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />  
  70.         </property>  
  71.     </bean>  
  72.     <!-- Use factory bean to build memcached client -->  
  73.     <bean  
  74.         id="memcachedClient"  
  75.         factory-bean="memcachedClientBuilder"  
  76.         factory-method="build"  
  77.         destroy-method="shutdown" />  
  78. </beans>  


这里的 memcachedClientBuilder节点完成 MemcachedClientBuilder,然后通过 memcachedClient节点配置 factory-method,调用 MemcachedClientBuilder的build()方法产生 MemcachedClient,并配置 destroy-method进行关闭。 
不过我还是疑惑,这里的异常由谁来处理?Spring容器吗?  或者需要另一个代理Bean包装一下? 

有了Spring容器支持,我们不需要在代码中进行配置,也不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。 
代码如下: 
Java代码   收藏代码
  1. import static junit.framework.Assert.*;  
  2.   
  3. import java.util.concurrent.TimeoutException;  
  4.   
  5. import net.rubyeye.xmemcached.MemcachedClient;  
  6. import net.rubyeye.xmemcached.exception.MemcachedException;  
  7.   
  8. import org.junit.Before;  
  9. import org.junit.Test;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  12.   
  13. /** 
  14.  * @author 梁栋 
  15.  *  
  16.  */  
  17. public class MemcachedSpringTest {  
  18.   
  19.     private ApplicationContext app;  
  20.     private MemcachedClient memcachedClient;  
  21.   
  22.     @Before  
  23.     public void init() {  
  24.         app = new ClassPathXmlApplicationContext("applicationContext.xml");  
  25.         memcachedClient = (MemcachedClient) app.getBean("memcachedClient");  
  26.     }  
  27.   
  28.     @Test  
  29.     public void test() {  
  30.         try {  
  31.             // 设置/获取  
  32.             memcachedClient.set("zlex"36000"set/get");  
  33.             assertEquals("set/get", memcachedClient.get("zlex"));  
  34.   
  35.             // 替换  
  36.             memcachedClient.replace("zlex"36000"replace");  
  37.             assertEquals("replace", memcachedClient.get("zlex"));  
  38.   
  39.             // 移除  
  40.             memcachedClient.delete("zlex");  
  41.             assertNull(memcachedClient.get("zlex"));  
  42.         } catch (TimeoutException e) {  
  43.             // TODO Auto-generated catch block  
  44.             e.printStackTrace();  
  45.         } catch (InterruptedException e) {  
  46.             // TODO Auto-generated catch block  
  47.             e.printStackTrace();  
  48.         } catch (MemcachedException e) {  
  49.             // TODO Auto-generated catch block  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53. }  


你可能感兴趣的:(xmemcached)