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:https://github.com/gwhalin/Memcached-Java-Client/wiki/PERFORMANCE
XMemcached:http://xmemcached.googlecode.com/svn/trunk/benchmark/benchmark.html
二、XMemcached特性
XMemcached特性:
此外,XMemcached更容易與Spring整合。而且,屬於中國原創!
三、XMemcached簡單實現
MemcachedClientBuilder是MemcachedClient核心介面,用來控制Client的構建(build()方法)和關閉(shutdown()方法)。
XMemcachedClientBuilder一般通過構造方法配置地址列表,通常還要配置權重,程式碼如下:
不過這哥們如果用this()可以省點程式碼, 還有給加點註釋吧!
此外,還需要設定連線池大小,使用二進位制協議/文字協議等。
通過build()方法獲得MemcachedClient
然後就可以通過Memcached進行set、get、replace、delete等Memcached操作了!
上程式碼:
四、XMemcached與Spring整合
XMemcached與Spring整合可以參考http://code.google.com/p/xmemcached/wiki/Spring_Integration,這裡只說最常用的方法。
memcached.properties做基本配置:
XML配置檔案:
這裡的memcachedClientBuilder節點完成MemcachedClientBuilder,然後通過memcachedClient節點配置factory-method,呼叫MemcachedClientBuilder的build()方法產生MemcachedClient,並配置destroy-method進行關閉。
不過我還是疑惑,這裡的異常由誰來處理?Spring容器嗎? 或者需要另一個代理Bean包裝一下?
有了Spring容器支援,我們不需要在程式碼中進行配置,也不需要重複呼叫build()跟shutdown()方法,這些操作交給Spring來完成。
程式碼如下:
相關連結:
Memcached筆記——(一)安裝&常規錯誤&監控
Memcached筆記——(二)XMemcached&Spring整合
Memcached筆記——(三)Memcached使用總結
Memcached筆記——(四)應對高併發攻擊
相關連結:
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筆記——(四)應對高併發攻擊
相關文章
- Spring Boot 2.0(八):Spring Boot 整合 MemcachedSpring Boot
- ssm框架整合筆記SSM框架筆記
- Spring筆記(四)整合MybatisSpring筆記MyBatis
- SSM學習筆記3——整合 SpringMVC、整合SSMSSM筆記SpringMVC
- 筆記二(JavaWeb)筆記JavaWeb
- Spring筆記二Spring筆記
- RUST 筆記(二)Rust筆記
- Spring Boot系列筆記--整合RedisSpring Boot筆記Redis
- SpingBoot_學習筆記整合boot筆記
- 軟考筆記二筆記
- Linux筆記 篇(二)Linux筆記
- Kotlin Coroutines 筆記 (二)Kotlin筆記
- moquette改造筆記(一):整合到SpringBoot筆記Spring Boot
- Java學習筆記記錄(二)Java筆記
- Memcached記憶體管理原始碼分析記憶體原始碼
- TS學習筆記(二)筆記
- Docker筆記二之容器Docker筆記
- ANFIS學習筆記(二)筆記
- activiti學習筆記二筆記
- Typescript學習筆記(二)TypeScript筆記
- JavaScript學習筆記(二)JavaScript筆記
- Hibernate學習筆記二筆記
- 『筆記』二分圖筆記
- React 學習筆記【二】React筆記
- TensorFlow學習筆記(二)筆記
- vue學習筆記二Vue筆記
- Java筆記-Java反射(二)Java筆記反射
- python學習筆記(二)Python筆記
- goLang學習筆記(二)Golang筆記
- koa原始碼筆記(二)原始碼筆記
- 【學習筆記】Spring與Junit的整合筆記Spring
- Memcached
- 被人遺忘的Memcached記憶體注射記憶體
- Hydro二開筆記 前端篇筆記前端
- Spring MVC學習筆記二SpringMVC筆記
- orientDB學習筆記(二)MATCH筆記
- javaSE第二章筆記Java筆記
- TS學習筆記(二):介面筆記
- Flutter 高效自學筆記(二)Flutter筆記