本文介绍使用 C 语言客户端连接腾讯云分布式缓存数据库实例的示例。
准备工作
环境要求
GCC 4.8及以上版本。
CMake 或 Make 构建工具。
安装客户端
推荐使用 hiredis(Redis 官方 C 客户端)和 hiredis-cluster(集群支持):
安装 hiredis
git clone https://github.com/redis/hiredis.gitcd hiredismakesudo make install
Running Environment
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
安装 hiredis-cluster
git clone https://github.com/Nordix/hiredis-cluster.gitcd hiredis-clustermkdir build && cd buildcmake ..makesudo make install
编译链接
# 代理模式gcc -o redis_example redis_example.c -lhiredis
获取连接信息
1. 登录 分布式缓存数据库控制台。
2. 在实例列表中,单击目标实例 ID,进入实例详情页。
3. 在网络信息区域,获取实例 VIP 地址和端口(默认6379)。
4. 在配置信息区域,获取实例的连接密码。
默认账号使用
实例 ID:密码 格式,自定义账号使用 用户名@密码 格式;免密认证实例无需密码。连接示例
代理模式下,客户端通过 Proxy 代理层提供的统一 VIP 地址连接实例,Proxy 自动处理请求路由和故障转移,客户端无需感知后端拓扑变化。
hiredis 基础连接示例
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <hiredis/hiredis.h>int main() {// 连接参数const char *host = "192.xx.xx.2"; // 实例 VIP 地址int port = 6379; // 实例端口const char *password = "crs-xxxx:密码"; // 默认账号密码格式:实例ID:密码struct timeval timeout = {2, 0}; // 连接超时 2 秒// 建立连接redisContext *c = redisConnectWithTimeout(host, port, timeout);if (c == NULL || c->err) {if (c) {printf("Connection error: %s\\n", c->errstr);redisFree(c);} else {printf("Connection error: can't allocate redis context\\n");}return 1;}// 认证redisReply *reply = redisCommand(c, "AUTH %s", password);if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {printf("Auth failed: %s\\n", reply ? reply->str : "unknown error");freeReplyObject(reply);redisFree(c);return 1;}freeReplyObject(reply);// 写入数据reply = redisCommand(c, "SET name %s", "腾讯云分布式缓存数据库");printf("SET result: %s\\n", reply->str);freeReplyObject(reply);// 读取数据reply = redisCommand(c, "GET name");printf("GET name: %s\\n", reply->str);freeReplyObject(reply);// 设置带过期时间的 Keyreply = redisCommand(c, "SETEX session:user123 3600 %s", "session_data");printf("SETEX result: %s\\n", reply->str);freeReplyObject(reply);// 哈希操作reply = redisCommand(c, "HSET user:1001 name %s age %s", "张三", "28");printf("HSET result: %lld\\n", reply->integer);freeReplyObject(reply);reply = redisCommand(c, "HGET user:1001 name");printf("HGET name: %s\\n", reply->str);freeReplyObject(reply);// 列表操作reply = redisCommand(c, "LPUSH queue:tasks %s %s %s", "task1", "task2", "task3");printf("LPUSH result: %lld\\n", reply->integer);freeReplyObject(reply);reply = redisCommand(c, "RPOP queue:tasks");printf("RPOP: %s\\n", reply->str);freeReplyObject(reply);// 关闭连接redisFree(c);return 0;}
SSL 加密连接示例
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <hiredis/hiredis.h>#include <hiredis/hiredis_ssl.h>int main() {const char *host = "192.xx.xx.2";int port = 6379;const char *password = "crs-xxxx:密码";const char *ca_cert = "/path/to/ca.pem";struct timeval timeout = {2, 0};// 初始化 SSLredisInitOpenSSL();// 创建 SSL 上下文redisSSLContext *ssl_context = redisCreateSSLContext(ca_cert, // CA 证书NULL, // 客户端证书(不需要)NULL, // 客户端私钥(不需要)NULL, // 证书密码NULL, // 错误信息NULL // 错误信息长度);if (ssl_context == NULL) {printf("Failed to create SSL context\\n");return 1;}// 建立 SSL 连接redisContext *c = redisConnectWithTimeout(host, port, timeout);if (c == NULL || c->err) {printf("Connection error: %s\\n", c ? c->errstr : "unknown");redisFreeSSLContext(ssl_context);if (c) redisFree(c);return 1;}// 初始化 SSL 连接if (redisInitiateSSLWithContext(c, ssl_context) != REDIS_OK) {printf("SSL init error: %s\\n", c->errstr);redisFreeSSLContext(ssl_context);redisFree(c);return 1;}// 认证redisReply *reply = redisCommand(c, "AUTH %s", password);if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {printf("Auth failed: %s\\n", reply ? reply->str : "unknown");if (reply) freeReplyObject(reply);redisFreeSSLContext(ssl_context);redisFree(c);return 1;}freeReplyObject(reply);// 操作数据reply = redisCommand(c, "SET ssl_test %s", "encrypted_connection");printf("SET: %s\\n", reply->str);freeReplyObject(reply);reply = redisCommand(c, "GET ssl_test");printf("GET: %s\\n", reply->str);freeReplyObject(reply);// 清理资源redisFree(c);redisFreeSSLContext(ssl_context);return 0;}
连接参数说明
参数 | 说明 | 推荐值 |
host | 实例连接地址。 | - |
port | 实例端口号 | 6379 |
timeout | 连接超时时间 | 2秒 |
password | 认证密码 | 默认账号格式: 实例 ID:密码 |
max_redirect | 最大重定向次数 | 3 |
常见问题
连接超时
确认客户端所在 CVM 与分布式缓存数据库实例在同一 VPC 内。
确认安全组规则已放通对应端口。
确认连接地址和端口正确。