国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群

這篇具有很好參考價(jià)值的文章主要介紹了條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言

Amazon ElastiCache for Redis 是速度超快的內(nèi)存數(shù)據(jù)存儲(chǔ),能夠提供亞毫秒級(jí)延遲來(lái)支持 實(shí)時(shí)應(yīng)用程序。適用于 Redis 的 ElastiCache 基于開(kāi)源 Redis 構(gòu)建,可與 Redis API 兼容,能夠與 Redis 客戶(hù)端配合工作,并使用開(kāi)放的 Redis 數(shù)據(jù)格式來(lái)存儲(chǔ)數(shù)據(jù)。適用于 Redis 的 ElastiCache 兼具開(kāi)源 Redis 的速度、簡(jiǎn)單性和多功能性與 Amazon 的可管理性、安全性和可擴(kuò)展性,能夠在游戲、廣告技術(shù)、電子商務(wù)、醫(yī)療保健、金融服務(wù)和物聯(lián)網(wǎng)領(lǐng)域支持要求最嚴(yán)苛的實(shí)時(shí)應(yīng)用程序。

亞馬遜云科技開(kāi)發(fā)者社區(qū)為開(kāi)發(fā)者們提供全球的開(kāi)發(fā)技術(shù)資源。這里有技術(shù)文檔、開(kāi)發(fā)案例、技術(shù)專(zhuān)欄、培訓(xùn)視頻、活動(dòng)與競(jìng)賽等。幫助中國(guó)開(kāi)發(fā)者對(duì)接世界最前沿技術(shù),觀點(diǎn),和項(xiàng)目,并將中國(guó)優(yōu)秀開(kāi)發(fā)者或技術(shù)推薦給全球云社區(qū)。如果你還沒(méi)有關(guān)注/收藏,看到這里請(qǐng)一定不要匆匆劃過(guò),點(diǎn)這里讓它成為你的技術(shù)寶庫(kù)!

?

利用 ElastiCache for Redis,客戶(hù)可以使用多個(gè)分區(qū)創(chuàng)建和運(yùn)行托管的 Redis 集群。當(dāng)遇到以下三種主要場(chǎng)景時(shí),必須擴(kuò)展 Redis 環(huán)境。第一,如果 Redis 數(shù)據(jù)的總內(nèi)存大小超出或預(yù)計(jì)超出單個(gè)虛擬機(jī)的內(nèi)存容量。第二,如果應(yīng)用程序?qū)?shù)據(jù)寫(xiě)入 Redis 的寫(xiě)入吞吐量超出單個(gè)虛擬機(jī)的容量。第三,如果要將數(shù)據(jù)分布到多個(gè)分區(qū)中,以便在單個(gè)節(jié)點(diǎn)遇到任何問(wèn)題時(shí),對(duì)總體 Redis 環(huán)境產(chǎn)生的影響都較小。

我們推出了一系列博客,展示了如何在不同語(yǔ)言中使用不同的支持 ElastiCache 集群模式的客戶(hù)端對(duì) ElastiCache 集群進(jìn)行連接和操作,前面已經(jīng)有一篇博客介紹了 “使用 redission 連接 Amazon ElastiCache for redis 集群”, 今天的主題是討論如何使用 C 語(yǔ)言連接 Elasticache for redis 集群。通過(guò) redis 官網(wǎng)上查詢(xún)到現(xiàn)有的客戶(hù)端,C 語(yǔ)言中支持 redis cluster 的主要有 Hiredis-cluster, hiredis-vip 等,C++?語(yǔ)言中支持 redis cluster 的主要有 redis-plus-plus,xredis 等。本篇博客主要會(huì)介紹 Hiredis-cluster 的使用。

2. 搭建測(cè)試環(huán)境

