1.上传安装包
libevent-2.0.22-stable.tar.gz
memcached-1.4.24.tar.gz
2.安装libevent
创建目录
mkdir -p /apps/install/libevent
解压
tar -zxvf libevent-2.0.22-stable.tar.gz -C /apps/install/libevent
安装
cd libevent-2.0.22-stable/
./configure
--prefix=/usr/local/libevent/
make
make install
3.安装memcached
创建目录
mkdir -p /apps/install/memcached
解压
tar -zxvf /apps/softWare/memcached/memcached-1.4.24.tar.gz -C /apps/install/memcahced/
安装
cd memcached-1.4.24/
./configure
--prefix=/usr/local/memcached/
--with-libevent=/usr/local/libevent/
make
make install
4.启动memcached
/usr/local/memcached/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
5.测试
a.安装telnet,telnet-server
查看是否有telnet
rpm -qa|grep telnet
没有
安装telnet
yum -y install telnet
测试
telnet localhost 12000
缓存key1
set key1 0 60 4
helloworld
获取缓存key1
get key1
退出编辑
ctrl+]
退出telnet
quit
6.集群测试
服务器:
192.168.158.129
192.168.158.128
在这2台服务器上分别开启2个端口
192.168.158.129:12011
192.168.158.129:12012
192.168.158.128:12021
192.168.158.128:12022
分别启动这4个实例
/usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.158.128 -p 12011 -c 256 -P /tmp/memcached1.pid
/usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.158.128 -p 12012 -c 256 -P /tmp/memcached2.pid
/usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.158.129 -p 12021 -c 256 -P /tmp/memcached1.pid
/usr/local/memcached/bin/memcached -d -m 10 -u root -l 192.168.158.129 -p 12022 -c 256 -P /tmp/memcached2.pid
集群配置
<?xml version="1.0" encoding="UTF-8"?> <memcached> <client name="mclient0" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0"> <errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler> </client> <client name="mclient0-back" compressEnable="true" defaultEncoding="UTF-8" socketpool="pool0-back"> <errorHandler>com.alisoft.xplatform.asf.cache.memcached.MemcachedErrorHandler</errorHandler> </client> <socketpool name="pool0" failover="true" initConn="20" minConn="20" maxConn="100" maintSleep="0" nagle="false" socketTO="3000" aliveCheck="true"> <servers>192.168.158.129:12011,192.168.158.128:12021</servers> </socketpool> <socketpool name="pool0-back" failover="true" initConn="20" minConn="20" maxConn="100" maintSleep="0" nagle="false" socketTO="3000" aliveCheck="true"> <servers>192.168.158.129:12012,192.168.158.128:12022</servers> </socketpool> <cluster name="cluster1" mode="active"> <!-- //mode = active,standby --> <memCachedClients>mclient0,mclient0-back</memCachedClients> </cluster> </memcached>
主要API
package guo.test.MemcachedDemo; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.alisoft.xplatform.asf.cache.ICacheManager; import com.alisoft.xplatform.asf.cache.IMemcachedCache; import com.alisoft.xplatform.asf.cache.memcached.CacheUtil; import com.alisoft.xplatform.asf.cache.memcached.MemcachedCacheManager; /** * @author * @create 2015-06-12 * @version 1.0 * @Function 基于alimemcachedClient 构建memcached集群的工具类,集群配置详见:memcached_cluster.xml. */ public class AliClusterCacheUtil { private static ICacheManager<IMemcachedCache> manager; public static IMemcachedCache cache; //开放cache,仅为了部分同志存在个性化需求。 static{ manager = CacheUtil.getCacheManager(IMemcachedCache.class,MemcachedCacheManager.class.getName()); String configFile=Config.getConfig("memcached_session_configPath"); manager.setConfigFile(configFile); manager.start(); cache = manager.getCache("mclient0"); } /** * 保存数据 * @param key * @param value * @return */ public static void put(String key,Object value){ cache.put(key, value); } /** * 保存有有效期的数据 * @param key * @param value * @param 设置有效期为距离当前时间后TTL秒。 * @return */ public static void put(String key,Object value,int TTL){ cache.put(key, value, TTL); } /** * 保存有有效期的数据 * @param key * @param value * @param 有效期(取的是客户端时间) * @return */ public static void put(String key,Object value,Date date){ cache.put(key, value, date); } /** * 获取缓存数据 * @param key * @return Object */ public static Object get(String key){ return cache.get(key); } /** * 移出缓存数据 * @param key * @return Object */ public static Object remove(String key){ return cache.remove(key); } /** * 删除所有缓存内的数据 * @return boolean */ public static boolean clear(){ return cache.clear(); } /** * 是否包含了指定key的数据 * @param key * @return boolean */ public static boolean containsKey(String key){ return cache.containsKey(key); } /** * 释放Cache占用的资源 */ public static void destroy(){ cache.destroy(); } /** * 降低memcache的交互频繁造成的性能损失,因此采用本地cache结合memcache的方式 * @param key * @param 本地缓存该数据有效秒数 * @return */ public static Object get(String key,int localTTL){ return cache.get(key, localTTL); } /** * 动态重新载入配置文件,可用于实现动态扩容。 */ public static void reload(String configfile){ manager.reload(configfile); } public static void main(String[] args) { AliClusterCacheUtil.put("guo", "you are the sun"); String key = (String) AliClusterCacheUtil.get("guo"); System.out.println("get the value is:"+key); List list = new ArrayList(); list.add("a"); list.add("b"); AliClusterCacheUtil.put("list", list); List tmp = (List) AliClusterCacheUtil.get("list"); System.out.println("get the list is:"+tmp); } }
运行
AliClusterCacheUtil.java 中的main方法进行测试,能够正常缓存与获取,当遇到异常,一台缓存服务器宕机或一个实例异常,可以正常切换。
集群满足要求。
MemcachedDemo 在百度云盘中
地址: http://pan.baidu.com/s/1nuAcuPN
可参考:
http://www.tashan10.com/memcachedda-jian-huan-cun-xi-tong/
http://www.cnblogs.com/wayne173/p/5652034.html