forked from docs/doc-exports
Reviewed-by: Mützel, Andrea <andrea.muetzel@t-systems.com> Co-authored-by: Chen, Junjie <chenjunjie@huawei.com> Co-committed-by: Chen, Junjie <chenjunjie@huawei.com>
93 lines
12 KiB
HTML
93 lines
12 KiB
HTML
<a name="dcs-ug-0312010"></a><a name="dcs-ug-0312010"></a>
|
|
|
|
<h1 class="topictitle1">hiredis in C++</h1>
|
|
<div id="body8662426"><p id="dcs-ug-0312010__en-us_topic_0148195243_p1484772321512"><span class="keyword" id="dcs-ug-0312010__en-us_topic_0148195243_keyword97571353828">Access a DCS Redis instance through hiredis</span> on an ECS in the same VPC. For more information about how to use other Redis clients, visit <a href="https://redis.io/clients" target="_blank" rel="noopener noreferrer">the Redis official website</a>.</p>
|
|
<div class="note" id="dcs-ug-0312010__en-us_topic_0148195243_note20184812103319"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="dcs-ug-0312010__en-us_topic_0148195243_p141841112113312">The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To use C++ to connect to a Redis Cluster instance, see the <a href="https://github.com/sewenew/redis-plus-plus?_ga=2.64990636.268662337.1603553558-977760105.1588733325#redis-cluster" target="_blank" rel="noopener noreferrer">C++ Redis client description</a>.</p>
|
|
</div></div>
|
|
<div class="section" id="dcs-ug-0312010__en-us_topic_0148195243_section1502270695932"><h4 class="sectiontitle">Prerequisites</h4><ul id="dcs-ug-0312010__en-us_topic_0148195243_ul46828691102415"><li id="dcs-ug-0312010__en-us_topic_0148195243_li7215915117">A DCS Redis instance has been created and is in the <strong id="dcs-ug-0312010__en-us_topic_0148195243_b141030376248">Running</strong> state.</li><li id="dcs-ug-0312010__en-us_topic_0148195243_li132154114111">An ECS has been created. For details about how to create an ECS, see <a href="https://docs.otc.t-systems.com/en-us/usermanual/ecs/en-us_topic_0163572588.html" target="_blank" rel="noopener noreferrer"><em id="dcs-ug-0312010__en-us_topic_0148195243_i10484132165917">Elastic Cloud Server User Guide</em></a>.</li><li id="dcs-ug-0312010__en-us_topic_0148195243_li152161914112">If the ECS runs the Linux OS, ensure that the GCC compilation environment has been installed on the ECS.</li></ul>
|
|
</div>
|
|
<div class="section" id="dcs-ug-0312010__en-us_topic_0148195243_section6715101110310"><h4 class="sectiontitle">Procedure</h4><ol id="dcs-ug-0312010__en-us_topic_0148195243_ol4547181014320"><li id="dcs-ug-0312010__en-us_topic_0148195243_li1655151054317"><a name="dcs-ug-0312010__en-us_topic_0148195243_li1655151054317"></a><a name="en-us_topic_0148195243_li1655151054317"></a><span>View the IP address/domain name and port number of the DCS Redis instance to be accessed.</span><p><p id="dcs-ug-0312010__en-us_topic_0148195243_p165602107438">For details, see <a href="dcs-ug-0312016.html">Viewing Details of a DCS Instance</a>.</p>
|
|
</p></li><li id="dcs-ug-0312010__en-us_topic_0148195243_li175501736825"><span>Log in to the ECS.</span><p><p id="dcs-ug-0312010__en-us_topic_0148195243_p1453163716214">The following uses CentOS as an example to describe how to access an instance in C++.</p>
|
|
</p></li><li id="dcs-ug-0312010__en-us_topic_0148195243_li240661954320"><span>Install GCC, Make, and hiredis.</span><p><p id="dcs-ug-0312010__en-us_topic_0148195243_p1810485434">If the system does not provide a compiling environment, run the following <strong id="dcs-ug-0312010__en-us_topic_0148195243_b139421447111315">yum</strong> command to install the environment:</p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p1758515454317"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b83683313445">yum install gcc make</strong></p>
|
|
</p></li><li id="dcs-ug-0312010__en-us_topic_0148195243_li168794412430"><span>Run the following command to download and decompress the hiredis package:</span><p><p id="dcs-ug-0312010__en-us_topic_0148195243_p13441219164410"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b33877298443">wget https://github.com/redis/hiredis/archive/master.zip</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p13183185616414"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b31517153515">unzip</strong> <strong id="dcs-ug-0312010__en-us_topic_0148195243_b113214121151">master.zip</strong></p>
|
|
</p></li><li id="dcs-ug-0312010__en-us_topic_0148195243_li1868311174411"><span>Go to the directory where the decompressed hiredis package is saved, and compile and install hiredis.</span><p><p id="dcs-ug-0312010__en-us_topic_0148195243_p674825304518"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b3481506461">make</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p274825318456"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b144914016460">make install</strong></p>
|
|
</p></li><li id="dcs-ug-0312010__en-us_topic_0148195243_li13528104214416"><span>Access the DCS instance by using hiredis.</span><p><p id="dcs-ug-0312010__en-us_topic_0148195243_p4708144210467">The following describes connection and password authentication of hiredis. For more information on how to use hiredis, visit the Redis official website.</p>
|
|
<ol type="a" id="dcs-ug-0312010__en-us_topic_0148195243_ol246018313477"><li id="dcs-ug-0312010__en-us_topic_0148195243_li174601935474">Edit the sample code for connecting to a DCS instance, and then save the code and exit.<p id="dcs-ug-0312010__en-us_topic_0148195243_p1964420264473"><a name="dcs-ug-0312010__en-us_topic_0148195243_li174601935474"></a><a name="en-us_topic_0148195243_li174601935474"></a><strong id="dcs-ug-0312010__en-us_topic_0148195243_b16614285382">vim connRedis.c</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p1094372924718">Example:</p>
|
|
<pre class="screen" id="dcs-ug-0312010__en-us_topic_0148195243_screen121620566485">#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <hiredis.h>
|
|
int main(int argc, char **argv) {
|
|
unsigned int j;
|
|
redisContext *conn;
|
|
redisReply *reply;
|
|
if (argc < 3) {
|
|
printf("Usage: example {instance_ip_address} 6379 {password}\n");
|
|
exit(0);
|
|
}
|
|
const char *hostname = argv[1];
|
|
const int port = atoi(argv[2]);
|
|
const char *password = argv[3];
|
|
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
|
|
conn = redisConnectWithTimeout(hostname, port, timeout);
|
|
if (conn == NULL || conn->err) {
|
|
if (conn) {
|
|
printf("Connection error: %s\n", conn->errstr);
|
|
redisFree(conn);
|
|
} else {
|
|
printf("Connection error: can't allocate redis context\n");
|
|
}
|
|
exit(1);
|
|
}
|
|
/* AUTH */
|
|
reply = redisCommand(conn, "AUTH %s", password);
|
|
printf("AUTH: %s\n", reply->str);
|
|
freeReplyObject(reply);
|
|
|
|
/* Set */
|
|
reply = redisCommand(conn,"SET %s %s", "welcome", "Hello, DCS for Redis!");
|
|
printf("SET: %s\n", reply->str);
|
|
freeReplyObject(reply);
|
|
|
|
/* Get */
|
|
reply = redisCommand(conn,"GET welcome");
|
|
printf("GET welcome: %s\n", reply->str);
|
|
freeReplyObject(reply);
|
|
|
|
/* Disconnects and frees the context */
|
|
redisFree(conn);
|
|
return 0;
|
|
}</pre>
|
|
</li><li id="dcs-ug-0312010__en-us_topic_0148195243_li45820814718">Run the following command to compile the code:<p id="dcs-ug-0312010__en-us_topic_0148195243_p1852713584915"><a name="dcs-ug-0312010__en-us_topic_0148195243_li45820814718"></a><a name="en-us_topic_0148195243_li45820814718"></a><strong id="dcs-ug-0312010__en-us_topic_0148195243_b8107105114912">gcc connRedis.c -o connRedis -I /usr/local/include/hiredis -lhiredis</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p1094812195020">If an error is reported, locate the directory where the <strong id="dcs-ug-0312010__en-us_topic_0148195243_b7579138151">hiredis.h</strong> file is saved and modify the compilation command.</p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p6781027145015">After the compilation, an executable <strong id="dcs-ug-0312010__en-us_topic_0148195243_b3152616191515">connRedis</strong> file is obtained.</p>
|
|
</li><li id="dcs-ug-0312010__en-us_topic_0148195243_li13134113319491">Run the following command to access the chosen DCS Redis instance:<p id="dcs-ug-0312010__en-us_topic_0148195243_p11552173917421"><a name="dcs-ug-0312010__en-us_topic_0148195243_li13134113319491"></a><a name="en-us_topic_0148195243_li13134113319491"></a><strong id="dcs-ug-0312010__en-us_topic_0148195243_b0951032135312">./connRedis</strong> <strong id="dcs-ug-0312010__en-us_topic_0148195243_b19583217534"><em id="dcs-ug-0312010__en-us_topic_0148195243_i1795123212531">{redis_instance_address}</em></strong> <strong id="dcs-ug-0312010__en-us_topic_0148195243_b1059544195310">6379</strong> <strong id="dcs-ug-0312010__en-us_topic_0148195243_b1759514118539"><em id="dcs-ug-0312010__en-us_topic_0148195243_i759515415537">{password}</em></strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p156153275218"><em id="dcs-ug-0312010__en-us_topic_0148195243_i213424919573">{redis_instance_address}</em> indicates the IP address/domain name of DCS instance and <span class="parmname" id="dcs-ug-0312010__en-us_topic_0148195243_parmname1113414985714"><b>6379</b></span> is an example port number of DCS instance. For details about how to obtain the IP address/domain name and port, see <a href="#dcs-ug-0312010__en-us_topic_0148195243_li1655151054317">1</a>. Change them as required. <em id="dcs-ug-0312010__en-us_topic_0148195243_i6461078717">{password}</em> indicates the password used to log in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.</p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p9507135912526">You have successfully accessed the instance if the following command output is displayed:</p>
|
|
<pre class="screen" id="dcs-ug-0312010__en-us_topic_0148195243_screen17216114885311">AUTH: OK
|
|
SET: OK
|
|
GET welcome: Hello, DCS for Redis!</pre>
|
|
</li></ol>
|
|
<div class="notice" id="dcs-ug-0312010__en-us_topic_0148195243_note773193685410"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="dcs-ug-0312010__en-us_topic_0148195243_p15747364548">If an error is reported, indicating that the hiredis library files cannot be found, run the following commands to copy related files to the system directories and add dynamic links:</p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p1131017645513"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b2033118283556">mkdir /usr/lib/hiredis</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p15310146135512"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b8334182895518">cp /usr/local/lib/libhiredis.so.0.13 /usr/lib/hiredis/</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p19310186205520"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b93341128115510">mkdir /usr/include/hiredis</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p183101363555"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b16334102814551">cp /usr/local/include/hiredis/hiredis.h /usr/include/hiredis/</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p931096105514"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b15334102895513">echo '/usr/local/lib' >>;>>;/etc/ld.so.conf</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p113121695518"><strong id="dcs-ug-0312010__en-us_topic_0148195243_b1233682817553">ldconfig</strong></p>
|
|
<p id="dcs-ug-0312010__en-us_topic_0148195243_p6269181419559">Replace the locations of the <strong id="dcs-ug-0312010__en-us_topic_0148195243_b1165117397189">so</strong> and <strong id="dcs-ug-0312010__en-us_topic_0148195243_b136511397185">.h</strong> files with actual ones before running the commands.</p>
|
|
</div></div>
|
|
</p></li></ol>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dcs-ug-0512002.html">Access in Different Languages</a></div>
|
|
</div>
|
|
</div>
|
|
|