在此說(shuō)明下,此次測(cè)試在寧夏區(qū)域進(jìn)行,測(cè)試的主要內(nèi)容包括:

  • 在開(kāi)啟 TLS 及 Auth 的 ElastiCache 集群中驗(yàn)證讀寫(xiě)和 Failover
  • 在未開(kāi)啟 TLS 及 Auth 的 ElastiCache 集群中驗(yàn)證讀寫(xiě)和 Failover,因?yàn)閷?duì)于未開(kāi)啟 TLS 及 Auth 的集群,Client 端的代碼實(shí)現(xiàn)會(huì)有所不同。
2.1. 創(chuàng)建測(cè)試環(huán)境

創(chuàng)建ElastiCache集群

  1. 首先創(chuàng)建一個(gè)開(kāi)啟 TLS 及其 Auth 的3 shards,6節(jié)點(diǎn)的ElastiCache 集群,實(shí)例類(lèi)型為 cache.t3.small,版本為5.0.6, 按下文的方式獲取 configuration endpoint,此處假設(shè)其值為:

具體獲取 endpoint 的方式如下:

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

查找 Redis(已啟用集群模式)集群的 configuration endpoint

  • 登錄 Amazon Management Console 并打開(kāi) ElastiCache 控制臺(tái) (https://console.aws.amazon.com/elasticache/)。
  • 從導(dǎo)航窗格中,選擇 Redis。
  • 此時(shí)會(huì)顯示一個(gè)列表,其中包含運(yùn)行任意版本 Redis 的集群。
  • 從集群列表中,選擇運(yùn)行“集群 Redis”的集群左側(cè)的復(fù)選框。
  • 此時(shí)屏幕會(huì)展開(kāi),以顯示有關(guān)選定集群的詳細(xì)信息。
  • 找到 Configuration endpoint

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

2.創(chuàng)建一個(gè)關(guān)閉 TLS 且無(wú) auth 的3 shards,6節(jié)點(diǎn)的 ElastiCache 集群,實(shí)例類(lèi)型為 cache.t3.medium,版本為5.0.6,按上文的方式獲取 configuration endpoint,此處假設(shè)其值為:

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

創(chuàng)建EC2測(cè)試客戶(hù)端

1.在同一個(gè) VPC 內(nèi)創(chuàng)建一個(gè) EC2并配置好響應(yīng) security group,我使用的 EC2是 Ubuntu 18.04,安裝的環(huán)境是一些 Ubuntu 的參考命令。

2.在 EC2上準(zhǔn)備環(huán)境, Hiredis-cluster 是基于 hiredis 擴(kuò)展編寫(xiě)的, 我們環(huán)境準(zhǔn)備準(zhǔn)備的時(shí)候也會(huì)涉及到 hiredis 部分,

  • C Compiler 安裝,這里我使用 GCC,可參考這個(gè) How to install GCC Compiler on Ubuntu 18.04
  • libssl-dev 安裝,sudo apt-get install libssl-dev 注意在CMake 之前安裝,否則會(huì)報(bào)錯(cuò) “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 官網(wǎng)提供了安裝方式Installing | CMake
  • libevent-dev 安裝,sudo apt install libevent-dev, 直接安裝可能會(huì)報(bào)如下錯(cuò)誤,解決辦法是
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 下載并編譯,因?yàn)?hiredis-cluster 是基于 hiredis 擴(kuò)展編寫(xiě)的,所以需要安裝并編譯 hiredis。 如下命令,因?yàn)闀?huì)用到 SSL,所以我使用 make USE_SSL=1, 可以直接使用 make 替代。
git clone https://github.com/redis/hiredis.git
cd hiredis
make USE_SSL=1
sudo make install
  • hiredis-cluster 環(huán)境準(zhǔn)備,因?yàn)闀?huì)測(cè)試 ssl,所以通過(guò)選項(xiàng)指定,Redis
mkdir build; cd build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_SSL=ON ..
make

3.在上面的步驟完成后,你的環(huán)境會(huì)是這樣的:

