Memcache集群安装

 

 

1、安装依赖包

yum  -y  install   gcc  libevent   libevent-devel

 

2、安装memcached

下载memcached , 如memcached-1.5.2.tar.gz 

解压到:  /usr/local/memcached-1.5.2    

cd   /usr/local/memcached-1.5.2    

./configure  --prefix=/usr/local/memcached   &&  make   &&  make install

 

3、启动memcached

在一台机器上开启2个memcached进程, 端口不同 12000 和 12001  , 2台机器就可以开4个进程,形成一个集群,

/usr/local/memcached/bin/memcached -d -u root -l 192.168.8.111 -p 12000 -m 256 -c 100 -P /usr/local/memcached/memcached_12000.pid

/usr/local/memcached/bin/memcached -d -u root -l 192.168.8.111 -p 12001 -m 256 -c 100 -P /usr/local/memcached/memcached_12001.pid

 

启动参数:

-p <num>      设置TCP端口号(默认设置为: 11211)
-U <num>      UDP监听端口(默认: 11211, 0 时关闭) 
-l <ip_addr>    绑定地址(默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问)
-c <num>       max simultaneous connections (default: 1024)
-d             以daemon方式运行
-u <username>  绑定使用指定用于运行进程<username>
-m <num>      允许最大内存用量,单位M (默认: 64 MB)
-P <file>       将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与-d 一起使用



 /usr/local/memcached/bin/memcached -d -u root -l 192.168.8.111 -p 12000 -m 10 -c 100 -P /tmp/memcached_12000.pid
#选项说明,这里只列出比较重要的选项,具体选项说明使用memcached -h来查阅
-p   TCP端口,默认为11211,可以不设置
-U   UDP端口,默认为11211,0为关闭
-l   监听的ip地址
-d   守护进程(daemon)
-u   指定用户,如果当前为 root ,需要使用此参数指定用户
-m   最大内存,单位MB。默认64MB,32位操作系统,每个进程最多只能使用2GB,64位无限制
-M   禁止LRU策略,内存耗尽时返回错误,而不是删除数据
-c   最大连接数,默认是1024
-vv  查看日志
-P   memcache的pid文件,结束memcache进程:kill `cat /tmp/memcached_12000.pid`
-f   增长因子,默认1.25
-n   初始chunk=key+suffix+value+32结构体,默认48字节
-L   启用大内存页,可以降低内存浪费,改进性能
-t   线程数,默认4。由于memcached采用NIO,所以更多线程没有太多作用
-R   每个event连接最大并发数,默认20
-C   禁用CAS命令(可以禁止版本计数,减少开销)
-I   每次申请内存的页的大小(page),默认1M,最小1k,最大128M
-F   禁用flush_all

 

4、连接

telnet   127.0.0.1 12000

 

 

5、基本命令

常用命令 set  /  add  / replace   /  get  /   delete    /    flush_all 

 

set  /  add  / replace

格式为: 

command   <key>   <flags>  <expiration time>  <bytes>   <value>

参数

用法

key

key 用于查找缓存值

flags

可以包括键值对的整型参数,客户机使用它存储关于键值对的额外信息

expiration time

在缓存中保存键值对的时间长度(以秒为单位,0 表示永远)

bytes

在缓存中存储的字节点

value

存储的值(始终位于第二行)

set : 命令用于向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。

set    cid   0   0    5    10011

向缓存中添加了一个键值对,其键为cid   ,其值为10011。并将过期时间设置为 0,这将向 memcached 通知将此值存储在缓存中直到删除它为止

 

replace:  仅当键已经存在时,replace 命令才会替换缓存中的键。如果缓存中不存在键,那么您将从 memcached 服务器接受到一条 NOT_STORED 响应。

replace  cid  0  0  5   10015

 

get :  get 命令用于检索与之前添加的键值对相关的值。您将使用 get 执行大多数检索操作。

get  cid  

 

delete : delete 命令用于删除 memcached 中的任何现有值。您将使用一个键调用delete,如果该键存在于缓存中,则删除该值。如果不存在,则返回一条NOT_FOUND 消息。

delete   cid  

 

stats   :    查看memcached 实例的当前统计数据,显示了关于当前 memcached 实例的信息。

 

flush_all  :   命令仅用于清理缓存中的所有名称/值对。如果您需要将缓存重置到干净的状态,则 flush_all 能提供很大的用处

 

 

6、  简单的Spring 整合  memcahced

<!-- xmemcached -->

<dependency>

<groupId>com.googlecode.xmemcached</groupId>

<artifactId>xmemcached</artifactId>

<version>2.0.0</version>

</dependency>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<bean name="memcachedClient" destroy-method="shutdown"
		class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean">
		<property name="servers">
			<value>192.168.8.111:12000 192.168.8.111:12001</value>
		</property>
		<property name="weights">
			<list>
				<value>1</value>
				<value>2</value>
			</list>
		</property>
		<property name="sessionLocator">
			<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
		</property>
		<property name="transcoder">
			<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
		</property>
		<property name="bufferAllocator">
			<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator" />
		</property>
	</bean>
</beans>

 

 

package cn.utils.memcached;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import net.rubyeye.xmemcached.MemcachedClient;

public class MemcachedTest {

	private static MemcachedClient client = null ;
	private static final Integer defaultExp = 60 * 60 * 24 ;// 一天
	
	public static void main(String[] args) throws Exception {
		
		@SuppressWarnings("resource")
		ApplicationContext app = new ClassPathXmlApplicationContext("classpath:applicationContext-memcached.xml") ;
		if(client == null){
			client = (MemcachedClient)app.getBean("memcachedClient") ;
		}
		
		client.add("name", 5, "abc") ;
		Thread.sleep(5000);
		Object object = client.get("name");
		System.out.println(object);
		
	}
	
	/**
	 * @param key
	 * @param exp  单位S(秒)
	 * @param value
	 * @return
	 */
	public static boolean add(String key , Integer exp , Object value ){
		try {
			if(exp == null){
				exp = defaultExp ;
			}
			return client.add(key, exp, value) ;
		} catch (Exception e) {
			
		}
		return false ;
	}
	
}

 

 

错误:

1、configure: error: newly created file is older than distributed files!

 

原因是linux系统时间慢了很多;

设置linux日期:date  -s  ‘2018-01-28 16:28:00’

时间和日期需要分开设置。

然后在  ./configure就不会报错了

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值