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

[经验分享] Memcached Client简要介绍

[复制链接]

尚未签到

发表于 2015-11-19 07:31:08 | 显示全部楼层 |阅读模式
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集成。而且,属于中国原创! DSC0000.gif   

三、XMemcached简单实现

MemcachedClientBuilder是MemcachedClient核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。
XMemcachedClientBuilder一般通过构造方法配置地址列表,通常还要配置权重,代码如下:
Java代码   DSC0001.png

  • public XMemcachedClientBuilder(List<InetSocketAddress> addressList) {  
  •     if (addressList != null) {  
  •         for (InetSocketAddress addr : addressList) {  
  •             this.addressMap.put(addr, null);  
  •         }  
  •     }  
  • }  
  •   
  • public XMemcachedClientBuilder(List<InetSocketAddress> addressList,  
  •         int[] weights) {  
  •     if (addressList != null) {  
  •         for (InetSocketAddress addr : addressList) {  
  •             this.addressMap.put(addr, null);  
  •         }  
  •     }  
  •     this.weights = weights;  
  • }  

不过这哥们如果用this()可以省点代码, 还有给加点注释吧! DSC0002.gif
此外,还需要设置连接池大小,使用二进制协议/文本协议等。
通过build()方法获得MemcachedClient
然后就可以通过Memcached进行set、get、replace、delete等Memcached操作了!
上代码:
Java代码  

  • import static junit.framework.Assert.*;  
  •   
  • import java.io.IOException;  
  • import java.util.concurrent.TimeoutException;  
  •   
  • import net.rubyeye.xmemcached.MemcachedClient;  
  • import net.rubyeye.xmemcached.MemcachedClientBuilder;  
  • import net.rubyeye.xmemcached.XMemcachedClientBuilder;  
  • import net.rubyeye.xmemcached.command.BinaryCommandFactory;  
  • import net.rubyeye.xmemcached.exception.MemcachedException;  
  • import net.rubyeye.xmemcached.utils.AddrUtil;  
  •   
  • import org.junit.Test;  
  •   
  • public class MemcachedClientTest {  
  •     @Test  
  •     public void test() {  
  •         MemcachedClientBuilder builder = new XMemcachedClientBuilder(  
  •                 AddrUtil.getAddresses(&quot;10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211&quot;),  
  •                 new int[] { 1, 1, 1, 1 });  
  •          
  •         // 设置连接池大小,即客户端个数  
  •         builder.setConnectionPoolSize(50);  
  •   
  •         // 宕机报警  
  •         builder.setFailureMode(true);  
  •   
  •         // 使用二进制文件  
  •         builder.setCommandFactory(new BinaryCommandFactory());  
  •   
  •         MemcachedClient memcachedClient = null;  
  •         try {  
  •             memcachedClient = builder.build();  
  •             try {  
  •                 // 设置/获取  
  •                 memcachedClient.set(&quot;zlex&quot;, 36000, &quot;set/get&quot;);  
  •                 assertEquals(&quot;set/get&quot;, memcachedClient.get(&quot;zlex&quot;));  
  •   
  •                 // 替换  
  •                 memcachedClient.replace(&quot;zlex&quot;, 36000, &quot;replace&quot;);  
  •                 assertEquals(&quot;replace&quot;, memcachedClient.get(&quot;zlex&quot;));  
  •   
  •                 // 移除  
  •                 memcachedClient.delete(&quot;zlex&quot;);  
  •                 assertNull(memcachedClient.get(&quot;zlex&quot;));  
  •             } catch (TimeoutException e) {  
  •                 // TODO Auto-generated catch block  
  •                 e.printStackTrace();  
  •             } catch (InterruptedException e) {  
  •                 // TODO Auto-generated catch block  
  •                 e.printStackTrace();  
  •             } catch (MemcachedException e) {  
  •                 // TODO Auto-generated catch block  
  •                 e.printStackTrace();  
  •             }  
  •   
  •         } catch (IOException e) {  
  •             // TODO Auto-generated catch block  
  •             e.printStackTrace();  
  •         } finally {  
  •             if (memcachedClient != null) {  
  •                 try {  
  •                     memcachedClient.shutdown();  
  •                 } catch (IOException e) {  
  •                     // TODO Auto-generated catch block  
  •                     e.printStackTrace();  
  •                 }  
  •             }  
  •         }  
  •     }  
  • }  

四、XMemcached与Spring集成
XMemcached与Spring集成可以参考http://code.google.com/p/xmemcached/wiki/Spring_Integration,这里只说最常用的方法。
memcached.properties做基本配置:
Properties代码  

  • #连接池大小即客户端个数  
  • memcached.connectionPoolSize=50  
  • memcached.failureMode=true  
  • #server1  
  • memcached.server1.host=10.11.155.26  
  • memcached.server1.port=11211  
  • memcached.server1.weight=4  
  • #server2  
  • memcached.server2.host=10.11.155.41  
  • memcached.server2.port=11211  
  • memcached.server2.weight=3               
  • #server3  
  • memcached.server3.host=10.10.76.31  
  • memcached.server3.port=11211  
  • memcached.server3.weight=2                    
  • #server4  
  • memcached.server4.host=10.10.76.35  
  • memcached.server4.port=11211  
  • memcached.server4.weight=1         
  •       

