Memcached理解笔2---XMemcached&Spring集成

一、Memcached Client简要介绍 
Memcached Client目前有3种: 


这三种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. public class MemcachedClientTest {  
  16.     @Test  
  17.     public void test() {  
  18.         MemcachedClientBuilder builder = new XMemcachedClientBuilder(  
  19.                 AddrUtil.getAddresses("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211"),  
  20.                 new int[] { 1, 1, 1, 1 });  
  21.           
  22.         // 设置连接池大小,即客户端个数  
  23.         builder.setConnectionPoolSize(50);  
  24.   
  25.         // 宕机报警  
  26.         builder.setFailureMode(true);  
  27.   
  28.         // 使用二进制文件  
  29.         builder.setCommandFactory(new BinaryCommandFactory());  
  30.   
  31.         MemcachedClient memcachedClient = null;  
  32.         try {  
  33.             memcachedClient = builder.build();  
  34.             try {  
  35.                 // 设置/获取  
  36.                 memcachedClient.set("zlex", 36000, "set/get");  
  37.                 assertEquals("set/get", memcachedClient.get("zlex"));  
  38.   
  39.                 // 替换  
  40.                 memcachedClient.replace("zlex", 36000, "replace");  
  41.                 assertEquals("replace", memcachedClient.get("zlex"));  
  42.   
  43.                 // 移除  
  44.                 memcachedClient.delete("zlex");  
  45.                 assertNull(memcachedClient.get("zlex"));  
  46.             } catch (TimeoutException e) {  
  47.                 // TODO Auto-generated catch block  
  48.                 e.printStackTrace();  
  49.             } catch (InterruptedException e) {  
  50.                 // TODO Auto-generated catch block  
  51.                 e.printStackTrace();  
  52.             } catch (MemcachedException e) {  
  53.                 // TODO Auto-generated catch block  
  54.                 e.printStackTrace();  
  55.             }  
  56.   
  57.         } catch (IOException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         } finally {  
  61.             if (memcachedClient != null) {  
  62.                 try {  
  63.                     memcachedClient.shutdown();  
  64.                 } catch (IOException e) {  
  65.                     // TODO Auto-generated catch block  
  66.                     e.printStackTrace();  
  67.                 }  
  68.             }  
  69.         }  
  70.     }  
  71. }  


四、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. public class MemcachedSpringTest {  
    14.   
    15.     private ApplicationContext app;  
    16.     private MemcachedClient memcachedClient;  
    17.   
    18.     @Before  
    19.     public void init() {  
    20.         app = new ClassPathXmlApplicationContext("applicationContext.xml");  
    21.         memcachedClient = (MemcachedClient) app.getBean("memcachedClient");  
    22.     }  
    23.   
    24.     @Test  
    25.     public void test() {  
    26.         try {  
    27.             // 设置/获取  
    28.             memcachedClient.set("zlex", 36000, "set/get");  
    29.             assertEquals("set/get", memcachedClient.get("zlex"));  
    30.   
    31.             // 替换  
    32.             memcachedClient.replace("zlex", 36000, "replace");  
    33.             assertEquals("replace", memcachedClient.get("zlex"));  
    34.   
    35.             // 移除  
    36.             memcachedClient.delete("zlex");  
    37.             assertNull(memcachedClient.get("zlex"));  
    38.         } catch (TimeoutException e) {  
    39.             // TODO Auto-generated catch block  
    40.             e.printStackTrace();  
    41.         } catch (InterruptedException e) {  
    42.             // TODO Auto-generated catch block  
    43.             e.printStackTrace();  
    44.         } catch (MemcachedException e) {  
    45.             // TODO Auto-generated catch block  
    46.             e.printStackTrace();  
    47.         }  
    48.     }  
    49. }  

转载地址:http://snowolf.iteye.com/blog/1471805

Memcached理解笔2---XMemcached&Spring集成,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。