Memcached笔记——(二)XMemcached&Spring集成

时间:2022-06-04 22:52:25

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




相关链接:

Memcached笔记——(一)安装&常规错误&监控

Memcached笔记——(二)XMemcached&Spring集成


Memcached笔记——(三)Memcached使用总结


Memcached笔记——(四)应对高并发攻击






一、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集成。而且,属于中国原创!Memcached笔记——(二)XMemcached&Spring集成




三、XMemcached简单实现




MemcachedClientBuilder是MemcachedClient核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。


XMemcachedClientBuilder一般通过构造方法配置地址列表,通常还要配置权重,代码如下:

  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. public XMemcachedClientBuilder(List<InetSocketAddress> addressList,
  9. int[] weights) {
  10. if (addressList != null) {
  11. for (InetSocketAddress addr : addressList) {
  12. this.addressMap.put(addr,
    null);
  13. }
  14. }
  15. this.weights = weights;
  16. }
	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()可以省点代码,Memcached笔记——(二)XMemcached&Spring集成 还有给加点注释吧!Memcached笔记——(二)XMemcached&Spring集成


此外,还需要设置连接池大小,使用二进制协议/文本协议等。

通过build()方法获得MemcachedClient

然后就可以通过Memcached进行set、get、replace、delete等Memcached操作了!Memcached笔记——(二)XMemcached&Spring集成


