`
Donald_Draper
  • 浏览: 980143 次
社区版块
存档分类
最新评论

Memcached failure客户端(Xmemcached)

阅读更多
memcached 安装配置:http://donald-draper.iteye.com/blog/2395580
memcached 命令:http://donald-draper.iteye.com/blog/2395628
Memcached客户端:http://donald-draper.iteye.com/blog/2396260
Memcached分布式客户端(Xmemcached):http://donald-draper.iteye.com/blog/2396313
从1.3版本开始,xmemcached支持failure模式。所谓failure模式是指,当一个memcached节点down掉的时候,
发往这个节点的请求将直接失败,而不是发送给下一个有效的memcached节点。具体可以看memcached的文档。
默认不启用failure模式,启用failure模式可以通过下列代码:
MemcachedClientBuilder builder=……
builder.setFailureMode(true);


不仅如此,xmemcached还支持主辅模式,你可以设置一个memcached的节点的备份节点,当主节点down掉的情况下,
会将本来应该发往主节点的请求转发给standby备份节点。使用备份节点的前提是启用failure模式。备份节点设置如下:
  
MemcachedClient builder=new XmemcachedClientBuilder(AddrUtil.
      getAddressMap("localhost:11211,localhost:11212 host2:11211,host2:11212"));


上面的例子,将localhost:11211的备份节点设置为localhost:11212,而将host2:11211的备份节点设置为host2:11212

本文的源代码可以从以下地址load:
https://github.com/Donaldhan/memcached-demo

启动4个memcached实例:
[memcached@donald ~]$  memcached -p 11211 -d -u memcached -l 192.168.126.128 -c 1024 
[memcached@donald ~]$  memcached -p 11212 -d -u memcached -l 192.168.126.128 -c 1024 
[memcached@donald ~]$  memcached -p 11213 -d -u memcached -l 192.168.126.128 -c 1024 
[memcached@donald ~]$  memcached -p 11214 -d -u memcached -l 192.168.126.128 -c 1024 
[memcached@donald ~]$ ps -ef | grep memcached
root       4159   1243  0 08:53 ?        00:00:00 sshd: memcached [priv]
memcach+   4161   4159  0 08:53 ?        00:00:00 sshd: memcached@pts/1
memcach+   4288      1  0 08:54 ?        00:00:00 memcached -p 11211 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4299      1  0 08:54 ?        00:00:00 memcached -p 11212 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4310      1  0 08:54 ?        00:00:00 memcached -p 11213 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4329      1  0 08:55 ?        00:00:00 memcached -p 11214 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4340   4163  0 08:55 pts/1    00:00:00 grep --color=auto memcached
[memcached@donald ~]$ 

规划主备节点如下:
11211主节点的备节点为11213,主节点11212的备节点为11214。

创建memcache属性文件memcached.properties:

############################
##memcached server ip address list
############standby############## 
standbyServerList=192.168.126.128:11211
############Distribute############## 
distributeServerList=192.168.126.128:11211 192.168.126.128:11212 192.168.126.128:11213
############Failure############## 
failureServerList=192.168.126.128:11211,192.168.126.128:11213 192.168.126.128:11212,192.168.126.128:11214
poolName=sidsock
poolSize=16



创建属性文件工具类:

package util;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
 * @author donald
 * 2017年9月29日
 * 下午10:21:34
 */
public class PropertiesUtil {
	private static final Logger log  = LoggerFactory.getLogger(PropertiesUtil.class);
	private static final String MEMCACHED_CONFIG_FILE = "memcached.properties";
    private static volatile PropertiesUtil instance = null;
    private static Properties properties = null;
    static{
    	 if (properties == null) {
    		 properties = new Properties();
         }
         try {
        	 InputStream inputStream = Thread.currentThread().getContextClassLoader()
        	            .getResourceAsStream(MEMCACHED_CONFIG_FILE);
        	 properties.load(inputStream);
         } catch (IOException e1) {
             e1.printStackTrace();
         }
    }
    
    /**
     * 
     * @return
     */
    public static synchronized PropertiesUtil getInstance() {
        if (instance == null) {
        	instance = new PropertiesUtil();
        }
        return instance;
    }
    /**
     * 
     * @param key
     * @return
     */
    public String getProperty(String key) {
        return properties.getProperty(key);
    }
    
    /**
     * 获取属性int值
     * @param key
     * @return
     */
    public Integer getInteger(String key) {
    	String value = properties.getProperty(key);
        return Integer.valueOf(value);
    }
    
    
    public static void main(String[] args) {
		log.info("serverList:"+PropertiesUtil.getInstance().getProperty("failureServerList"));
	}
}


创建memcache failure模式客户端:

package bootstrap.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.rubyeye.xmemcached.Counter;
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
import util.PropertiesUtil;

/**
 * Memcached Failure 模式客户端
 * @author donald
 * 2017年10月10日
 * 下午12:49:45
 */
public class MemcachedFailureClient {
	private static final Logger log = LoggerFactory.getLogger(MemcachedFailureClient.class);
	private static final String MEMCACHED_SERVER_LIST = "failureServerList";
	private static PropertiesUtil  propertiesUtil = PropertiesUtil.getInstance();
	private static volatile MemcachedFailureClient instance;
	private static MemcachedClientBuilder builder;
	private static MemcachedClient memcachedClient;
	static{
		String failureServerList = propertiesUtil.getProperty(MEMCACHED_SERVER_LIST);
		Map<InetSocketAddress, InetSocketAddress> serverAddressesMap = AddrUtil.getAddressMap(failureServerList);
		builder = new XMemcachedClientBuilder(serverAddressesMap);
		builder.setFailureMode(true);
		try {
			memcachedClient = builder.build();
		} catch (IOException e) {
			log.error("连接异常");
			e.printStackTrace();
		}
	}
	public static synchronized MemcachedFailureClient getInstance() {
		if (instance == null) {
			instance = new MemcachedFailureClient();
		}
		return instance;
	}

	/**
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(String key,Object value){
		return set(key, 0, value);
	}
	/**
	 * 
	 * @param key
	 * @param expire 过期时间秒
	 * @param value
	 * @return
	 */
	public boolean set(String key,int expire,Object value){
		boolean finish = false;
		try {
			finish = memcachedClient.set(key, expire, value);
		} catch (TimeoutException e) {
			log.error("set超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("set中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("set错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean add(String key,Object value){
		return add(key, 0, value);
	}
	/**
	 * 
	 * @param key
	 * @param expire 过期时间秒
	 * @param value
	 * @return
	 */
	public boolean add(String key,int expire,Object value){
		boolean finish = false;
		try {
			finish = memcachedClient.add(key, expire, value);
		} catch (TimeoutException e) {
			log.error("add超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("add中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("add错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @return
	 */
	public Object get(String key){
		Object value = null;
		try {
			value = memcachedClient.get(key);
		} catch (TimeoutException e) {
			log.error("get超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("get中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("get操作错误");
			e.printStackTrace();
		}
		return value;
	}
	/**
	 * 
	 * @param key
	 * @param appendValue
	 * @return
	 */
	public boolean append(String key,Object appendValue){
		boolean finish = false;
		try {
			finish = memcachedClient.append(key, appendValue);
		} catch (TimeoutException e) {
			log.error("append超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("append中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("append操作错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @param prependValue
	 * @return
	 */
	public boolean prepend(String key,Object prependValue){
		boolean finish = false;
		try {
			finish = memcachedClient.prepend(key, prependValue);
		} catch (TimeoutException e) {
			log.error("prepend超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("prepend中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("prepend操作错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean replace(String key,Object value){
		return replace(key, 0, value);
	}
	/**
	 * 
	 * @param key
	 * @param expire 过期时间秒
	 * @param value
	 * @return
	 */
	public boolean replace(String key,int expire,Object value){
		boolean finish = false;
		try {
			finish = memcachedClient.replace(key, expire, value);
		} catch (TimeoutException e) {
			log.error("replace超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("replace中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("replace错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @param expire
	 * @return
	 */
	public boolean touch(String key,int expire){
		boolean finish = false;
		try {
			finish = memcachedClient.touch(key, expire);
		} catch (TimeoutException e) {
			log.error("touch超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("touch中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("touch操作错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * @param key
	 * @param step
	 * @param defalut
	 * @return
	 */
	public long incr(String key,long step,long defalut){
		long value = 0;
		try {
			value = memcachedClient.incr(key, step, defalut);
		} catch (TimeoutException e) {
			log.error("incr超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("incr中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("incr操作错误");
			e.printStackTrace();
		}
		return value;
	}
	/**
	 * 
	 * @param key
	 * @param step
	 * @return
	 */
	public long incr(String key,long step){
		long value = 0;
		try {
			value = memcachedClient.incr(key, step);
		} catch (TimeoutException e) {
			log.error("incr超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("incr中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("incr操作错误");
			e.printStackTrace();
		}
		return value;
	}
	/**
	 * 
	 * @param key
	 * @param step
	 * @return
	 */
	public long decr(String key,long step){
		long value = 0;
		try {
			value = memcachedClient.decr(key, step);
		} catch (TimeoutException e) {
			log.error("decr超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("decr中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("decr操作错误");
			e.printStackTrace();
		}
		return value;
	}
	/**
	 * 
	 * @param key
	 * @return
	 */
	public long gets(String key){
		long sid = 0;
		try {
			GetsResponse<Integer> result = memcachedClient.gets(key);
			sid = result.getCas(); 
		} catch (TimeoutException e) {
			log.error("gets超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("gets中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("gets操作错误");
			e.printStackTrace();
		}
		return sid;
	}
	/**
	 * 
	 * @param key
	 * @param obj
	 * @return
	 */
	public boolean cas(String key,Object obj){
		boolean finish = false;
		long sid = gets(key);
		finish = cas(key, 0, obj, sid);
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @param expire
	 * @param obj
	 * @param sid key当前版本id
	 * @return
	 */
	public boolean cas(String key, int expire, Object obj, long sid){
		boolean finish = false;
		try {
			finish = memcachedClient.cas(key, expire, obj, sid);
		} catch (TimeoutException e) {
			log.error("cas超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("cas中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("cas操作错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * @param key
	 * @return
	 */
	public boolean delete(String key){
		boolean finish = false;
		try {
			finish = memcachedClient.delete(key);
		} catch (TimeoutException e) {
			log.error("delete超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("delete中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("delete操作错误");
			e.printStackTrace();
		}
		return finish;
	}
	/**
	 * 
	 * @param key
	 * @return
	 */
	public void deleteWithNoReply(String key){
		try {
			memcachedClient.deleteWithNoReply(key);
		} catch (InterruptedException e) {
			log.error("deleteWithNoReply中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("deleteWithNoReply操作错误");
			e.printStackTrace();
		}
	}
	/**
	 * @param key
	 * @return
	 */
	public Counter getCounter(String key) {
		return getCounter(key,0);
	}
	/**
	 * @param key
	 * @param init
	 * @return
	 */
	public Counter getCounter(String key,int init) {
		Counter counter = memcachedClient.getCounter(key,init);
		return counter;
	}
	/**
	 * 
	 */
	public void flushAll(){
		try {
			memcachedClient.flushAll();
		} catch (TimeoutException e) {
			log.error("flushAll超时");
			e.printStackTrace();
		} catch (InterruptedException e) {
			log.error("flushAll中断异常");
			e.printStackTrace();
		} catch (MemcachedException e) {
			log.error("flushAll操作错误");
			e.printStackTrace();
		}
	}
	public void shutdown(){
		try {
			memcachedClient.shutdown();
		} catch (IOException e) {
			log.error("客户端关闭异常");
			e.printStackTrace();
		}
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#finalize()
	 */
	@Override
	protected void finalize() {
		try {
			
			memcachedClient.shutdown();
		} catch (IOException e) {
			log.error("memcached 关闭客户端连接失败!");
			e.printStackTrace();
		}
	}
}



创建测试类:

package bootstrap;

import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import bootstrap.client.MemcachedFailureClient;
import net.rubyeye.xmemcached.Counter;
import net.rubyeye.xmemcached.exception.MemcachedException;

/**
 * Memcached  Failure 模式客户端测试类
 * @author 
 * donald 
 * 2017年10月10日 
 * 下午12:49:45
 */
public class MemcachedFailureClientTest {
	private static final Logger log = LoggerFactory.getLogger(MemcachedFailureClientTest.class);
	public static void main(String[] args) {
		MemcachedFailureClient memcachedClient = MemcachedFailureClient.getInstance();
		memcachedClient.set("name", 0, "donald");
		String value = (String) memcachedClient.get("name");
		log.info("set name={}", value);
		memcachedClient.delete("name");
		value = (String) memcachedClient.get("name");
		log.info("delete name={}", value);
		if (!memcachedClient.set("name", 0, "jamel")) {
			log.error("set error");
		}
		value = (String) memcachedClient.get("name");
		log.info("set name={}", value);
		if (memcachedClient.add("name", 0, "donald")) {
			log.error("Add error,key is existed");
		}
		value = (String) memcachedClient.get("name");
		log.info("name={}", value);
		if (!memcachedClient.replace("name", 0, "rain")) {
			log.error("replace error");
		}
		value = (String) memcachedClient.get("name");
		log.info("repalce name={}", value);
		memcachedClient.append("name", "-han");
		value = (String) memcachedClient.get("name");
		log.info("append name={}", value);
		memcachedClient.prepend("name", "0-");
		value = (String) memcachedClient.get("name");
		log.info("prepend name={}", value);
		memcachedClient.touch("name", 3);
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		value = (String) memcachedClient.get("name");
		log.info("after touch name={}", value);
		memcachedClient.deleteWithNoReply("name");
		memcachedClient.set("age", 0, "27");
		log.info("age={}", memcachedClient.get("age"));
		memcachedClient.incr("age", 2, 1);// age 增加2,age不存在,则为1
		memcachedClient.incr("age", 1);
		log.info("incr age={}", memcachedClient.get("age"));
		memcachedClient.decr("age", 2);
		log.info("decr age={}", memcachedClient.get("age"));
		if (!memcachedClient.cas("age", 27)) {
			log.error("cas error");
		}
		log.info("cas age={}", memcachedClient.get("age"));
		Counter counter = memcachedClient.getCounter("counter", 0);
		try {
			log.info("incrementAndGet counter,{}", counter.incrementAndGet());
			log.info("decrementAndGet counter,{}", counter.decrementAndGet());
			log.info("addAndGet counter,{}", counter.addAndGet(3));
		} catch (MemcachedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
		memcachedClient.shutdown();
	}
}



运行测试类,控制台输出如下:
12:45:18.135 [main] INFO  net.rubyeye.xmemcached.XMemcachedClient 729- XMemcachedClient is using Text protocol
12:45:18.454 [main] INFO  net.rubyeye.xmemcached.XMemcachedClient 744- XMemcachedClient in failure mode.
12:45:18.456 [main] INFO  com.google.code.yanf4j.nio.impl.SelectorManager 37- Creating 4 reactors...
12:45:18.698 [main] INFO  com.google.code.yanf4j.core.impl.AbstractController 377- The Controller started at localhost/127.0.0.1:0 ...
12:45:27.827 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 253- Add a session: 192.168.126.128:11211
12:45:36.882 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 290- Add a standby session: 192.168.126.128:11213 for 192.168.126.128:11211
12:45:45.904 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 253- Add a session: 192.168.126.128:11212
12:45:54.927 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 290- Add a standby session: 192.168.126.128:11214 for 192.168.126.128:11212
12:45:54.993 [main] INFO  bootstrap.MemcachedFailureClientTest 25- set name=donald
12:45:55.014 [main] INFO  bootstrap.MemcachedFailureClientTest 28- delete name=null
12:45:55.015 [main] INFO  bootstrap.MemcachedFailureClientTest 33- set name=jamel
12:45:55.016 [main] INFO  bootstrap.MemcachedFailureClientTest 38- name=jamel
12:45:55.017 [main] INFO  bootstrap.MemcachedFailureClientTest 43- repalce name=rain
12:45:55.018 [main] INFO  bootstrap.MemcachedFailureClientTest 46- append name=rain-han
12:45:55.020 [main] INFO  bootstrap.MemcachedFailureClientTest 49- prepend name=0-rain-han
12:45:58.022 [main] INFO  bootstrap.MemcachedFailureClientTest 57- after touch name=null
12:45:58.023 [main] INFO  bootstrap.MemcachedFailureClientTest 60- age=27
12:45:58.045 [main] INFO  bootstrap.MemcachedFailureClientTest 63- incr age=30
12:45:58.047 [main] INFO  bootstrap.MemcachedFailureClientTest 65- decr age=28
12:45:58.050 [main] INFO  bootstrap.MemcachedFailureClientTest 69- cas age=27
12:45:58.054 [main] INFO  bootstrap.MemcachedFailureClientTest 72- incrementAndGet counter,1
12:45:58.055 [main] INFO  bootstrap.MemcachedFailureClientTest 73- decrementAndGet counter,0
12:45:58.056 [main] INFO  bootstrap.MemcachedFailureClientTest 74- addAndGet counter,3
12:45:58.089 [Xmemcached-Reactor-2] INFO  com.google.code.yanf4j.core.impl.AbstractController 368- Remove a session: 192.168.126.128:11211
12:45:58.090 [Xmemcached-Reactor-1] INFO  com.google.code.yanf4j.core.impl.AbstractController 368- Remove a session: 192.168.126.128:11212
12:45:58.091 [main] INFO  com.google.code.yanf4j.core.impl.AbstractController 486- Controller has been stopped.


从控制台输出来看192.168.126.128:11211的备节点为192.168.126.128:11213,
192.168.126.128:11212的备节点为192.168.126.128:11214。

分别登陆11211,11212,11213,11214实例:
[memcached@donald ~]$ telnet 192.168.126.128 11211
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name
END
get age
END
get counter
VALUE counter 0 1
3
END


[memcached@donald ~]$ telnet 192.168.126.128 11212
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.

get name
END
get age
VALUE age 512 1

END
get counter
END


[memcached@donald ~]$ telnet 192.168.126.128 11213
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name
END
get age
END
get counter
END

[memcached@donald ~]$ telnet 192.168.126.128 11214
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get nage
END
get name
END
get age
END
get counter
END


从上面可以看出age和counter属性存储在主节点11211和11212上,对应的备节点11213和11214上面不存储数据。



新建测试类2:
package bootstrap;

import java.util.concurrent.TimeoutException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import bootstrap.client.MemcachedFailureClient;
import net.rubyeye.xmemcached.Counter;
import net.rubyeye.xmemcached.exception.MemcachedException;

/**
 * Memcached Failure 模式客户端测试类
 * @author 
 * donald 
 * 2017年10月10日 
 * 下午12:49:45
 */
public class MemcachedFailureClient2Test {
	private static final Logger log = LoggerFactory.getLogger(MemcachedFailureClient2Test.class);
	public static void main(String[] args) {
		MemcachedFailureClient memcachedClient = MemcachedFailureClient.getInstance();
		memcachedClient.flushAll();
		memcachedClient.set("name", 0, "donald");
		String value = (String) memcachedClient.get("name");
		log.info("set name={}", value);
		memcachedClient.set("age", 0, "27");
		log.info("age={}", memcachedClient.get("age"));
		memcachedClient.incr("age", 1);
		log.info("incr age={}", memcachedClient.get("age"));//debug breakpoint
		memcachedClient.set("sex", "man");
		try {
			Thread.sleep(180000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		Counter counter = memcachedClient.getCounter("counter", 0);
		try {
			log.info("incrementAndGet counter,{}", counter.incrementAndGet());
			log.info("decrementAndGet counter,{}", counter.decrementAndGet());
			log.info("addAndGet counter,{}", counter.addAndGet(3));
		} catch (MemcachedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			e.printStackTrace();
		}
		memcachedClient.shutdown();
	}
}


在debug breakpoint添加断点,并以debug模式运行,在到达断点时kill 11211 实例,
控制台输出:

13:08:33.393 [main] INFO  net.rubyeye.xmemcached.XMemcachedClient 729- XMemcachedClient is using Text protocol
13:08:33.434 [main] INFO  net.rubyeye.xmemcached.XMemcachedClient 744- XMemcachedClient in failure mode.
13:08:33.435 [main] INFO  com.google.code.yanf4j.nio.impl.SelectorManager 37- Creating 4 reactors...
13:08:33.452 [main] INFO  com.google.code.yanf4j.core.impl.AbstractController 377- The Controller started at localhost/127.0.0.1:0 ...
13:08:42.486 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 253- Add a session: 192.168.126.128:11211
13:08:51.505 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 290- Add a standby session: 192.168.126.128:11213 for 192.168.126.128:11211
13:09:00.526 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 253- Add a session: 192.168.126.128:11212
13:09:09.549 [Xmemcached-Reactor-0] INFO  com.google.code.yanf4j.core.impl.AbstractController 290- Add a standby session: 192.168.126.128:11214 for 192.168.126.128:11212
13:09:09.563 [main] INFO  bootstrap.MemcachedFailureClient2Test 26- set name=donald
13:09:09.566 [main] INFO  bootstrap.MemcachedFailureClient2Test 28- age=27
13:09:09.568 [main] INFO  bootstrap.MemcachedFailureClient2Test 30- incr age=28

分别登陆11211,11212,11213,11214实例:

[memcached@donald ~]$ telnet 192.168.126.128 11211
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name    
END
get age
END
[memcached@donald ~]$ 

[memcached@donald ~]$ telnet 192.168.126.128 11212
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name
VALUE name 0 6
donald
END
get age
VALUE age 0 2
28
END


[memcached@donald ~]$ telnet 192.168.126.128 11213
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name    
END
get age
END



[memcached@donald ~]$ telnet 192.168.126.128 11214
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name
END
get age
END


关闭11211实例:
[memcached@donald ~]$ ps -ef | grep memcached
root       4159   1243  0 08:53 ?        00:00:00 sshd: memcached [priv]
memcach+   4161   4159  0 08:53 ?        00:00:00 sshd: memcached@pts/1
memcach+   4288      1  0 08:54 ?        00:00:00 memcached -p 11211 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4299      1  0 08:54 ?        00:00:00 memcached -p 11212 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4310      1  0 08:54 ?        00:00:00 memcached -p 11213 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4329      1  0 08:55 ?        00:00:00 memcached -p 11214 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4340   4163  0 08:55 pts/1    00:00:00 grep --color=auto memcached
[memcached@donald ~]$ kill -9 4288
[memcached@donald ~]$ kill -9 4288
-bash: kill: (4288) - No such process
[memcached@donald ~]$ ps -ef | grep memcached
root       4159   1243  0 08:53 ?        00:00:00 sshd: memcached [priv]
memcach+   4161   4159  0 08:53 ?        00:00:00 sshd: memcached@pts/1,pts/2,pts/3,pts/4,pts/5
memcach+   4299      1  0 08:54 ?        00:00:04 memcached -p 11212 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4310      1  0 08:54 ?        00:00:04 memcached -p 11213 -d -u memcached -l 192.168.126.128 -c 1024
memcach+   4329      1  0 08:55 ?        00:00:04 memcached -p 11214 -d -u memcached -l 192.168.126.128 -c 1024
memcach+  10588   4163  0 13:12 pts/1    00:00:00 grep --color=auto memcached
[memcached@donald ~]$ 


继续执行程序:
分别在11212,11213,11214,获取sex和counter属性:

[memcached@donald ~]$ telnet 192.168.126.128 11211
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name    
END
get age
END
Connection closed by foreign host.
[memcached@donald ~]$ 

[memcached@donald ~]$ telnet 192.168.126.128 11212
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name
VALUE name 0 6
donald
END
get age
VALUE age 0 2
28
END
get sex   
END
get counter 
END

[memcached@donald ~]$ telnet 192.168.126.128 11213
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name    
END
get age
END
get sex
VALUE sex 0 3
man
END
get counter
VALUE counter 0 1
3
END

[memcached@donald ~]$ telnet 192.168.126.128 11214
Trying 192.168.126.128...
Connected to 192.168.126.128.
Escape character is '^]'.
get name
END
get age
END
get sex
END
get counter
END


控制台输出:
13:12:02.932 [Xmemcached-Reactor-2] WARN  com.google.code.yanf4j.core.impl.AbstractController 363- Client in failure mode,we don't remove session 192.168.126.128:11211
13:12:04.935 [Heal-Session-Thread] INFO  com.google.code.yanf4j.core.impl.AbstractController 129- Trying to connect to 192.168.126.128:11211 for 1 times
13:12:05.962 [Heal-Session-Thread] ERROR com.google.code.yanf4j.core.impl.AbstractController 173- Reconnected to /192.168.126.128:11211 fail
13:12:05.993 [Xmemcached-Reactor-0] ERROR com.google.code.yanf4j.core.impl.AbstractController 562- Exception occured in controller
java.io.IOException: Connect to 192.168.126.128:11211 fail,Connection refused: no further information
	at net.rubyeye.xmemcached.impl.MemcachedConnector.onConnect(MemcachedConnector.java:426)
	at com.google.code.yanf4j.nio.impl.Reactor.dispatchEvent(Reactor.java:331)
	at com.google.code.yanf4j.nio.impl.Reactor.run(Reactor.java:180)
13:12:05.993 [Xmemcached-Reactor-0] ERROR remoting 353- Reactor dispatch events error
java.io.IOException: Connect to 192.168.126.128:11211 fail,Connection refused: no further information
	at net.rubyeye.xmemcached.impl.MemcachedConnector.onConnect(MemcachedConnector.java:426)
	at com.google.code.yanf4j.nio.impl.Reactor.dispatchEvent(Reactor.java:331)
	at com.google.code.yanf4j.nio.impl.Reactor.run(Reactor.java:180)
13:12:07.970 [Heal-Session-Thread] INFO  com.google.code.yanf4j.core.impl.AbstractController 129- Trying to connect to 192.168.126.128:11211 for 2 times
13:12:08.975 [Heal-Session-Thread] ERROR com.google.code.yanf4j.core.impl.AbstractController 173- Reconnected to /192.168.126.128:11211 fail
13:12:08.975 [Xmemcached-Reactor-0] ERROR com.google.code.yanf4j.core.impl.AbstractController 562- Exception occured in controller
java.io.IOException: Connect to 192.168.126.128:11211 fail,Connection refused: no further information
	at net.rubyeye.xmemcached.impl.MemcachedConnector.onConnect(MemcachedConnector.java:426)
	at com.google.code.yanf4j.nio.impl.Reactor.dispatchEvent(Reactor.java:331)
	at com.google.code.yanf4j.nio.impl.Reactor.run(Reactor.java:180)
13:12:08.976 [Xmemcached-Reactor-0] ERROR remoting 353- Reactor dispatch events error
java.io.IOException: Connect to 192.168.126.128:11211 fail,Connection refused: no further information
	at net.rubyeye.xmemcached.impl.MemcachedConnector.onConnect(MemcachedConnector.java:426)
	at com.google.code.yanf4j.nio.impl.Reactor.dispatchEvent(Reactor.java:331)
	at com.google.code.yanf4j.nio.impl.Reactor.run(Reactor.java:180)
13:12:09.573 [main] INFO  bootstrap.MemcachedFailureClient2Test 39- incrementAndGet counter,1
13:12:09.574 [main] INFO  bootstrap.MemcachedFailureClient2Test 40- decrementAndGet counter,0
13:12:09.575 [main] INFO  bootstrap.MemcachedFailureClient2Test 41- addAndGet counter,3
13:12:09.577 [Xmemcached-Reactor-1] INFO  com.google.code.yanf4j.core.impl.AbstractController 368- Remove a session: 192.168.126.128:11212
13:12:09.578 [main] INFO  com.google.code.yanf4j.core.impl.AbstractController 486- Controller has been stopped.



从上面可以看出,当主节点11211宕机时,其备节点升级为主节点。
0
0
分享到:
评论

相关推荐

    Java的开源高性能memcached客户端XMemcached.zip

    Xmemcached是基于java nio实现的高性能可扩展的memcached客户端。它的主要特点: 高性能,稳定可靠,已经在众多公司的众多项目里得到应用。 功能完备:客户端分布式、权重、最新最完整的协议支持。 ...

    memcached C++ 客户端 源码

    标题"memcached C++ 客户端 源码"表明了这是一个关于使用C++编写的memcached客户端的源代码库。memcached是一款高性能、分布式的内存对象缓存系统,常用于减轻数据库负载,提高Web应用性能。C++客户端则为开发者提供...

    易语言Memcached协议客户端模块

    易语言Memcached协议客户端模块源码,Memcached协议客户端模块,Initialize,Connect,Timeout,Exptime,IsRunning,RunStorageCommand,AnalyzeMessage,Set,Add,Replace,Delete,Incr,Decr,Version,Get,GetMulti,...

    memcached客户端(三种客户端)

    在Java开发中,有三种主流的Memcached客户端库供开发者选择:官方的基于传统阻塞IO的客户端、Dustin Sallings实现的基于Java NIO的Spymemcached以及XMemcached。 1. **官方客户端** 官方提供的客户端是由Greg ...

    Memcached 客户端 服务端 管理工具

    客户端是应用与Memcached交互的接口,常见的客户端库有libmemcached(C语言)、pylibmc(Python)和memcached-client(Java)等。客户端的主要任务包括: - **连接管理**:建立与服务端的TCP连接或Unix域套接字连接...

    Memcached学习笔记 — 第四部分:Memcached Java 客户端-gwhalin(1)-介绍及使用

    本篇学习笔记将重点介绍如何在Java环境中使用gwhalin提供的Memcached客户端进行开发。gwhalin的Memcached Java客户端是一个轻量级、高性能的库,使得Java开发者可以轻松地与Memcached服务器进行交互。 ### 一、...

    memcached java客户端驱动包

    **Memcached Java客户端驱动包详解** Memcached是一种高性能的分布式内存对象缓存系统,用于减少数据库负载,提高网站性能。Java连接Memcached的驱动包使得Java开发者能够方便地与Memcached进行交互,实现数据的...

    易语言源码易语言Memcached协议客户端模块源码.rar

    在这个"易语言源码易语言Memcached协议客户端模块源码.rar"压缩包中,我们找到了一个易语言实现的Memcached协议客户端模块的源代码。Memcached是一种高性能、分布式内存对象缓存系统,常用于减轻数据库负载,提高...

    memcached之客户端memadmin安装

    "memcached之客户端memadmin安装" 这个标题涉及到的是关于`memcached`的一个客户端工具——`memadmin`的安装过程。`memcached`是一种高性能的分布式内存对象缓存系统,常用于减轻数据库负载,提高Web应用性能。而`...

    memcached客户端文件及服务端

    在描述中提到的“客户端调用dll文件”,指的是 Memcached 的客户端库,通常用于编程语言中与服务端进行通信。例如,如果你使用的是C++,可能会需要`libmemcached.dll`这样的动态链接库。客户端库提供了一套API,允许...

    memcached客户端和服务端程序和jar包

    XMemcached 是另一个流行的 Memcached Java 客户端,它提供了更丰富的功能,如支持多线程、异步操作等。在项目中引入 XMemcached 的 JAR 包: ```xml &lt;groupId&gt;com.googlecode.xmemcached&lt;/groupId&gt; ...

    内存缓存服务器(memcached)客户端比较报告[收集].pdf

    该系统基于键值对的哈希映射,由C语言实现,允许各种编程语言的客户端通过特定的memcached协议与其交互。 在Java客户端中,Spymemcached和Xmemcached是两个最受欢迎的选项。为了评估这些客户端的性能和适用性,进行...

    memcached客户端所需jar包

    3. **Java_memcached-release_2.6.6**:这个文件名可能是另一个Java Memcached客户端版本,比如Xmemcached。Xmemcached是一个高性能、线程安全的Java Memcached客户端,它支持异步操作,提供全面的API和配置选项。...

    memcached C++客户端的最简单实现

    老大想用memcached,但是不喜欢网上的那些库。于是我写了这份测试代码。这里面对socket各种不正常状态基本没有做处理,不适合直接使用。但是它绝对足够简单方便,很适合打算用memcached但又不喜欢用那些庞大复杂的...

    memcached的三种java客户端jar

    3.xmemcached,XMemcached也使用得比较广泛,而且有较详细的中文API文档,具有如下特点:高性 能、支持完整的协议、支持客户端分布、允许设置节点权重、动态增删节点、支持JMX、与Spring框架和Hibernate-memcached的...

    MemCached Provider客户端

    **Memcached 提供者客户端详解** Memcached 是一个高性能、分布式的内存对象缓存系统,用于在分布式环境中存储和检索数据。它通过减少对数据库的访问来提高网站和应用程序的性能。C# 中的 Memcached 客户端库,如 ...

    Memcached 服务端,客户端,C#

    Memcached 服务端是运行在服务器上的程序,负责接收客户端的请求并处理。它使用基于UDP和TCP的简单文本协议,这使得Memcached易于实现且高效。服务端会根据键来存储和检索数据,支持LRU(Least Recently Used)策略...

    go语言memcached客户端.zip

    在“go语言memcached客户端.zip”这个压缩包中,可能包含了一个或多个Go语言编写的Memcached客户端库的源代码。由于提供的文件名"flghd"并不明确,我们无法直接解析其具体内容,但可以基于通用的Go Memcached客户端...

    C# memcached客户端源码 测试例子

    本压缩包文件“memcacheddotnet”提供的就是C#环境下连接和操作Memcached的客户端源码,以及相关的测试示例,这对于理解和应用C#与Memcached的集成至关重要。 首先,我们需要了解C#中的Memcached客户端是如何工作的...

    memcached客户端包memcached-1.4.11.tar.gz

    **Memcached 客户端包详解** `memcached-1.4.11.tar.gz` 是一个用于安装 Memcached 客户端库的压缩包,版本号为 1.4.11。Memcached 是一个高性能、分布式内存对象缓存系统,它广泛应用于Web应用中,用于减轻数据库...

Global site tag (gtag.js) - Google Analytics