网站统计分析平台,成都现在能去吗,wordpress评论已关闭,网站开发服务内容前言
Amazon ElastiCache for Redis 是速度超快的内存数据存储#xff0c;能够提供亚毫秒级延迟来支持 实时应用程序。适用于 Redis 的 ElastiCache 基于开源 Redis 构建#xff0c;可与 Redis API 兼容#xff0c;能够与 Redis 客户端配合工作#xff0c;并使用开放的 Re…前言
Amazon ElastiCache for Redis 是速度超快的内存数据存储能够提供亚毫秒级延迟来支持 实时应用程序。适用于 Redis 的 ElastiCache 基于开源 Redis 构建可与 Redis API 兼容能够与 Redis 客户端配合工作并使用开放的 Redis 数据格式来存储数据。适用于 Redis 的 ElastiCache 兼具开源 Redis 的速度、简单性和多功能性与 Amazon 的可管理性、安全性和可扩展性能够在游戏、广告技术、电子商务、医疗保健、金融服务和物联网领域支持要求最严苛的实时应用程序。
亚马逊云科技开发者社区为开发者们提供全球的开发技术资源。这里有技术文档、开发案例、技术专栏、培训视频、活动与竞赛等。帮助中国开发者对接世界最前沿技术观点和项目并将中国优秀开发者或技术推荐给全球云社区。如果你还没有关注/收藏看到这里请一定不要匆匆划过点这里让它成为你的技术宝库 利用 ElastiCache for Redis客户可以使用多个分区创建和运行托管的 Redis 集群。当遇到以下三种主要场景时必须扩展 Redis 环境。第一如果 Redis 数据的总内存大小超出或预计超出单个虚拟机的内存容量。第二如果应用程序将数据写入 Redis 的写入吞吐量超出单个虚拟机的容量。第三如果要将数据分布到多个分区中以便在单个节点遇到任何问题时对总体 Redis 环境产生的影响都较小。
我们推出了一系列博客展示了如何在不同语言中使用不同的支持 ElastiCache 集群模式的客户端对 ElastiCache 集群进行连接和操作前面已经有一篇博客介绍了 “使用 redission 连接 Amazon ElastiCache for redis 集群” 今天的主题是讨论如何使用 C 语言连接 Elasticache for redis 集群。通过 redis 官网上查询到现有的客户端C 语言中支持 redis cluster 的主要有 Hiredis-cluster hiredis-vip 等C 语言中支持 redis cluster 的主要有 redis-plus-plusxredis 等。本篇博客主要会介绍 Hiredis-cluster 的使用。
2. 搭建测试环境
在此说明下此次测试在宁夏区域进行测试的主要内容包括
在开启 TLS 及 Auth 的 ElastiCache 集群中验证读写和 Failover在未开启 TLS 及 Auth 的 ElastiCache 集群中验证读写和 Failover因为对于未开启 TLS 及 Auth 的集群Client 端的代码实现会有所不同。
2.1. 创建测试环境
创建ElastiCache集群
首先创建一个开启 TLS 及其 Auth 的3 shards6节点的ElastiCache 集群实例类型为 cache.t3.small版本为5.0.6 按下文的方式获取 configuration endpoint此处假设其值为:
具体获取 endpoint 的方式如下 查找 Redis已启用集群模式集群的 configuration endpoint
登录 Amazon Management Console 并打开 ElastiCache 控制台 (https://console.aws.amazon.com/elasticache/)。从导航窗格中选择 Redis。此时会显示一个列表其中包含运行任意版本 Redis 的集群。从集群列表中选择运行“集群 Redis”的集群左侧的复选框。此时屏幕会展开以显示有关选定集群的详细信息。找到 Configuration endpoint 2.创建一个关闭 TLS 且无 auth 的3 shards6节点的 ElastiCache 集群实例类型为 cache.t3.medium版本为5.0.6按上文的方式获取 configuration endpoint此处假设其值为: 创建EC2测试客户端
1.在同一个 VPC 内创建一个 EC2并配置好响应 security group我使用的 EC2是 Ubuntu 18.04安装的环境是一些 Ubuntu 的参考命令。
2.在 EC2上准备环境 Hiredis-cluster 是基于 hiredis 扩展编写的 我们环境准备准备的时候也会涉及到 hiredis 部分
C Compiler 安装这里我使用 GCC可参考这个 How to install GCC Compiler on Ubuntu 18.04libssl-dev 安装sudo apt-get install libssl-dev 注意在CMake 之前安装否则会报错 “Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)”CMake 安装我使用的是 CMake 编译CMake 官网提供了安装方式Installing | CMakelibevent-dev 安装sudo apt install libevent-dev 直接安装可能会报如下错误解决办法是
CMake Error at /usr/local/share/cmake-3.23/Modules/FindPackageHandleStandardArgs.cmake:230 (message):Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
Call Stack (most recent call first):/usr/local/share/cmake-3.23/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)/usr/local/share/cmake-3.23/Modules/FindPkgConfig.cmake:99 (find_package_handle_standard_args)tests/CMakeLists.txt:40 (find_package)hiredis 下载并编译因为 hiredis-cluster 是基于 hiredis 扩展编写的所以需要安装并编译 hiredis。 如下命令因为会用到 SSL所以我使用 make USE_SSL1, 可以直接使用 make 替代。
git clone https://github.com/redis/hiredis.git
cd hiredis
make USE_SSL1
sudo make install
hiredis-cluster 环境准备因为会测试 ssl所以通过选项指定Redis
mkdir build; cd build
cmake -DCMAKE_BUILD_TYPERelWithDebInfo -DENABLE_SSLON ..
make
3.在上面的步骤完成后你的环境会是这样的
hiredis 相关中会生成 hiredis_ssl lib 文件在/usr/local/lib/中 hiredis.a 文件 中hiredis_cluster lib 文件生成 2.2 不开启传输中加密TLS及 auth 的 ElastiCache 集群测试
加载测试数据
使用 redis-cli 登录未开启了 TLS 和 auth 的 ElastiCache 集群并通过 cluster nodes 命令获取 slots 在 shards 中的分布通过 redis-cli 工具连接集群并进行操作的具体方法请见使用 redis-cli 连接到 Redis 集群。
redis-cli -c -h -p 分别生成 testkatestbtestc 三个 key可以通过其 slot 值结合上文各个 shard 上 slot 分布信息判断出3个 key 恰好分布在 shard1shard2 和 shard3 上 C代码
1.以下是写入 ElastiCache 集群的代码, 针对于 Elasticache 三个 shard 上的 test1test2test3 三个 key 进行写入请注意配置的地址为 ElastiCache 集群的 configuration endpoint另外修改不同的 keyname。
#include hircluster.h
#include stdio.h
#include stdlib.h
#include unistd.hint main(int argc, char **argv) {UNUSED(argc);UNUSED(argv);struct timeval timeout {1, 500000}; // 1.5sredisClusterContext *cc redisClusterContextInit();redisClusterSetOptionAddNodes(cc, No TLS and auth ElastiCache configuration endpoint:port); redisClusterSetOptionConnectTimeout(cc, timeout);redisClusterSetOptionRouteUseSlots(cc);redisClusterConnect2(cc);if (cc cc-err) {printf(Error: %s\n, cc-errstr);// handle errorexit(-1);}int count 0;while(count 10000){redisReply *reply (redisReply *)redisClusterCommand(cc, SET %s %d, test1, count);printf(SET: %d\n, count);count ;if (cc cc-err) {printf(Error: %s\n, cc-errstr);}freeReplyObject(reply);sleep(1);}redisClusterFree(cc);return 0;
}
2.以下是读取 ElastiCache 集群的代码, 针对于 Elasticache 三个 shard 上的 test1test2test3 三个 key 进行读取请注意配置的地址为 ElastiCache 集群的 configuration endpoint另外修改不同的 keyname。
#include hircluster.h
#include stdio.h
#include stdlib.h
#include unistd.hint main(int argc, char **argv) {UNUSED(argc);UNUSED(argv);struct timeval timeout {1, 500000}; // 1.5sredisClusterContext *cc redisClusterContextInit();redisClusterSetOptionAddNodes(cc, No TLS and auth ElastiCache configuration endpoint:port); redisClusterSetOptionConnectTimeout(cc, timeout);redisClusterSetOptionRouteUseSlots(cc);redisClusterConnect2(cc);if (cc cc-err) {printf(Error: %s\n, cc-errstr);// handle errorexit(-1);}int count 0;while(count 10000){redisReply *reply (redisReply *)redisClusterCommand(cc, GET %s, test1);if(reply reply-str) {printf(GET: %s\n, reply-str);}freeReplyObject(reply);if (cc cc-err) {printf(Error: %s\n, cc-errstr);}count;sleep(1);}redisClusterFree(cc);return 0;
}
基本功能测试
1.使用如下命令编译代码
gcc hiredisc-naw1.c -o hiredisc-naw1 -L./build -lhiredis_cluster /usr/local/lib/libhiredis.a -L~/hiredis -lhiredis_ssl
2.运行程序并监控 ElastiCache 未开启 TLS 及 Auth 集群的 CurrConnections 指标可以看到负载被均衡地发送到了6个节点上即 hiredis-cluster 可以完成读写分离和负载均衡的工作
我的代码在~/hiredis-cluster 路径下面所以
cd hiredis-cluster/ ./hiredisc-naw1
cd hiredis-cluster/ ./hiredisc-naw2
cd hiredis-cluster/ ./hiredisc-naw3
cd hiredis-cluster/ ./hiredisc-nar1
cd hiredis-cluster/ ./hiredisc-nar2
cd hiredis-cluster/ ./hiredisc-nar3 2.3. 开启传输中加密TLS及 auth 的 ElastiCache 集群测试
Amazon ElastiCache 传输中加密是一项可选功能它允许您在数据最脆弱时候从一个位置传输到另一个位置时提高数据的安全性。由于在终端节点加密和解密数据时需要进行一些处理因此启用传输中加密会对性能产生一些影响。应对使用和不使用传输中加密的数据进行基准测试以确定对使用案例的性能影响。 关于 TLS 加密这一块具体可以参考条条大路通罗马- 使用 redission 连接 Amazon ElastiCache for redis 集群。
加载测试数据
1.使用 redis-cli 登录开启了 TLS 和 auth 的 ElastiCache 集群并通过 cluster nodes 命令获取 slots 在 shards 中的分布通过 redis-cli 工具连接集群并进行操作的具体方法请见使用 redis-cli 连接到 Redis 集群。 redis-cli -c -h -p
2.分别生成 testkatestbtestc 三个 key可以通过其 slot 值结合上文各个 shard 上 slot 分布信息判断出 3 个 key 恰好分布在 shard1shard2 和 shard3 上 C代码
1.以下是写入 ElastiCache 集群的代码, 针对于 Elasticache 三个shard 上的 test1test2test3 三个 key 进行写入请注意配置的地址为 ElastiCache 集群的 configuration endpoint设置密码另外修改不同的 keyname。这里需要注意的是 hiredis-cluster 对 SSL 的支持可以分为两种方式一种是指定 SSL 证书路径另一种是指定证书对于 EC2 访问 ElastiCache 的情况指定证书路径即可, 具体可参考 hiredis 的文档。
#include hiredis_cluster/hircluster.h#include hiredis/hiredis_ssl.h
#include stdio.h
#include stdlib.h#define CLUSTER_NODE_TLS :Enable TLS and auth ElastiCache configuration endpoint:port
#define CLUSTER_PASSWORD PASSWORDint main(int argc, char **argv) {UNUSED(argc);UNUSED(argv);redisSSLContext *ssl;redisSSLContextError ssl_error;redisInitOpenSSL();ssl redisCreateSSLContext(NULL, /etc/ssl/certs, NULL, NULL,NULL, ssl_error);if (!ssl) {printf(SSL Context error: %s\n, redisSSLContextGetError(ssl_error));exit(1);}struct timeval timeout {5, 500000}; // 5.5sredisClusterContext *cc redisClusterContextInit();redisClusterSetOptionAddNodes(cc, CLUSTER_NODE_TLS);redisClusterSetOptionConnectTimeout(cc, timeout);redisClusterSetOptionRouteUseSlots(cc);redisClusterSetOptionEnableSSL(cc, ssl);redisClusterSetOptionPassword(cc, CLUSTER_PASSWORD);redisClusterConnect2(cc);if (cc cc-err) {printf(Error: %s\n, cc-errstr);// handle errorexit(-1);}int count 0;while(count 10000){redisReply *reply (redisReply *)redisClusterCommand(cc, SET %s %d, test1, count);printf(SET: %d\n, count);if (cc cc-err) {printf(Error: %s\n, cc-errstr);}count ;freeReplyObject(reply);sleep(1);}redisClusterFree(cc);redisFreeSSLContext(ssl);return 0;
}
2.以下是读取 ElastiCache 集群的代码, 针对于 Elasticache 三个shard 上的 test1test2test3 三个 key 进行读取请注意配置的地址为 ElastiCache 集群的 configuration endpoint 另外修改不同的 keyname。
#include hircluster.h
#include stdio.h
#include stdlib.h
#include unistd.hint main(int argc, char **argv) {UNUSED(argc);UNUSED(argv);struct timeval timeout {1, 500000}; // 1.5sredisClusterContext *cc redisClusterContextInit();redisClusterSetOptionAddNodes(cc, No TLS and auth ElastiCache configuration endpoint:port); redisClusterSetOptionConnectTimeout(cc, timeout);redisClusterSetOptionRouteUseSlots(cc);redisClusterConnect2(cc);if (cc cc-err) {printf(Error: %s\n, cc-errstr);// handle errorexit(-1);}int count 0;while(count 10000){redisReply *reply (redisReply *)redisClusterCommand(cc, GET %s, test1);if(reply reply-str) {printf(GET: %s\n, reply-str);}freeReplyObject(reply);if (cc cc-err) {printf(Error: %s\n, cc-errstr);}count;sleep(1);}redisClusterFree(cc);return 0;
}
基本功能测试
1.使用如下命令编译代码
gcc hiredisc-naw1.c -o hiredisc-naw1 -L./build -lhiredis_cluster /usr/local/lib/libhiredis.a -L~/hiredis -lhiredis_ssl
2.运行程序并监控 ElastiCache 未开启 TLS 及 Auth 集群的CurrConnections 指标可以看到负载被均衡地发送到了6个节点上即 hiredis-cluster 可以完成读写分离和负载均衡的工作
我的代码在~/hiredis-cluster 路径下面所以
cd hiredis-cluster/ ./hiredisc-naw1
cd hiredis-cluster/ ./hiredisc-naw2
cd hiredis-cluster/ ./hiredisc-naw3
cd hiredis-cluster/ ./hiredisc-nar1
cd hiredis-cluster/ ./hiredisc-nar2
cd hiredis-cluster/ ./hiredisc-nar3 2.3. 开启传输中加密TLS及 auth 的 ElastiCache 集群测试
Amazon ElastiCache 传输中加密是一项可选功能它允许您在数据最脆弱时候从一个位置传输到另一个位置时提高数据的安全性。由于在终端节点加密和解密数据时需要进行一些处理因此启用传输中加密会对性能产生一些影响。应对使用和不使用传输中加密的数据进行基准测试以确定对使用案例的性能影响。 关于 TLS 加密这一块具体可以参考条条大路通罗马- 使用 redission 连接 Amazon ElastiCache for redis 集群。
加载测试数据
1.使用 redis-cli 登录开启了 TLS 和 auth 的 ElastiCache 集群并通过 cluster nodes 命令获取 slots 在 shards 中的分布通过 redis-cli 工具连接集群并进行操作的具体方法请见使用 redis-cli 连接到 Redis 集群。 redis-cli -c -h -p
2.分别生成 testkatestbtestc 三个 key可以通过其 slot 值结合上文各个 shard 上 slot 分布信息判断出 3 个 key 恰好分布在shard1shard2 和 shard3 上 C代码
1.以下是写入 ElastiCache 集群的代码, 针对于 Elasticache 三个shard 上的 test1test2test3 三个 key 进行写入请注意配置的地址为 ElastiCache 集群的 configuration endpoint设置密码另外修改不同的 keyname。这里需要注意的是 hiredis-cluster 对SSL的支持可以分为两种方式一种是指定 SSL 证书路径另一种是指定证书对于 EC2 访问 ElastiCache 的情况指定证书路径即可, 具体可参考 hiredis 的文档。
#include hiredis_cluster/hircluster.h#include hiredis/hiredis_ssl.h
#include stdio.h
#include stdlib.h#define CLUSTER_NODE_TLS :Enable TLS and auth ElastiCache configuration endpoint:port
#define CLUSTER_PASSWORD PASSWORDint main(int argc, char **argv) {UNUSED(argc);UNUSED(argv);redisSSLContext *ssl;redisSSLContextError ssl_error;redisInitOpenSSL();ssl redisCreateSSLContext(NULL, /etc/ssl/certs, NULL, NULL,NULL, ssl_error);if (!ssl) {printf(SSL Context error: %s\n, redisSSLContextGetError(ssl_error));exit(1);}struct timeval timeout {5, 500000}; // 5.5sredisClusterContext *cc redisClusterContextInit();redisClusterSetOptionAddNodes(cc, CLUSTER_NODE_TLS);redisClusterSetOptionConnectTimeout(cc, timeout);redisClusterSetOptionRouteUseSlots(cc);redisClusterSetOptionEnableSSL(cc, ssl);redisClusterSetOptionPassword(cc, CLUSTER_PASSWORD);redisClusterConnect2(cc);if (cc cc-err) {printf(Error: %s\n, cc-errstr);// handle errorexit(-1);}int count 0;while(count 10000){redisReply *reply (redisReply *)redisClusterCommand(cc, SET %s %d, test1, count);printf(SET: %d\n, count);if (cc cc-err) {printf(Error: %s\n, cc-errstr);}count ;freeReplyObject(reply);sleep(1);}redisClusterFree(cc);redisFreeSSLContext(ssl);return 0;
}
2.以下是读取 ElastiCache 集群的代码, 针对于 Elasticache 三个shard 上的 test1test2test3 三个 key 进行读取请注意配置的地址为 ElastiCache 集群的 configuration endpoint 另外修改不同的 keyname 。
#include hiredis_cluster/hircluster.h#include hiredis/hiredis_ssl.h
#include stdio.h
#include stdlib.h#define CLUSTER_NODE_TLS Enable TLS and auth ElastiCache configuration endpoint:port
#define CLUSTER_PASSWORD PASSWORDint main(int argc, char **argv) {UNUSED(argc);UNUSED(argv);redisSSLContext *ssl;redisSSLContextError ssl_error;redisInitOpenSSL();ssl redisCreateSSLContext(NULL, /etc/ssl/certs, NULL, NULL,NULL, ssl_error);if (!ssl) {printf(SSL Context error: %s\n, redisSSLContextGetError(ssl_error));exit(1);}struct timeval timeout {5, 500000}; // 5.5sredisClusterContext *cc redisClusterContextInit();redisClusterSetOptionAddNodes(cc, CLUSTER_NODE_TLS);redisClusterSetOptionConnectTimeout(cc, timeout);redisClusterSetOptionRouteUseSlots(cc);redisClusterSetOptionEnableSSL(cc, ssl);redisClusterSetOptionPassword(cc, CLUSTER_PASSWORD);redisClusterConnect2(cc);if (cc cc-err) {printf(Error: %s\n, cc-errstr);// handle errorexit(-1);}int count 0;while(count 10000){redisReply *reply (redisReply *)redisClusterCommand(cc, GET %s, test1);if(reply reply-str) {printf(GET: %s\n, reply-str);}freeReplyObject(reply);
if (cc cc-err) {printf(Error: %s\n, cc-errstr);}count;sleep(1);}redisClusterFree(cc);redisFreeSSLContext(ssl);return 0;
}
基本功能测试
1.使用如下命令编译代码
gcc hiredisc-aw1.c -o hiredisc-aw1 -L./build -lhiredis_cluster /usr/local/lib/libhiredis.a -L~/hiredis -lhiredis_ssl
2.运行程序并监控 ElastiCache 开启 TLS 及 Auth 集群的CurrConnections 指标可以看到负载被均衡地发送到了 6 个节点上即 hiredis-cluster 可以完成读写分离和负载均衡的工作
我的代码在~/hiredis-cluster 路径下面所以
cd hiredis-cluster/ ./hiredisc-aw1
cd hiredis-cluster/ ./hiredisc-aw2
cd hiredis-cluster/ ./hiredisc-aw3
cd hiredis-cluster/ ./hiredisc-ar1
cd hiredis-cluster/ ./hiredisc-ar2
cd hiredis-cluster/ ./hiredisc-ar3 三级标题
3.1. 未开启 TLS 及 auth 的 ElastiCache failover 测试
登录 console对 Shard1 进行 failover然后观测我们的C程序的输出, 此过程大约持续12s 观测所得 从 ElastiCache 集群的每个节点的 CurrConnections 指标截图同样可以看出hiredis-cluster 将读写请求都发送到了 shard1 新的主节点上: 3.2. 开启 TLS 及 auth 的 ElastiCache failover 测试
登录 console对 Shard1 进行 failover然后观测我们的C程序的输出这个时间大概 10s 左右观测所得 从 ElastiCache 集群的每个节点的 CurrConnections 指标截图同样可以看出 hiredis-cluster 将读写请求都发送到了 shard1 新的主节点上: failover 测试中发生问题以及解决方案
在做 failover 的时候发现一个问题在 failover 过程中有的时候client 端直接断开链接有的时候成功后来经过分析 hiredis 的代码得知需要把 timeout 时间调整下所以从 1.5s 调整为 5.5s, 注意这个值并不是最佳实践是根据测试得来的。
4. 结束语
在本博客中主要使用C语言通过 hiredis-cluster 连接和操作ElastiCache 集群从简单的 Demo 中我们可以看到 hiredis-cluster 可以很好地支持 ElastiCache 集群开启 TLS 及 auth 等功能并自动完成读写分离负载均衡failover 等工作有助于我们便捷高效地使用 ElastiCache。
5.参考
Hiredis-cluster 文档GitHub - Nordix/hiredis-cluster: C client library for Redis Cluster. This project is used and sponsored by Ericsson. It is a fork of the now unmaintained hiredis-vip.
Hiredis 文档: GitHub - redis/hiredis: Minimalistic C client for Redis 1.2
Amazon Elasticache Amazon ElastiCache for Redis
条条大路通罗马 —— 使用 redisson 连接 Amazon ElastiCache for redis 集群条条大路通罗马 —— 使用redisson连接Amazon ElastiCache for redis 集群 | 亚马逊AWS官方博客
相关博客
条条大路通罗马 —— 使用 redisson 连接 Amazon ElastiCache for redis 集群
条条大路通罗马 —— 使用 redis-py 访问 Amazon ElastiCache for redis集群
条条大路通罗马 —— 使用 go-redis 连接 Amazon ElastiCache for Redis 集群
本篇作者 冯秋爽
Amazon 解决方案架构师负责跨国企业级客户基于 Amazon 的技术架构设计、咨询和设计优化工作。在加入 Amazon 之前曾就职于 IBM 、甲骨文等 IT 企业积累了丰富的程序开发和数据库的实践经验。
文章来源https://dev.amazoncloud.cn/column/article/630a21a42ecbae73705ffb40?sc_mediumregulartrafficamp;sc_campaigncrossplatformamp;sc_channelCSDN