hiredis 相關(guān),中會(huì)生成 hiredis_ssl lib 文件,在</usr/local/lib/>中 hiredis.a 文件

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

中,hiredis_cluster lib 文件生成

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

2.2 不開(kāi)啟傳輸中加密(TLS)及 auth 的 ElastiCache 集群測(cè)試

加載測(cè)試數(shù)據(jù)

  1. 使用 redis-cli 登錄未開(kāi)啟了 TLS 和 auth 的 ElastiCache 集群并通過(guò) cluster nodes 命令獲取 slots 在 shards 中的分布,通過(guò) redis-cli 工具連接集群并進(jìn)行操作的具體方法請(qǐng)見(jiàn)使用 redis-cli 連接到 Redis 集群。

redis-cli -c -h -p

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

  1. 分別生成 testka,testb,testc 三個(gè) key,可以通過(guò)其 slot 值結(jié)合上文各個(gè) shard 上 slot 分布信息,判斷出3個(gè) key 恰好分布在 shard1,shard2 和 shard3 上

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

C代碼

1.以下是寫(xiě)入 ElastiCache 集群的代碼, 針對(duì)于 Elasticache 三個(gè) shard 上的 test1,test2,test3 三個(gè) key 進(jìn)行寫(xiě)入,請(qǐng)注意配置的地址為 ElastiCache 集群的 configuration endpoint,另外修改不同的 keyname。

#include "hircluster.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
    UNUSED(argc);
    UNUSED(argv);
    struct timeval timeout = {1, 500000}; // 1.5s

    redisClusterContext *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 error
        exit(-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 集群的代碼, 針對(duì)于 Elasticache 三個(gè) shard 上的 test1,test2,test3 三個(gè) key 進(jìn)行讀取,請(qǐng)注意配置的地址為 ElastiCache 集群的 configuration endpoint,另外修改不同的 keyname。

#include "hircluster.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
    UNUSED(argc);
    UNUSED(argv);
    struct timeval timeout = {1, 500000}; // 1.5s

    redisClusterContext *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 error
        exit(-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;
}

基本功能測(cè)試

1.使用如下命令編譯代碼:

gcc hiredisc-naw1.c -o hiredisc-naw1 -L./build -lhiredis_cluster /usr/local/lib/libhiredis.a -L~/hiredis -lhiredis_ssl

2.運(yùn)行程序并監(jiān)控 ElastiCache 未開(kāi)啟 TLS 及 Auth 集群的 CurrConnections 指標(biāo),可以看到負(fù)載被均衡地發(fā)送到了6個(gè)節(jié)點(diǎn)上,即 hiredis-cluster 可以完成讀寫(xiě)分離和負(fù)載均衡的工作:

我的代碼在~/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

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

2.3. 開(kāi)啟傳輸中加密(TLS)及 auth 的 ElastiCache 集群測(cè)試

Amazon ElastiCache 傳輸中加密是一項(xiàng)可選功能,它允許您在數(shù)據(jù)最脆弱時(shí)候(從一個(gè)位置傳輸?shù)搅硪粋€(gè)位置時(shí))提高數(shù)據(jù)的安全性。由于在終端節(jié)點(diǎn)加密和解密數(shù)據(jù)時(shí)需要進(jìn)行一些處理,因此啟用傳輸中加密會(huì)對(duì)性能產(chǎn)生一些影響。應(yīng)對(duì)使用和不使用傳輸中加密的數(shù)據(jù)進(jìn)行基準(zhǔn)測(cè)試,以確定對(duì)使用案例的性能影響。 關(guān)于 TLS 加密這一塊,具體可以參考條條大路通羅馬- 使用 redission 連接 Amazon ElastiCache for redis 集群。

加載測(cè)試數(shù)據(jù)

