笔记:搭建memcached集群环境并通过java客户端访问它

今天在CentOS 5.7 上搭建了memcahced集群环境,并通过Xmemcached这个java client写了一个程序。步骤如下:

1.
CentOS上添加EPEL仓库,这样才能用yum安装memcached

su -c 'rpm -Uvh http://download.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm'

2.
安装memcached

yum install memcached

3.
在另一台机器上重复#1, #2,安装另一个memcached; 当然这不是必须的,你也可以在一台机上跑两个memcached实例。 具体参考memcached的参数

4.
启动memcached,即成memcached cluster

service start memcached -l 192.68.1.1 #本机ip

service start memcached -l 192.68.1.2

5.
客户端通过Xmemcached访问这个集群


http://code.google.com/p/xmemcached/搞定jar包或maven依赖

			XMemcachedClient client = new XMemcachedClient(
					AddrUtil.getAddresses("192.168.1.1:11211 192.168.1.2:11211"));

			String userId = "1";
			Object user = client.get(userId);
			if (user == null) {
				user = getNameFromDB(userId);
				client.set(userId, 5, user);
			}


			client.shutdown();

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.