连接二进制协议的 Memcached 实例
使用场景
每个 Memcached 客户端各有特点,用户可根据应用特点选用支持 SASL 和 Memcached Binary Protocol 的任何一款 Memcached 客户端。
网易云提供下列客户端连接二进制协议实例的示例:
Python 客户端连接二进制协议的 Memcached 实例
Note
1.本地服务器连接实例,请先连接 网易云 OpenVPN
2.网易云提供 Python 客户端示例
3.示例 Python 版本为2.7
操作步骤
在线安装
yum -y install epel-release yum -y install python-pip pip install python-binary-memcached
创建测试脚本 test.py,加入以下内容
#!/usr/bin/env python import bmemcached client = bmemcached.Client(('ip:port'), 'user', 'passwd') print client.set('key', 'value1') print client.get('key')
参数说明:
- ip:port:memcached的内网IP和端口(默认 11211)
- user:memcached实例名(没有密码则参数为空)
- passwd:memcached密码(没有密码则参数为空)
执行测试脚本,查看测试结果
python test.py
Attention
若本地环境无法使用 pip 方式安装,可选择源码方式安装。python-binary-memcached下载地址:python-binary-memcached
C++ 客户端连接二进制协议的 Memcached 实例
Note
1.本地服务器连接实例,请先登录 网易云 OpenVPN
2.网易云 C++ 客户端示例,libmemcached下载:libmemcached
3.示例操作系统版本为 Centos7.0
操作步骤
源码安装
yum install gcc-c++ gcc -y yum install cyrus-sasl* -y tar -xvf libmemcached-1.0.18.tar.gz cd libmemcached-1.0.18 ./configure --enable-sasl make make install
编写测试程序 test.cpp,加入以下内容
#include <iostream> #include <string> #include <libmemcached/memcached.h> using namespace std; int main(int argc, char *argv[]) { memcached_st *memc = NULL; memcached_return res; memcached_server_st *server; memc = memcached_create(NULL); server = memcached_server_list_append(NULL, "ip", port, &res); sasl_client_init(NULL); /* AUTH */ res = memcached_set_sasl_auth_data(memc, "user", "passwd"); if (res != MEMCACHED_SUCCESS) { cout<<"Set SASL Error!"<< endl; } /* AUTH */ res = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); if(res != MEMCACHED_SUCCESS) { cout<<"Binary Set Error!"<< endl; } res = memcached_server_push(memc, server); if(res != MEMCACHED_SUCCESS) { cout <<"Connect Memcached Error:"<< res << endl; } memcached_server_list_free(server); string key = "key1"; string value = "value1"; size_t value_length = value.length(); size_t key_length = key.length(); int expiration = 0; uint32_t flags = 0; //Save Data res = memcached_set(memc, key.c_str(), key.length(), value.c_str(), value.length(), expiration, flags); if (res != MEMCACHED_SUCCESS){ cout <<"Save data failed: " << res << endl; return -1; } cout <<"Save data succeed, key: " << key << " value: " << value << endl; cout << "Start get key:" << key << endl; char* result = memcached_get(memc, key.c_str(), key_length, &value_length, &flags, &res); cout << "Get value:" << result << endl; //Delete data cout << "Start delete key:" << key << endl; res = memcached_delete(memc, key.c_str(), key_length, expiration); if (res != MEMCACHED_SUCCESS) { cout << "Delete key failed: " << res << endl; } cout << "Delete key succeed: " << res << endl; //free memcached_free(memc); return 0; }
参数说明:
- ip:port:memcached的内网IP和端口(默认 11211)
- user:memcached实例名
- passwd:memcached密码
若未启动密码,则需注释掉/* AUTH */之间的代码。
配置环境变量
vim /etc/profile 增加:export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH source /etc/profile
Attention
将libmemcached.so文件所在目录加入到变量LD_LIBRARY_PATH中,不同系统路径可能不一样, 请根据自己的安装目录替换。
编译
g++ -g -Wall -std=c++0x test.cpp -lmemcached -lpthread -o memcached -l sasl2
执行测试程序,查看测试结果
./memcached
Java 客户端连接二进制协议的 Memcached 实例
Note
1.本地服务器连接实例,请先登录 网易云 OpenVPN
2.网易云Java 客户端示例,spymemcached-2.10.3.jar下载:spymemcached-2.10.3.jar
操作步骤
示例代码
import net.spy.memcached.AddrUtil; import net.spy.memcached.BinaryConnectionFactory; import net.spy.memcached.ConnectionFactoryBuilder; import net.spy.memcached.ConnectionFactoryBuilder.Protocol; import net.spy.memcached.MemcachedClient; import net.spy.memcached.auth.AuthDescriptor; import net.spy.memcached.auth.PlainCallbackHandler; import java.net.InetSocketAddress; import java.util.concurrent.Future; import java.io.IOException; import java.net.*; public class main{ public static void main(String[] args){ String key = "key1"; String value = "value1"; try{ //连接 Memcached 服务(未启动密码) MemcachedClient mcc = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses("ip:port")); /*连接 Memcached 服务(已启动密码) MemcachedClient mcc = null; AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler("user", "password")); try{ if(mcc == null){ mcc = new MemcachedClient(new ConnectionFactoryBuilder() .setProtocol(Protocol.BINARY) .setAuthDescriptor(ad).build(), AddrUtil.getAddresses("ip:port")); } }catch(IOException ex){ System.err.println("Couldn't create a connection,bailing out:\nIOException" +ex.getMessage()); }*/ //存储数据 Future fo = mcc.set(key, 900, value); //查看存储状态 System.out.println("set status:" + fo.get()); //输出值 System.out.println(key +':'+ mcc.get(key)); //删除数据 fo = mcc.delete(key); //输出执行 delete 方法后的状态 System.out.println("delete status:" + fo.get()); //查看数据是否删除 System.out.println(key +':'+ mcc.get(key)); //关闭连接 mcc.shutdown(); }catch(Exception ex){ System.out.println( ex.getMessage() ); } } }
参数说明:
- ip:port:memcached的内网IP和端口(默认 11211)
- user:memcached实例名
- passwd:memcached密码
若启动密码,则需注释掉未启动密码部分代码,将已启动密码部分代码取消注释。
执行测试代码,查看测试结果
PHP 客户端连接二进制协议的 Memcached 实例
Note
1.本地服务器连接实例,请先登录 网易云 OpenVPN
2.网易云 PHP 客户端的示例,libmemcached下载:libmemcached
3.示例PHP版本为5.4,操作系统版本为 Centos7.0
操作步骤
源码安装
(1) libmemcached 包下载和安装
yum install gcc-c++ gcc -y yum install cyrus-sasl* -y tar -xvf libmemcached-1.0.18.tar.gz cd libmemcached-1.0.18 ./configure --enable-sasl make make install
(2)安装 php memcached 扩展
yum install -y php-devel wget http://pecl.php.net/get/memcached-2.2.0.tgz tar zxvf memcached-2.2.0.tgz cd memcached-2.2.0 phpize #如果系统中有多套php环境需要绝对路径执行 ./configure --with-libmemcached-dir=/usr/local/ --enable-memcached-sasl #根据自己libmemcached安装路径调整, make make install #安装成功会显示memcached.so的位置
配置php.ini
增加: [Memcache] extension_dir = "/usr/lib64/php/modules/" #具体路径为上面make install 后显示的memcached.so所在目录的路径 extension = memcache.so extension = memcached.so memcached.use_sasl = 1
配置环境变量
vim /etc/profile 增加:export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH source /etc/profile
Attention
将libmemcached.so文件所在目录加入到变量LD_LIBRARY_PATH中,不同系统路径可能不一样, 请根据自己的安装目录替换。
php -m | grep memcached
编写测试程序 test.php,加入以下内容
<?php $key = "key1"; $value = "value1"; $memcached = new Memcached; #使用二进制协议 $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); #设置memcached服务 ip:port $memcached->addServer('ip', port); #启动密码时需要认证,未启动密码则注释掉该行代码 $memcached->setSaslAuthData('user', 'passwd'); #存储数据 $memcached->set($key, $value); #获取数据 $get_value = $memcached->get($key); printf("%s\n", $get_value); #删除数据 $memcached->delete($key); #获取数据 $get_value = $memcached->get($key); printf("%s\n", $get_value); ?>
参数说明:
- ip:port:memcached的内网IP和端口(默认 11211)
- user:memcached实例名
- passwd:memcached密码
执行测试程序,查看测试结果
php test.php