Memcached笔记——(二)XMemcached&Spring集成
今天研究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集成。而且,属于中国原创!
三、XMemcached简单实现
MemcachedClientBuilder是MemcachedClient核心接口,用来控制Client的构建(build()方法)和关闭(shutdown()方法)。
XMemcachedClientBuilder一般通过构造方法配置地址列表,通常还要配置权重,代码如下:
- 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;
- }
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()可以省点代码, 还有给加点注释吧!
此外,还需要设置连接池大小,使用二进制协议/文本协议等。
通过build()方法获得MemcachedClient
然后就可以通过Memcached进行set、get、replace、delete等Memcached操作了!
上代码:
- 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();
- }
- }
- }
- }
- }
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做基本配置:
- #连接池大小即客户端个数
- 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
#连接池大小即客户端个数
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
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>
<?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容器吗? 或者需要另一个代理Bean包装一下?
有了Spring容器支持,我们不需要在代码中进行配置,也不需要重复调用build()跟shutdown()方法,这些操作交给Spring来完成。
代码如下:
- 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();
- }
- }
- }
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笔记——(四)应对高并发攻击
Memcached笔记——(二)XMemcached&Spring集成的更多相关文章
- Memcached理解笔2---XMemcached&Spring集成
一.Memcached Client简要介绍 Memcached Client目前有3种: Memcached Client for Java SpyMemcached XMemcached 这三种C ...
- 160530、memcached集群(spring集成的配置)
第一步:在linux机或windows机上安装memcached服务端(server) linux中安装memcached:centos中命令 yum -y install memcached 如果没 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...
- mybatis笔记<二> 整合spring
mybatis与spring整合需要添加几个jar包,mybatis-spring, spring-context, spring-jdbc 1. spring ioc只要一个jar包就ok 2. 我 ...
- 二)spring 集成 ehcache jgroups 集群
依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-co ...
- 二、spring集成ibatis进行数据源事务管理拦截器环境配置
1.dataSource-applicationContext.xml文件配置理解:(spring1.2.8+ibatis1.5.3)1.1)配置数据源 DriverManagerDataSource ...
- SpringBoot学习笔记二之Spring整合Mybatis
原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...
- Memcached笔记——(四)应对高并发攻击【转】
http://snowolf.iteye.com/blog/1677495 近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最 ...
- Memcached笔记——(四)应对高并发攻击
近半个月过得很痛苦,主要是产品上线后,引来无数机器用户恶意攻击,不停的刷新产品各个服务入口,制造垃圾数据,消耗资源.他们的最好成绩,1秒钟可以并发6次,赶在Database入库前,Cache进行Mis ...
随机推荐
- sublime text2设置快捷键打开浏览器
1 编辑一个py文件,内容如下: import sublime, sublime_plugin import webbrowser url_map = { 'C:\\server\\www\\' : ...
- DataStructuresAndAlgorithm--字谜游戏
参考:http://tieba.baidu.com/p/2071585293 输入是由一些字母构成的一个二维数组以及一些单词组成.目标是要找出字谜中的单词,这些单词可能是水平.垂直或沿对角线上任何方向 ...
- hdu 1246
很久没有写题解了~因为懒(年纪大了就是脸皮厚,还有脸说) 这道题今天花了很长时间去推,一开始以为是规律题,没推出来,直接模拟也TLE了,接着考虑实在是没思路,看了题解. 思路大概就是这样: 先上代码( ...
- Java的各类型数据在内存中分配情况详解
1. 有这样一种说法,如今争锋于IT战场的两大势力,MS一族偏重于底层实现,Java一族偏重于系统架构.说法根据无从考证,但从两大势力各自的社区力量和图书市场已有佳作不难看出,此说法不虚,但 ...
- C语言入门基础整理
学习计算机技术,C语言可以说是必备的,他已经成为现在计算机行业人学习必备的,而且应用也是十分的广泛,今天就来看看拥有几年c语言工作经验的大神整理的C语言入门基础知识,没有学不会,只有不肯学. 结构化程 ...
- RDD(九)——序列化问题
在实际开发中我们往往需要自己定义一些对于RDD的操作,那么此时需要考虑的主要问题是,初始化工作是在Driver端进行的,而实际运行程序是在Executor端进行的,这就涉及到了跨进程通信,是需要序列化 ...
- mysql之存储过程(三)
带参数的存储过程: 特别说明: 在游标中是不支持对形参的判断的,外部可以 调用操作: call settlexxxxx_common("1970-11",999); 定义如下: ...
- 迅为iTOP-4418开发板编译Ubuntu
Ubuntu 系统比较特殊,源码就是它的镜像.Ubuntu 系统通过解压的方式进行烧写,我们也可以通过配置解压出来的 Ubuntu 系统源码文件夹,来配置 Ubuntu 系统.然后通过打包压缩的方式来 ...
- Codeforces Round #598 (Div. 3)E(dp路径转移)
题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...
- 007.前端开发知识,前端基础CSS(2020-01-28)
一.布局 一列固定宽度且居中 两列左窄右宽型 通栏平均分布型 1.一列固定宽度且居中布局<body> .top+.banner+.main+.footer 按Tab键,得到下框中代码 &l ...