1.使用 redis-cli 登錄開(kāi)啟了 TLS 和 auth 的 ElastiCache 集群并通過(guò) cluster nodes 命令獲取 slots 在 shards 中的分布,通過(guò) redis-cli 工具連接集群并進(jìn)行操作的具體方法請(qǐng)見(jiàn)使用 redis-cli 連接到 Redis 集群。

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

redis-cli -c -h -p

2.分別生成 testka,testb,testc 三個(gè) key,可以通過(guò)其 slot 值結(jié)合上文各個(gè) shard 上 slot 分布信息,判斷出 3 個(gè) key 恰好分布在 shard1,shard2 和 shard3 上

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

C代碼

1.以下是寫(xiě)入 ElastiCache 集群的代碼, 針對(duì)于 Elasticache 三個(gè)shard 上的 test1,test2,test3 三個(gè) key 進(jìn)行寫(xiě)入,請(qǐng)注意配置的地址為 ElastiCache 集群的 configuration endpoint,設(shè)置密碼,另外修改不同的 keyname。這里需要注意的是 hiredis-cluster 對(duì) SSL 的支持,可以分為兩種方式,一種是指定 SSL 證書(shū)路徑,另一種是指定證書(shū),對(duì)于 EC2 訪問(wèn) ElastiCache 的情況,指定證書(shū)路徑即可, 具體可參考 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 "<PASSWORD>"

int 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.5s

    redisClusterContext *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 error
        exit(-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 集群的代碼, 針對(duì)于 Elasticache 三個(gè)shard 上的 test1,test2,test3 三個(gè) key 進(jìn)行讀取,請(qǐng)注意配置的地址為 ElastiCache 集群的 configuration endpoint ,另外修改不同的 keyname。

#include "hircluster.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv) {
    UNUSED(argc);
    UNUSED(argv);
    struct timeval timeout = {1, 500000}; // 1.5s

    redisClusterContext *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 error
        exit(-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;
}

基本功能測(cè)試

1.使用如下命令編譯代碼:

gcc hiredisc-naw1.c -o hiredisc-naw1 -L./build -lhiredis_cluster /usr/local/lib/libhiredis.a -L~/hiredis -lhiredis_ssl

2.運(yùn)行程序并監(jiān)控 ElastiCache 未開(kāi)啟 TLS 及 Auth 集群的CurrConnections 指標(biāo),可以看到負(fù)載被均衡地發(fā)送到了6個(gè)節(jié)點(diǎn)上,即 hiredis-cluster 可以完成讀寫(xiě)分離和負(fù)載均衡的工作:

我的代碼在~/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

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

2.3. 開(kāi)啟傳輸中加密(TLS)及 auth 的 ElastiCache 集群測(cè)試

Amazon ElastiCache 傳輸中加密是一項(xiàng)可選功能,它允許您在數(shù)據(jù)最脆弱時(shí)候(從一個(gè)位置傳輸?shù)搅硪粋€(gè)位置時(shí))提高數(shù)據(jù)的安全性。由于在終端節(jié)點(diǎn)加密和解密數(shù)據(jù)時(shí)需要進(jìn)行一些處理,因此啟用傳輸中加密會(huì)對(duì)性能產(chǎn)生一些影響。應(yīng)對(duì)使用和不使用傳輸中加密的數(shù)據(jù)進(jìn)行基準(zhǔn)測(cè)試,以確定對(duì)使用案例的性能影響。 關(guān)于 TLS 加密這一塊,具體可以參考條條大路通羅馬- 使用 redission 連接 Amazon ElastiCache for redis 集群。

加載測(cè)試數(shù)據(jù)

1.使用 redis-cli 登錄開(kāi)啟了 TLS 和 auth 的 ElastiCache 集群并通過(guò) cluster nodes 命令獲取 slots 在 shards 中的分布,通過(guò) redis-cli 工具連接集群并進(jìn)行操作的具體方法請(qǐng)見(jiàn)使用 redis-cli 連接到 Redis 集群。

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

redis-cli -c -h -p