上代码:

  1. import static junit.framework.Assert.*;
  2. import java.io.IOException;
  3. import java.util.concurrent.TimeoutException;
  4. import net.rubyeye.xmemcached.MemcachedClient;
  5. import net.rubyeye.xmemcached.MemcachedClientBuilder;
  6. import net.rubyeye.xmemcached.XMemcachedClientBuilder;
  7. import net.rubyeye.xmemcached.command.BinaryCommandFactory;
  8. import net.rubyeye.xmemcached.exception.MemcachedException;
  9. import net.rubyeye.xmemcached.utils.AddrUtil;
  10. import org.junit.Test;
  11. public class MemcachedClientTest {
  12. @Test
  13. public void test() {
  14. MemcachedClientBuilder builder = new XMemcachedClientBuilder(
  15. AddrUtil.getAddresses("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211"),
  16. new int[] {
    1,
    1, 1,
    1 });
  17. // 设置连接池大小,即客户端个数
  18. builder.setConnectionPoolSize(50);
  19. // 宕机报警
  20. builder.setFailureMode(true);
  21. // 使用二进制文件
  22. builder.setCommandFactory(new BinaryCommandFactory());
  23. MemcachedClient memcachedClient = null;
  24. try {
  25. memcachedClient = builder.build();
  26. try {
  27. // 设置/获取
  28. memcachedClient.set("zlex",
    36000,
    "set/get");
  29. assertEquals("set/get", memcachedClient.get("zlex"));
  30. // 替换
  31. memcachedClient.replace("zlex",
    36000,
    "replace");
  32. assertEquals("replace", memcachedClient.get("zlex"));
  33. // 移除
  34. memcachedClient.delete("zlex");
  35. assertNull(memcachedClient.get("zlex"));
  36. } catch (TimeoutException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. } catch (InterruptedException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. } catch (MemcachedException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. } catch (IOException e) {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. } finally {
  50. if (memcachedClient !=
    null) {
  51. try {
  52. memcachedClient.shutdown();
  53. } catch (IOException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }
  60. }
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("10.11.155.26:11211 10.11.155.41:11211 10.10.76.31:11211 10.10.76.35:11211"),
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("zlex", 36000, "set/get");
assertEquals("set/get", memcachedClient.get("zlex")); // 替换
memcachedClient.replace("zlex", 36000, "replace");
assertEquals("replace", memcachedClient.get("zlex")); // 移除
memcachedClient.delete("zlex");
assertNull(memcachedClient.get("zlex"));
} 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做基本配置:

  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
#连接池大小即客户端个数
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配置文件:

  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>
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="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">
<!-- http://code.google.com/p/xmemcached/wiki/Spring_Integration -->
<context:property-placeholder location="memcached.properties" />
<bean
id="memcachedClientBuilder"
class="net.rubyeye.xmemcached.XMemcachedClientBuilder"
p:connectionPoolSize="${memcached.connectionPoolSize}"
p:failureMode="${memcached.failureMode}">
<!-- XMemcachedClientBuilder have two arguments.First is server list,and
second is weights array. -->
<constructor-arg>
<list>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>${memcached.server1.host}</value>
</constructor-arg>
<constructor-arg>
<value>${memcached.server1.port}</value>
</constructor-arg>
</bean>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>${memcached.server2.host}</value>
</constructor-arg>
<constructor-arg>
<value>${memcached.server2.port}</value>
</constructor-arg>
</bean>
<bean class="java.net.InetSocketAddress">
<constructor-arg>
<value>${memcached.server3.host}</value>
</constructor-arg>
<constructor-arg>
<value>${memcached.server3.port}</value>
</constructor-arg>
</bean>
<bean class="java.net.InetSocketAddress">
<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="commandFactory">
<bean class="net.rubyeye.xmemcached.command.TextCommandFactory" />
</property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean>
<!-- Use factory bean to build memcached client -->
<bean
id="memcachedClient"
factory-bean="memcachedClientBuilder"
factory-method="build"
destroy-method="shutdown" />
</beans>

这里的memcachedClientBuilder节点完成MemcachedClientBuilder,然后通过memcachedClient节点配置factory-method,调用MemcachedClientBuilder的build()方法产生MemcachedClient,并配置destroy-method进行关闭。


不过我还是疑惑,这里的异常由谁来处理?Spring容器吗?Memcached笔记——(二)XMemcached&Spring集成 或者需要另一个代理Bean包装一下?




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

代码如下:

  1. import static junit.framework.Assert.*;
  2. import java.util.concurrent.TimeoutException;
  3. import net.rubyeye.xmemcached.MemcachedClient;
  4. import net.rubyeye.xmemcached.exception.MemcachedException;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. public class MemcachedSpringTest {
  10. private ApplicationContext app;
  11. private MemcachedClient memcachedClient;
  12. @Before
  13. public void init() {
  14. app = new ClassPathXmlApplicationContext("applicationContext.xml");
  15. memcachedClient = (MemcachedClient) app.getBean("memcachedClient");
  16. }
  17. @Test
  18. public void test() {
  19. try {
  20. // 设置/获取
  21. memcachedClient.set("zlex",
    36000,
    "set/get");
  22. assertEquals("set/get", memcachedClient.get("zlex"));
  23. // 替换
  24. memcachedClient.replace("zlex",
    36000,
    "replace");
  25. assertEquals("replace", memcachedClient.get("zlex"));
  26. // 移除
  27. memcachedClient.delete("zlex");
  28. assertNull(memcachedClient.get("zlex"));
  29. } catch (TimeoutException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. } catch (InterruptedException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. } catch (MemcachedException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
  39. }
  40. }
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("applicationContext.xml");
memcachedClient = (MemcachedClient) app.getBean("memcachedClient");
} @Test
public void test() {
try {
// 设置/获取
memcachedClient.set("zlex", 36000, "set/get");
assertEquals("set/get", memcachedClient.get("zlex")); // 替换
memcachedClient.replace("zlex", 36000, "replace");
assertEquals("replace", memcachedClient.get("zlex")); // 移除
memcachedClient.delete("zlex");
assertNull(memcachedClient.get("zlex"));
} 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();
}
}
}

相关链接:

Memcached笔记——(一)安装&常规错误&监控

Memcached笔记——(二)XMemcached&Spring集成


Memcached笔记——(三)Memcached使用总结


Memcached笔记——(四)应对高并发攻击