Home > Database > Mysql Tutorial > Memcached服务端安装

Memcached服务端安装

WBOY
Release: 2016-06-07 16:34:51
Original
1000 people have browsed it

libevent安装 cd /tmpwget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gztar -zxf libevent-1.4.14b-stable.tar.gzcd libevent-1.4.14b-stable./configure --prefix=/opt/libevent/makemake install Memcached安装 cd /tmpwget -N http://www.m

libevent安装

<code class="language-text">cd /tmp
wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar -zxf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/opt/libevent/
make
make install
</code>
Copy after login

Memcached安装

<code class="language-text">cd /tmp
wget -N http://www.memcached.org/files/memcached-1.4.17.tar.gz
tar -zxf memcached-1.4.17.tar.gz
cd memcached-1.4.17
./configure --prefix=/opt/memcached --enable-64bit --with-libevent=/opt/libevent
make
make install
</code>
Copy after login

编写Memcached启动脚本

<code class="language-text">vi /etc/init.d/memcached
#!/bin/sh
# chkconfig: - 50 50
# description: memcached
# 以下要自己配置
CMD=/opt/memcached/bin/memcached
MEMORY=128
USER=root
HOST=master
PORT=11211
CONNECTION=32
PID=/tmp/memcached.pid
start() {
    echo -n $"memcached starting... "  
    $CMD -d -m $MEMORY -u $USER -l $HOST -p $PORT -c $CONNECTION -P $PID
    echo
}
stop() {
    echo -n $"memcached shutting down... "  
    kill `cat $PID`
    echo
}
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
    echo $"Usage: $0 {start|stop|restart}"  
    exit 1
esac
exit 0
</code>
Copy after login

将memcached加入系统启项

<code class="language-text">chmod +x /etc/init.d/memcached
chkconfig --add /etc/init.d/memcached
chkconfig --level 35 memcached on
chkconfig --level 35 iptables off
</code>
Copy after login

启动Memcached

<code class="language-text">/opt/memcached/bin/memcached -d -m 128 -u root -l master -p 11211 -c 32 -P /tmp/memcached.pid
service memcached restart
</code>
Copy after login

基于上面,其他一些选项的含义

<code class="language-text">-d  指定memcached进程作为一个守护进程启动
-m <num>    指定分配给memcached使用的内存,单位是MB
-u <username>   运行memcached的用户
-l <ip_addr>    监听的服务器IP地址,如果有多个地址的话,使用逗号分隔,格式可以为“IP地址:端口号”,例如:-l 指定192.168.0.184:19830,192.168.0.195:13542;端口号也可以通过-p选项指定
-p <num>    Memcached监听的端口,要保证该端口号未被占用
-c <num>    设置最大运行的并发连接数,默认是1024
-R <num>    为避免客户端饿死(starvation),对连续达到的客户端请求数设置一个限额,如果超过该设置,会选择另一个连接来处理请求,默认为20
-k  设置锁定所有分页的内存,对于大缓存应用场景,谨慎使用该选项
-P  保存memcached进程的pid文件
-s <file>   指定Memcached用于监听的UNIX socket文件
-a <perms>  设置-s选项指定的UNIX socket文件的权限
-U <num>    指定监听UDP的端口,默认11211,0表示关闭
-M  当内存使用超出配置值时,禁止自动清除缓存中的数据项,此时Memcached不可以,直到内存被释放
-r  设置产生core文件大小
-f <factor> 用于计算缓存数据项的内存块大小的乘数因子,默认是1.25
-n  为缓存数据项的key、value、flag设置最小分配字节数,默认是48
-C  禁用CAS
-h  显示Memcached版本和摘要信息
-v  输出警告和错误信息
-vv 打印信息比-v更详细:不仅输出警告和错误信息,也输出客户端请求和响应信息
-i  打印libevent和Memcached的licenses信息
-t <threads>    指定用来处理请求的线程数,默认为4
-D <char>   用于统计报告中Key前缀和ID之间的分隔符,默认是冒号“:”
-L  尝试使用大内存分页(pages)
-B <proto>  指定使用的协议,默认行为是自动协商(autonegotiate),可能使用的选项有auto、ascii、binary。
-I <size>   覆盖默认的STAB页大小,默认是1M
-F  禁用flush_all命令
-o <options>    指定逗号分隔的选项,一般用于用于扩展或实验性质的选项
</options></size></proto></char></threads></factor></num></perms></file></num></num></num></ip_addr></username></num></code>
Copy after login

停止Memcached

<code class="language-text">ps -ef | grep memcached
kill -9 <pid>
</pid></code>
Copy after login

Java客户端 - XMemcached客户端

spring.xml

<code class="language-text"><!-- 启动参数加入 -Dxmemcached.jmx.enable=true 可启动jmx支持 -->
<!-- 监控端可通过service:jmx:rmi:///jndi/rmi://host:7077/xmemcachedServer连接监控 -->
<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
    <!-- servers节点列表格式 主节点1:port,主节点1备份:port 主节点2:port,主节点2备份:port -->
    <!--可以不设置备份节点,主备节点都好分割,不同的主备分组用空格分隔 -->
    <property name="servers">
        <value>${cache.memcacheHost}</value>
    </property>
    <!-- 权重 -->
    <property name="weights">
        <list>
            <value>${cache.memcacheHostWeights1}</value>
        </list>
    </property>
    <!-- 开启sasl验证需要配置此部分 -->
    <!-- <bean name="server1" class="java.net.InetSocketAddress"> <constructor-arg> 
        <value>host1</value> </constructor-arg> <constructor-arg> <value>port1</value> 
        </constructor-arg> </bean> <property name="authInfoMap"> <map> <entry key-ref="server1"> 
        <bean class="net.rubyeye.xmemcached.auth.AuthInfo" factory-method="typical"> 
        <constructor-arg index="0"> <value>cacheuser</value> </constructor-arg> <constructor-arg 
        index="1"> <value>123456</value> </constructor-arg> </bean> </entry> </map> 
        </property> -->
    <!-- nio线程数最好是cpu核数-1 -->
    <property name="connectionPoolSize">
        <value>${cache.memcacheConnectionPoolSize}</value>
    </property>
    <!--默认超时时间 -->
    <property name="opTimeout">
        <value>${cache.opTimeout}</value>
    </property>
    <!-- 使用二进制保存 -->
    <property name="commandFactory">
        <!-- <bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean> -->
        <bean class="net.rubyeye.xmemcached.command.TextCommandFactory"></bean>
    </property>
    <!-- 分布式hash一致策略 -->
    <property name="sessionLocator">
        <bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
    </property>
    <!-- 序列化转换 -->
    <property name="transcoder">
        <bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"></bean>
    </property>
    <!-- buffer分配策略 -->
    <property name="bufferAllocator">
        <bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
    </property>
    <!-- 在hash一致模式下节点失败路由到下一节点,关闭快速失败模式 -->
    <property name="failureMode" value="false"></property>
</bean>
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template