XML配置文件:
Xml代码  

  • <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>  
  • <beans  
  •     xmlns=&quot;http://www.springframework.org/schema/beans&quot;  
  •     xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;  
  •     xmlns:context=&quot;http://www.springframework.org/schema/context&quot;  
  •     xmlns:p=&quot;http://www.springframework.org/schema/p&quot;  
  •     xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  •         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd&quot;>  
  •     <!-- http://code.google.com/p/xmemcached/wiki/Spring_Integration -->  
  •     <context:property-placeholder location=&quot;memcached.properties&quot; />  
  •     <bean  
  •         id=&quot;memcachedClientBuilder&quot;  
  •         class=&quot;net.rubyeye.xmemcached.XMemcachedClientBuilder&quot;  
  •         p:connectionPoolSize=&quot;${memcached.connectionPoolSize}&quot;  
  •         p:failureMode=&quot;${memcached.failureMode}&quot;>  
  •         <!-- XMemcachedClientBuilder have two arguments.First is server list,and   
  •             second is weights array. -->  
  •         <constructor-arg>  
  •             <list>  
  •                 <bean class=&quot;java.net.InetSocketAddress&quot;>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server1.host}</value>  
  •                     </constructor-arg>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server1.port}</value>  
  •                     </constructor-arg>  
  •                 </bean>  
  •                 <bean class=&quot;java.net.InetSocketAddress&quot;>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server2.host}</value>  
  •                     </constructor-arg>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server2.port}</value>  
  •                     </constructor-arg>  
  •                 </bean>  
  •                 <bean class=&quot;java.net.InetSocketAddress&quot;>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server3.host}</value>  
  •                     </constructor-arg>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server3.port}</value>  
  •                     </constructor-arg>  
  •                 </bean>  
  •                 <bean class=&quot;java.net.InetSocketAddress&quot;>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server4.host}</value>  
  •                     </constructor-arg>  
  •                     <constructor-arg>  
  •                         <value>${memcached.server4.port}</value>  
  •                     </constructor-arg>  
  •                 </bean>  
  •             </list>  
  •         </constructor-arg>  
  •         <constructor-arg>  
  •             <list>  
  •                 <value>${memcached.server1.weight}</value>  
  •                 <value>${memcached.server2.weight}</value>  
  •                 <value>${memcached.server3.weight}</value>  
  •                 <value>${memcached.server4.weight}</value>  
  •             </list>  
  •         </constructor-arg>  
  •         <property name=&quot;commandFactory&quot;>  
  •             <bean class=&quot;net.rubyeye.xmemcached.command.TextCommandFactory&quot; />  
  •         </property>  
  •         <property name=&quot;sessionLocator&quot;>  
  •             <bean class=&quot;net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator&quot; />  
  •         </property>  
  •         <property name=&quot;transcoder&quot;>  
  •             <bean class=&quot;net.rubyeye.xmemcached.transcoders.SerializingTranscoder&quot; />  
  •         </property>  
  •     </bean>  
  •     <!-- Use factory bean to build memcached client -->  
  •     <bean  
  •         id=&quot;memcachedClient&quot;  
  •         factory-bean=&quot;memcachedClientBuilder&quot;  
  •         factory-method=&quot;build&quot;  
  •         destroy-method=&quot;shutdown&quot; />  
  • </beans>  


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

有了Spring容器支持,我们不需要在代码中进行配置,也不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。
代码如下:
Java代码  

  • import static junit.framework.Assert.*;  
  •   
  • import java.util.concurrent.TimeoutException;  
  •   
  • import net.rubyeye.xmemcached.MemcachedClient;  
  • import net.rubyeye.xmemcached.exception.MemcachedException;  
  •   
  • import org.junit.Before;  
  • import org.junit.Test;  
  • import org.springframework.context.ApplicationContext;  
  • import org.springframework.context.support.ClassPathXmlApplicationContext;  
  •   
  • public class MemcachedSpringTest {  
  •   
  •     private ApplicationContext app;  
  •     private MemcachedClient memcachedClient;  
  •   
  •     @Before  
  •     public void init() {  
  •         app = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);  
  •         memcachedClient = (MemcachedClient) app.getBean(&quot;memcachedClient&quot;);  
  •     }  
  •   
  •     @Test  
  •     public void test() {  
  •         try {  
  •             // 设置/获取  
  •             memcachedClient.set(&quot;zlex&quot;, 36000, &quot;set/get&quot;);  
  •             assertEquals(&quot;set/get&quot;, memcachedClient.get(&quot;zlex&quot;));  
  •   
  •             // 替换  
  •             memcachedClient.replace(&quot;zlex&quot;, 36000, &quot;replace&quot;);  
  •             assertEquals(&quot;replace&quot;, memcachedClient.get(&quot;zlex&quot;));  
  •   
  •             // 移除  
  •             memcachedClient.delete(&quot;zlex&quot;);  
  •             assertNull(memcachedClient.get(&quot;zlex&quot;));  
  •         } catch (TimeoutException e) {  
  •             // TODO Auto-generated catch block  
  •             e.printStackTrace();  
  •         } catch (InterruptedException e) {  
  •             // TODO Auto-generated catch block  
  •             e.printStackTrace();  
  •         } catch (MemcachedException e) {  
  •             // TODO Auto-generated catch block  
  •             e.printStackTrace();  
  •         }  
  •     }  
  • }  


运维网声明 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-140860-1-1.html 上篇帖子: Redis工作系列之一 与 Memcached对比理解 下篇帖子: Memcached源码阅读之初始化参数解析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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