2.分別生成 testka,testb,testc 三個(gè) key,可以通過(guò)其 slot 值結(jié)合上文各個(gè) shard 上 slot 分布信息,判斷出 3 個(gè) key 恰好分布在shard1,shard2 和 shard3 上

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

C代碼

1.以下是寫(xiě)入 ElastiCache 集群的代碼, 針對(duì)于 Elasticache 三個(gè)shard 上的 test1,test2,test3 三個(gè) key 進(jìn)行寫(xiě)入,請(qǐng)注意配置的地址為 ElastiCache 集群的 configuration endpoint,設(shè)置密碼,另外修改不同的 keyname。這里需要注意的是 hiredis-cluster 對(duì)SSL的支持,可以分為兩種方式,一種是指定 SSL 證書(shū)路徑,另一種是指定證書(shū),對(duì)于 EC2 訪問(wèn) ElastiCache 的情況,指定證書(shū)路徑即可, 具體可參考 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 "<PASSWORD>"

int 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.5s

    redisClusterContext *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 error
        exit(-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 集群的代碼, 針對(duì)于 Elasticache 三個(gè)shard 上的 test1,test2,test3 三個(gè) key 進(jìn)行讀取,請(qǐng)注意配置的地址為 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 "<PASSWORD>"

int 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.5s

    redisClusterContext *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 error
        exit(-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;
}

基本功能測(cè)試

1.使用如下命令編譯代碼:

gcc hiredisc-aw1.c -o hiredisc-aw1 -L./build -lhiredis_cluster /usr/local/lib/libhiredis.a -L~/hiredis -lhiredis_ssl

2.運(yùn)行程序并監(jiān)控 ElastiCache 開(kāi)啟 TLS 及 Auth 集群的CurrConnections 指標(biāo),可以看到負(fù)載被均衡地發(fā)送到了 6 個(gè)節(jié)點(diǎn)上,即 hiredis-cluster 可以完成讀寫(xiě)分離和負(fù)載均衡的工作:

我的代碼在~/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

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

三級(jí)標(biāo)題

3.1. 未開(kāi)啟 TLS 及 auth 的 ElastiCache failover 測(cè)試

登錄 console,對(duì) Shard1 進(jìn)行 failover,然后觀測(cè)我們的C程序的輸出, 此過(guò)程大約持續(xù)12s ((觀測(cè)所得)),

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

從 ElastiCache 集群的每個(gè)節(jié)點(diǎn)的 CurrConnections 指標(biāo)截圖同樣可以看出,hiredis-cluster 將讀寫(xiě)請(qǐng)求都發(fā)送到了 shard1 新的主節(jié)點(diǎn)上:

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

3.2. 開(kāi)啟 TLS 及 auth 的 ElastiCache failover 測(cè)試

登錄 console,對(duì) Shard1 進(jìn)行 failover,然后觀測(cè)我們的C程序的輸出,這個(gè)時(shí)間大概 10s 左右(觀測(cè)所得),

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

從 ElastiCache 集群的每個(gè)節(jié)點(diǎn)的 CurrConnections 指標(biāo)截圖同樣可以看出, hiredis-cluster 將讀寫(xiě)請(qǐng)求都發(fā)送到了 shard1 新的主節(jié)點(diǎn)上:

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

failover 測(cè)試中發(fā)生問(wèn)題以及解決方案

在做 failover 的時(shí)候發(fā)現(xiàn)一個(gè)問(wèn)題,在 failover 過(guò)程中,有的時(shí)候client 端直接斷開(kāi)鏈接,有的時(shí)候成功,后來(lái)經(jīng)過(guò)分析 hiredis 的代碼得知需要把 timeout 時(shí)間調(diào)整下,所以從 1.5s 調(diào)整為 5.5s, 注意這個(gè)值并不是最佳實(shí)踐,是根據(jù)測(cè)試得來(lái)的。

4. 結(jié)束語(yǔ)

在本博客中,主要使用C語(yǔ)言通過(guò) hiredis-cluster 連接和操作ElastiCache 集群,從簡(jiǎn)單的 Demo 中我們可以看到 hiredis-cluster 可以很好地支持 ElastiCache 集群開(kāi)啟 TLS 及 auth 等功能,并自動(dòng)完成讀寫(xiě)分離,負(fù)載均衡,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官方博客

相關(guān)博客

條條大路通羅馬 —— 使用 redisson 連接 Amazon ElastiCache for redis 集群

條條大路通羅馬 —— 使用 redis-py 訪問(wèn) Amazon ElastiCache for redis集群

條條大路通羅馬 —— 使用 go-redis 連接 Amazon ElastiCache for Redis 集群

本篇作者

條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群,redis,bootstrap,數(shù)據(jù)庫(kù)

馮秋爽

Amazon 解決方案架構(gòu)師,負(fù)責(zé)跨國(guó)企業(yè)級(jí)客戶(hù)基于 Amazon 的技術(shù)架構(gòu)設(shè)計(jì)、咨詢(xún)和設(shè)計(jì)優(yōu)化工作。在加入 Amazon 之前曾就職于 IBM 、甲骨文等 IT 企業(yè),積累了豐富的程序開(kāi)發(fā)和數(shù)據(jù)庫(kù)的實(shí)踐經(jīng)驗(yàn)。

文章來(lái)源:https://dev.amazoncloud.cn/column/article/630a21a42ecbae73705ffb40?sc_medium=regulartraffic&amp;sc_campaign=crossplatform&amp;sc_channel=CSDN?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-637016.html

到了這里,關(guān)于條條大路通羅馬系列—— 使用 Hiredis-cluster 連接 Amazon ElastiCache for Redis 集群的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 羅馬數(shù)字轉(zhuǎn)整數(shù)——leetcode.13

    引言: 本篇博客屬于題解篇,是我在刷力扣的題后給大家總結(jié)了一下解題思路以及代碼實(shí)現(xiàn),并在后面分析了map與unordered_map的區(qū)別。我也會(huì)分享我的做題思路,一起來(lái)看看吧。 題目: 羅馬數(shù)字包含以下七種字符:? I ,? V ,? X ,? L , C , D ?和? M 。 例如, 羅馬數(shù)字?

    2024年04月13日
    瀏覽(27)
  • 面試經(jīng)典150題——羅馬數(shù)字轉(zhuǎn)整數(shù)

    羅馬數(shù)字包含以下七種字符:? I ,? V ,? X ,? L , C , D ?和? M 。 例如, 羅馬數(shù)字 2 寫(xiě)做? II ?,即為兩個(gè)并列的 1 。 12 寫(xiě)做? XII ?,即為? X ?+? II ?。 27 寫(xiě)做?? XXVII , 即為? XX ?+? V ?+? II ?。 通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右邊。但也存在特例,例

    2024年02月12日
    瀏覽(22)
  • 【Leetcode刷題】算法:羅馬數(shù)字轉(zhuǎn)整數(shù)

    【Leetcode刷題】算法:羅馬數(shù)字轉(zhuǎn)整數(shù)

    定義一個(gè) Solution 類(lèi),該類(lèi)包含一個(gè) romanToInt 方法用于將羅馬數(shù)字轉(zhuǎn)換為整數(shù)。 初始化變量 answer 為 0,用于保存轉(zhuǎn)換后的整數(shù)值。 獲取輸入字符串 s 的長(zhǎng)度,并保存在變量 length 中。 創(chuàng)建一個(gè)字典 d,將每個(gè)羅馬數(shù)字字符與對(duì)應(yīng)的數(shù)值進(jìn)行映射。 使用 for 循環(huán)遍歷 s 中的每個(gè)

    2024年02月05日
    瀏覽(17)
  • 【古希臘羅馬神話】期末結(jié)課論文

    1、這門(mén)課疫情時(shí)期的結(jié)課方式為論文,非疫情時(shí)期的結(jié)課方式為開(kāi)卷考試,本文內(nèi)容請(qǐng)酌情參考。 2、本專(zhuān)欄從【英語(yǔ)演講】改為【英語(yǔ)公選課】,旨在提供各類(lèi)非普通讀寫(xiě)/聽(tīng)說(shuō)課的學(xué)習(xí)資料參考,切忌照搬作業(yè)! 3、當(dāng)時(shí)這篇論文的要求好像是字?jǐn)?shù)在800字左右,剖析一個(gè)課

    2024年02月02日
    瀏覽(16)
  • leetcode13題羅馬數(shù)字轉(zhuǎn)成整數(shù)
  • LeeCode前端算法基礎(chǔ)100題(18)整數(shù)轉(zhuǎn)羅馬數(shù)字

    一、問(wèn)題詳情: 羅馬數(shù)字包含以下七種字符:? I ,? V ,? X ,? L , C , D ?和? M 。 例如, 羅馬數(shù)字 2 寫(xiě)做? II ?,即為兩個(gè)并列的 1。12 寫(xiě)做? XII ?,即為? X ?+? II ?。 27 寫(xiě)做?? XXVII , 即為? XX ?+? V ?+? II ?。 通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右邊。但

    2024年01月18日
    瀏覽(21)
  • LeeCode前端算法基礎(chǔ)100題(17)- 羅馬數(shù)字轉(zhuǎn)整數(shù)

    羅馬數(shù)字包含以下七種字符:? I ,? V ,? X ,? L , C , D ?和? M 。 例如, 羅馬數(shù)字? 2 ?寫(xiě)做? II ?,即為兩個(gè)并列的 1 。 12 ?寫(xiě)做? XII ?,即為? X ?+? II ?。? 27 ?寫(xiě)做?? XXVII , 即為? XX ?+? V ?+? II ?。 通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右邊。但也存在特

    2024年01月19日
    瀏覽(21)
  • 暴力破解(if循環(huán))解決leetcode數(shù)字轉(zhuǎn)成羅馬數(shù)字

    暴力破解(if循環(huán))解決leetcode數(shù)字轉(zhuǎn)成羅馬數(shù)字

    1.題目描述 2.解題思路 剛看到這個(gè)題目的時(shí)候,感覺(jué)說(shuō)的有點(diǎn)啰嗦,其實(shí)不難發(fā)現(xiàn),這個(gè)題目和之前的給你多少錢(qián),什么2元,5元的,給你一個(gè)數(shù)字,讓你算各種錢(qián)幣有多少?gòu)?。無(wú)非就是從小到大進(jìn)行判斷,首先判斷給定的數(shù)字,能容納多少個(gè)最大的,然后依次減少。 3.代碼

    2024年02月19日
    瀏覽(20)
  • Java | Leetcode Java題解之第13題羅馬數(shù)字轉(zhuǎn)整數(shù)
  • LeetCode面向運(yùn)氣之Javascript—第13題-羅馬數(shù)字轉(zhuǎn)整數(shù)-99.21%

    LeetCode面向運(yùn)氣之Javascript—第13題-羅馬數(shù)字轉(zhuǎn)整數(shù)-99.21%

    給定一個(gè)羅馬數(shù)字,將其轉(zhuǎn)換成整數(shù)。 羅馬數(shù)字包含以下七種字符: I, V, X, L,C,D 和 M 分別代表1,5,10,50,100,500,1000 羅馬數(shù)字 2 寫(xiě)做 II ,即為兩個(gè)并列的 1 。12 寫(xiě)做 XII ,即為 X + II 。 27 寫(xiě)做 XXVII, 即為 XX + V + II 。 通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右

    2024年02月07日
    瀏覽(22)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包