安装memcached的小事

今天 ,心血来潮 想安装下memcached 先给自己测试一下。

首先 从memcached 官网中下载 memcached-1.4.15 

首先memcached 安装需要libevent

从官网中下载:

libevent-2.0.21-stable.tar.gz 

解压 安装:

# tar zxvf libevent-2.0.21-stable.tar.gz  # cd libevent-1.2
# ./configure --prefix=/usr
# make
# make install

判断memcached是否安装成功

# ls -al /usr/lib | grep libevent

结果如下:

安装memcached的小事_第1张图片

.安装memcached,同时需要安装中指定libevent的安装位置

# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
# ./configure --with-libevent=/usr
# make
# make install

测试是否成功安装memcached:

# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug

这个时候 我们需要来启动memcached了。

usr/local/bin/memcached -d -m 100 -u root -p 12000 -c 1000 -P /tmp/memcached.pid

我们来分析下参数代表的含义:

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

当我启动进程 调用 

memcached 已经启动了 。

好 现在我想用telnet命令来测试一下

可是 当我用telnet时 发现竟然显示 

-bash: telent: command not found
马上百度之

很多人说是因为没有装 telnet服务 。

所以调用yum命令来安装 telnet

um -y install telnet
另外需要配置:
[root@localhost memcached-1.4.15]# vi /etc/xinetd.d/telnet 

# default: on
# description: The telnet server serves telnet sessions; it uses \
#       unencrypted username/password pairs for authentication.
service telnet
{
        flags           = REUSE
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/sbin/in.telnetd
        log_on_failure  += USERID
        disable         = no
}
这里需要将disable 从 yes 修改成 no

需要启动服务

cd /etc/rc.d/init.d/
service xinetd restart
现在 我用 xmemcached来测试一下

测试代码如下:

package nio;


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 java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * 测试 cas
 * User: zhangyong
 * Date: 12-12-2
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
public class SimpleTest {

    public static void main(String[] args) throws IOException {
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(
                AddrUtil.getAddresses("192.168.56.1:12001"));
        MemcachedClient memcachedClient = builder.build();
        try {
            memcachedClient.set("hello", 0, "Hello,xmemcached");
            String value = memcachedClient.get("hello");
            System.out.println("hello=" + value);

            //尝试添加 看看返回什么
            boolean  flag = memcachedClient.add("hello", 0, "Hello,xmemcached");
            System.out.println("flag==" + flag);

            memcachedClient.delete("hello");
            value = memcachedClient.get("121709_300");
            System.out.println("value=" + value);

            HashMap map = new HashMap();
            map.put("key" ,"123");
            memcachedClient.set("map",0,map) ;

            System.out.println(memcachedClient.get("map").getClass());

        } catch (MemcachedException e) {
            System.err.println("MemcachedClient operation fail");
            e.printStackTrace();
        } catch (TimeoutException e) {
            System.err.println("MemcachedClient operation timeout");
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        try {
//            //close memcached client
//            memcachedClient.shutdown();
//        } catch (IOException e) {
//            System.err.println("Shutdown MemcachedClient fail");
//            e.printStackTrace();
//        }
    }

}
结果如下:

[WARN ]2013-03-09 20:14:19416 net.rubyeye.xmemcached.XMemcachedClient XMemcachedClient use Text protocol
[INFO ]2013-03-09 20:14:19465 com.google.code.yanf4j.nio.impl.SelectorManager Creating 8 reactors...
[WARN ]2013-03-09 20:14:19507 com.google.code.yanf4j.core.impl.AbstractController The Controller started at localhost/127.0.0.1:0 ...
[WARN ]2013-03-09 20:14:19521 com.google.code.yanf4j.core.impl.AbstractController Add a session: 192.168.56.1:12001
hello=Hello,xmemcached
flag==false
value=null
class java.util.HashMap

ok 测试工作准备就绪 可以开始 写个类似memcached-client的小东西了 练练手 下两周的计划。




你可能感兴趣的:(memcached,libevent,实践)