Spring整合xmemcached过程

XMemcached是一个基于Java nio的memcached客户端。

它是线程安全,结构简单,支持所有的memcached文本协议和二进制协议,并且有比较优异的性能表现。还支持一些高级特性,如JMX、动态增删节点、客户端统计以及nio连接池等。

整合开始前,先确认有一个或多个可用的Memcached服务,XMemcached支持集群及服务的权重配置。

文章参考:

1. http://code.google.com/p/xmemcached/

2. http://code.google.com/p/xmemcached/wiki/Spring_Integration

 

1. 项目使用maven管理,那么请在pom.xml里边添加Xmemcached的依赖包:

1 <dependency
2     <groupId>com.googlecode.xmemcached</groupId>
3     <artifactId>xmemcached</artifactId>
4     <version>1.4.1</version>
5 </dependency>

2. 添加bean配置,自动装载到容器

 1 <bean id="propertyHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 2         <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
 3         <property name="ignoreResourceNotFound" value="true" />
 4         <property name="locations">
 5             <list>
 6                 <value>classpath:conf/system.properties</value>
 7                 <value>classpath:conf/jdbc.properties</value>
 8                 <value>classpath:conf/memcached.properties</value>
 9             </list>
10         </property>
11 </bean>
12 
13 <bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
14         <constructor-arg>
15             <list>
16                 <bean class="java.net.InetSocketAddress">
17                     <constructor-arg>
18                             <value>${memcached.server.host}</value>
19                     </constructor-arg>
20                     <constructor-arg>
21                             <value>${memcached.server.port}</value>
22                     </constructor-arg>
23                 </bean>
24             </list>
25         </constructor-arg>
26         <constructor-arg>
27             <list>
28                 <value>${memcached.server.weight}</value>
29             </list>
30         </constructor-arg>
31         <property name="connectionPoolSize" value="${memcached.connectionPoolSize}" />
32         <property name="commandFactory">
33                 <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
34         </property>
35         <property name="sessionLocator">
36                 <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
37         </property>
38         <property name="transcoder">
39                 <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
40         </property>
41 </bean>
42 <bean id="memcachedClient" factory-bean="memcachedClientBuilder" factory-method="build" destroy-method="shutdown" />

3. memcached.properties 配置内容:

memcached.connectionPoolSize=50
memcached.failureMode=true
#server
memcached.server.host=127.0.0.1
memcached.server.port=11211
memcached.server.weight=1

4. 上面可以设置多台memcached服务器,并指定weight权重。XMemcachedClientBuilder 有两个参数,第一个参数是server配置的list,第二个参数是每台服务器的权重。如:

 1 <bean id="memcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
 2         <constructor-arg>
 3             <list>
 4                 <bean class="java.net.InetSocketAddress">
 5                     <constructor-arg>
 6                             <value>${memcached.server1.host}</value>
 7                     </constructor-arg>
 8                     <constructor-arg>
 9                             <value>${memcached.server1.port}</value>
10                     </constructor-arg>
11                 </bean>
12                 <bean class="java.net.InetSocketAddress">
13                     <constructor-arg>
14                             <value>${memcached.server2.host}</value>
15                     </constructor-arg>
16                     <constructor-arg>
17                             <value>${memcached.server2.port}</value>
18                     </constructor-arg>
19                 </bean>
20             </list>
21         </constructor-arg>
22         <constructor-arg>
23             <list>
24                 <value>${memcached.server1.weight}</value>
25                 <value>${memcached.server2.weight}</value>
26             </list>
27         </constructor-arg>
28         <property name="connectionPoolSize" value="${memcached.connectionPoolSize}" />
29         <property name="commandFactory">
30                 <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
31         </property>
32         <property name="sessionLocator">
33                 <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
34         </property>
35         <property name="transcoder">
36                 <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
37         </property>
38     </bean>

相应properties配置变更为:

#server1
memcached.server1.host=127.0.0.1
memcached.server1.port=11211
memcached.server1.weight=1
#server2
memcached.server2.host=127.0.0.1
memcached.server2.port=11212
memcached.server2.weight=2

5. 下面简单提供了一份单台memcached服务器的存取操作及获取memcached状态信息的代码实例:

 1 /*
 2  * Copyright 2013 Alibaba.com All right reserved. This software is the
 3  * confidential and proprietary information of Alibaba.com ("Confidential
 4  * Information"). You shall not disclose such Confidential Information and shall
 5  * use it only in accordance with the terms of the license agreement you entered
 6  * into with Alibaba.com.
 7  */
 8 package com.yunos.tv.server.controller.web;
 9 
10 import java.net.InetSocketAddress;
11 import java.util.Iterator;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.concurrent.TimeoutException;
15 
16 import net.rubyeye.xmemcached.MemcachedClient;
17 import net.rubyeye.xmemcached.exception.MemcachedException;
18 
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Controller;
21 import org.springframework.web.bind.annotation.RequestMapping;
22 import org.springframework.web.bind.annotation.RequestMethod;
23 import org.springframework.web.bind.annotation.ResponseBody;
24 
25 /**
26  * 类DemoController.java的实现描述:TODO 类实现描述
27  * @author riqi 2013-6-28 上午10:48:58
28  */
29 
30 @Controller
31 @RequestMapping(value = "/demo")
32 public class DemoController extends BaseController {
33 
34     @Autowired
35     private MemcachedClient memcachedClient;
36 
37     @SuppressWarnings("unchecked")
38     @ResponseBody
39     @RequestMapping(value = "/hello", method = RequestMethod.GET)
40     public String helloWorld() {
41         try {
42             memcachedClient.set("string", 3600, "Hello World!");
43             Map<InetSocketAddress, Map<String, String>> memcachedStats = memcachedClient.getStats();
44 
45             String helloWord = memcachedClient.get("string");
46             StringBuilder strBuilder = new StringBuilder(helloWord);
47             strBuilder.append("\n");
48 
49             for (Iterator<?> statsIterator = memcachedStats.entrySet().iterator(); statsIterator.hasNext();) {
50                 Map.Entry<InetSocketAddress, Map<String, String>> entry = (Entry<InetSocketAddress, Map<String, String>>) statsIterator.next();
51 
52                 strBuilder.append(entry.getKey() + ":" + entry.getValue());
53                 strBuilder.append("\n");
54             }
55 
56             return strBuilder.toString();
57 
58         } catch (TimeoutException e) {
59             e.printStackTrace();
60         } catch (InterruptedException e) {
61             e.printStackTrace();
62         } catch (MemcachedException e) {
63             e.printStackTrace();
64         }
65 
66         return "";
67     }
68 }

浏览器输入:http://localhost:8080/demo/hello.htm

输出内容:

Hello World! 
/127.0.0.1:11211:{delete_hits=0, bytes=68, total_items=62, listen_disabled_num=0, auth_errors=0, evictions=0, version=1.4.4-14-g9c660c0, pointer_size=32, time=1373528740, incr_hits=0, threads=4, limit_maxbytes=67108864, bytes_read=169146, curr_connections=109, get_misses=0, bytes_written=2559445, connection_structures=110, cas_hits=0, delete_misses=0, total_connections=310, cmd_flush=0, uptime=1426, pid=6312, cas_badval=0, get_hits=61, curr_items=1, cas_misses=0, accepting_conns=1, cmd_get=61, cmd_set=62, incr_misses=0, auth_cmds=0, decr_misses=0, decr_hits=0, conn_yields=0}

 

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