所谓“难者不会,会者不难”,这不只是一句简简单单的俗语,而是历经人类进化史而得到的历史经验,只有亲身体会过才会越发了解这句话的意义。当你苦思冥想几天几夜难以解决的问题,一句过来人的点拨便可瞬间化解,这是人生路上的需要经历的,愿每个人在关键时刻都能得到善良人的点拨。
人生有四个境界:①不知道自己不知道;②知道自己不知道;③不知道自己知道,④知道自己知道。不同的境界对应着不同的人生,仔细想想这四种境界并没有哪一种是绝对的好与坏,具体怎么选择取决于你想要什么样的人生,想想坐井观天的那只青蛙,属于“不知道自己不知道”的典型,如果它每天逍遥快活,完全没必要非得跳出井来卷入井外的纷争。
长久以来自给自足的小农经济,如果没有战乱,没有暴政,没有入侵,耕田织布的生活不也挺好,但是人性是贪婪的,欲望促使人们侵略、扩张,使得一些人进入了“知道自己不知道”的境界,于是加速了人与人之间的交流。进入现代社会以后,信息爆炸,越来越多的人“觉醒”了,但只是“觉醒”而不付出努力就会使自己陷入焦虑,其实焦虑不可怕,重要的是你要采取什么措施来缓解焦虑。
题外话扯多了,回归正题,写这篇总结的主要还是一个学习记录,也希望这样的记录可以解决一些人的困惑,这些东西并不复杂,也许只是我这个初学者才会关心的问题,但是现在有一种现象,就是大佬们不愿写基础的东西,而小白们想成为大佬从基础学习,却发现路上的坑让他们头破血流。一句学神们口中的“不难证明”,可能你几个月都证明不出来,这个问题是双方面的,一方面小白认为这东西很难,不知道怎么入手,另一方面大神感觉这东西这么简单还需要学?
屠龙勇士中成恶龙,大神们经过时间的洗礼,很多人已经忘记了自己曾经的头破血流,忘记了自己怎么从小白一步步走来,所以遇到困惑的人不要先入为主,不要认为这个东西太简单了怎么可能有人不会?耐心聆听给出建议,或许可以瞬间打通他的任督二脉。
好了,好了,又跑题了,拉回来,我们从头来搭建一个redis集群,不过搭建之前我们先来看看“安装”这个词。
安装软件、安装程序、安装APP,自从电子设备飞入寻常百姓家,我们每天都在安装、安装、安装。但是这个词并不是电子设备出现后才诞生的。这个词自古就有,安装——本意是:把机件、器材等固定在一定的位置。那么软件、APP的安装就是把它们放在一定的位置。
软件是什么?至今我都记得初中的一道计算机的题目,我是04年在学校机房第一次接触到真的计算机,当时有一道题是问:Windows是系统软件,还是应用软件?当时这种题目真的是死记硬背啊,脑子里完全没有概念,每周只有一节课,还不一定每个人都有机器,那时的软件对我来说就是一个迷。
现在接触计算机也这么多年了,对软件也有了一些认识。软件可以说是用来向发出计算机发出任务的东西,当我们对一个人发号任务指令时,能通过声音传递给人,然后接到指令的人可以完成相应的任务,当我们给计算机发送指令时,可以通过键盘、鼠标等把指令输进去,也可以运行软件来完成某些计算机任务,其实软件就是一些提前编好的任务指令的集合。
那么安装软件就是把软件放到对应的地方,为什么要安装?为什么要放到指定的地方?随便放有什么问题?其实还是为了方便。就像生活中我们把床安装在卧室,把马桶安装在厕所一样,大家都是这样,已经成了习惯。如果你把床安装在厕所,并不是不能用来睡觉,只是其他人在来到厕所睡觉这件事上有些不太习惯。
因为软件并不只是一个东西,他还有数据啊、配置啊、服务啊等等,所以这些东西都要放在相应的位置,并且做一个登记,就像你把床放到厕所一样,这种不一样的习惯更要登记下来,否则对于一个普通人来说,肯定会去卧室找床,而不是厕所。
windows中软件的傻瓜式安装降低了软件的安装难度,它有一个注册表,里面记录了很多奇奇怪怪的东西,在windows上安装软件也就是把软件的各部分发到对应的位置,然后通过注册表这个总管登记入册,但是有些软件不太友好,安装的时候一顿乱放,卸载的时候不自己清理干净,导致出现很多问题,比如当年的SQLServer和3DMax,当时为了再次安装不得不重做了好多次系统。
在linux中安装软件就没有注册表这个东西,只要把软件各部分按照类型分别放好就行,比如可执行程序放/usr/bin/
,库文件放/usr/lib
,配置文件放 /etc/
等等,卸载就是把这些新加的东西删除就可以了,相比windows的注册表要简单一些。
好了,接下来我们就来安装配置一下redis-cluster,看了一下,本地机器真的很干净,什么都没有,现在从安装redis开始吧。
sudo apt-get install redis-server
安装吧,当然你也可以通过源码编译安装,这都是可以的,输出信息如下:albert@home-pc:/usr/src$$ sudo apt-get install redis-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libjemalloc1 redis-tools
Suggested packages:
ruby-redis
The following NEW packages will be installed:
libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 125 not upgraded.
Need to get 519 kB of archives.
After this operation, 1,507 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78.9 kB]
Get:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial-updates/universe amd64 redis-tools amd64 2:3.0.6-1ubuntu0.4 [95.5 kB]
Get:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu xenial-updates/universe amd64 redis-server amd64 2:3.0.6-1ubuntu0.4 [344 kB]
Fetched 519 kB in 13s (38.0 kB/s)
Selecting previously unselected package libjemalloc1.
(Reading database ... 33895 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ...
Unpacking libjemalloc1 (3.6.0-9ubuntu1) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_2%3a3.0.6-1ubuntu0.4_amd64.deb ...
Unpacking redis-tools (2:3.0.6-1ubuntu0.4) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_2%3a3.0.6-1ubuntu0.4_amd64.deb ...
Unpacking redis-server (2:3.0.6-1ubuntu0.4) ...
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ureadahead (0.100.0-19.1) ...
Processing triggers for systemd (229-4ubuntu21.27) ...
Setting up libjemalloc1 (3.6.0-9ubuntu1) ...
Setting up redis-tools (2:3.0.6-1ubuntu0.4) ...
Setting up redis-server (2:3.0.6-1ubuntu0.4) ...
invoke-rc.d: could not determine current runlevel
Processing triggers for libc-bin (2.23-0ubuntu11) ...
Processing triggers for ureadahead (0.100.0-19.1) ...
Processing triggers for systemd (229-4ubuntu21.27) ...
albert@home-pc:/usr/src$
/usr/bin/
albert@home-pc:/usr/src$ which redis-server
/usr/bin/redis-server
albert@home-pc:/usr/src$ which redis-cli
/usr/bin/redis-cli
sudo vim /etc/redis/redis.conf
命令打开文件搜索 requirepass 找到下面这一行,把前面的#去掉,requirepass 后面的就是密码,可以自己修改。# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared #把这一行前面的'#'去掉就行,密码我就不改了
# Command renaming.
sudo redis-server /etc/redis/redis.conf
,redis服务器就起来了albert@home-pc:/mnt/c/Users/Albert$ sudo redis-server /etc/redis/redis.conf
albert@home-pc:/mnt/c/Users/Albert$ ps -ef | grep redis
root 663 1 0 13:52 ? 00:00:00 redis-server 127.0.0.1:6379
foobared
albert@home-pc:/mnt/c/Users/Albert$ redis-cli -a foobared -p 6379
127.0.0.1:6379> set name redis-test
OK
127.0.0.1:6379> get name
"redis-test"
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> get age
"18"
127.0.0.1:6379>
set
和get
两个命令如上忽然发现Ubuntu16.04自带的 redis3.0.6
版本太低了,无法自动配置集群,所以决定编译安装,如果自动安装完redis版本在5以上,那就不需要编译安装了。
/usr/local/redis/
目录并进入,然后下载redis源码redis-6.2.4.tar.gzalbert@home-pc:/usr/local$ mkdir redis
albert@home-pc:/usr/local$ sudo mkdir redis
albert@home-pc:/usr/local$ cd redis/
albert@home-pc:/usr/local/redis$ sudo wget https://download.redis.io/releases/redis-6.2.4.tar.gz
--2021-06-26 14:10:42-- https://download.redis.io/releases/redis-6.2.4.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2457940 (2.3M) [application/octet-stream]
Saving to: ‘redis-6.2.4.tar.gz’
redis-6.2.4.tar.gz 100%[========================================>] 2.34M 45.4KB/s in 54s
2021-06-26 14:11:37 (44.3 KB/s) - ‘redis-6.2.4.tar.gz’ saved [2457940/2457940]
albert@home-pc:/usr/local/redis$
运行 sudo tar -zxvf redis-6.2.4.tar.gz
命令解压文件
运行 cd redis-6.2.4
进入 redis-6.2.4 目录
直接运行 sudo make && make install
即可,如果遇到了 ‘/bin/sh: 1: pkg-config: not found’ 错误,先通过 sudo apt-get install pkg-config
安装 pkg-config
,再运行 make
即可。
这次 redis-server
和 redis-cli
都被安装到了 /usr/local/bin/
目录,版本是 6.2.4
,接下来我们就用这个版本来搭建redis集群
为什么要用redis集群?很直接的原因就是一个redis不够用了,需要使用好几个来组合分摊数据量和压力,那么搭建redis集群其实就是启动多个redis程序实例,让他们配合工作就好了,他们之间的配合是redis本身实现的,我们只需要配置启动多个实例就行了,因为redis集群最少需要三主三从,所以我们启动6个实例,:
/usr/local/redis-cluster
目录并进入albert@home-pc:/usr/local$ sudo mkdir redis-cluster
albert@home-pc:/usr/local$ cd redis-cluster/
.
├── conf
│ ├── redis-6301.conf
│ ├── redis-6302.conf
│ ├── redis-6303.conf
│ ├── redis-6304.conf
│ ├── redis-6305.conf
│ └── redis-6306.conf
├── data
│ ├── redis-6301
│ ├── redis-6302
│ ├── redis-6303
│ ├── redis-6304
│ ├── redis-6305
│ └── redis-6306
└── log
pidfile /var/run/redis/redis-server.pid
port 6379
logfile /var/log/redis/redis-server.log
dir /var/ilib/redis
dbfilename dump.rdb
#requirepass foobared
#cluster-enabled yes
#cluster-config-file nodes-6379.conf
#cluster-node-timeout 15000
文件 redis-6301.conf
修改后的文件内容如下,接下来5个配置文件分别按照6302, 6303, 6304, 6305, 6306来配置:
pidfile /var/run/redis/redis-6301-server.pid
port 6301
logfile /usr/local/redis-cluster/log/redis-6301-server.log
dir /usr/local/redis-cluster/data/redis-6301
dbfilename dump-6301.rdb
requirepass foobared
cluster-enabled yes
cluster-config-file nodes-6301.conf
cluster-node-timeout 15000
/usr/local/redis-cluster
目录并启动实例:albert@home-pc:/usr/local/redis-cluster$ sudo /usr/local/bin/redis-server conf/redis-6301.conf
albert@home-pc:/usr/local/redis-cluster$ sudo /usr/local/bin/redis-server conf/redis-6302.conf
albert@home-pc:/usr/local/redis-cluster$ sudo /usr/local/bin/redis-server conf/redis-6303.conf
albert@home-pc:/usr/local/redis-cluster$ sudo /usr/local/bin/redis-server conf/redis-6304.conf
albert@home-pc:/usr/local/redis-cluster$ sudo /usr/local/bin/redis-server conf/redis-6305.conf
albert@home-pc:/usr/local/redis-cluster$ sudo /usr/local/bin/redis-server conf/redis-6306.conf
albert@home-pc:/usr/local/redis-cluster$ ps -ef | grep redis
root 663 1 0 13:52 ? 00:00:00 redis-server 127.0.0.1:6379
root 5928 1 0 14:57 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6301 [cluster]
root 5935 1 0 14:57 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6302 [cluster]
root 5942 1 0 14:57 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6303 [cluster]
root 5949 1 0 14:57 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6304 [cluster]
root 5956 1 0 14:57 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6305 [cluster]
root 5963 1 0 14:57 ? 00:00:00 /usr/local/bin/redis-server 127.0.0.1:6306 [cluster]
此时查看目录已经生成了默认的节点文件:
albert@home-pc:/usr/local/redis-cluster$ tree
.
├── conf
│ ├── redis-6301.conf
│ ├── redis-6302.conf
│ ├── redis-6303.conf
│ ├── redis-6304.conf
│ ├── redis-6305.conf
│ └── redis-6306.conf
├── data
│ ├── redis-6301
│ │ └── nodes-6301.conf
│ ├── redis-6302
│ │ └── nodes-6302.conf
│ ├── redis-6303
│ │ └── nodes-6303.conf
│ ├── redis-6304
│ │ └── nodes-6304.conf
│ ├── redis-6305
│ │ └── nodes-6305.conf
│ └── redis-6306
│ └── nodes-6306.conf
└── log
├── redis-6301-server.log
├── redis-6302-server.log
├── redis-6303-server.log
├── redis-6304-server.log
├── redis-6305-server.log
└── redis-6306-server.log
9 directories, 18 files
redis-cli -a foobared --cluster create 127.0.0.1:6301 127.0.0.1:6302 127.0.0.1:6303 127.0.0.1:6304 127.0.0.1:6305 127.0.0.1:6306 --cluster-replicas 1
即可,命令中的 --cluster-replicas 1
表示为每个主节点都提供一个从节点,中间还需要输入 yes,这样运行完就形成了一个三主三从的redis集群:albert@home-pc:/usr/local/redis-cluster$ redis-cli -a foobared --cluster create 127.0.0.1:6301 127.0.0.1:6302 127.0.0.1:6303 127.0.0.1:6304 127.0.0.1:6305 127.0.0.1:6306 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6305 to 127.0.0.1:6301
Adding replica 127.0.0.1:6306 to 127.0.0.1:6302
Adding replica 127.0.0.1:6304 to 127.0.0.1:6303
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 5d60bdad15a6f8ceec188a1081e9381f181a5c5e 127.0.0.1:6301
slots:[0-5460] (5461 slots) master
M: 8c814d4a32763d47723398fcf8f596d7b6340afc 127.0.0.1:6302
slots:[5461-10922] (5462 slots) master
M: 7bc53b512772c3a1df3217facca283ff9564d32d 127.0.0.1:6303
slots:[10923-16383] (5461 slots) master
S: b39bdb5b6e720e9fbedd43e58b57661910dcc3d7 127.0.0.1:6304
replicates 5d60bdad15a6f8ceec188a1081e9381f181a5c5e
S: 5fef2bedd430bf86cdff63cb2f852aeb21e1b18f 127.0.0.1:6305
replicates 8c814d4a32763d47723398fcf8f596d7b6340afc
S: 0123b160087743a5296807145b426d9b9cefcf21 127.0.0.1:6306
replicates 7bc53b512772c3a1df3217facca283ff9564d32d
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:6301)
M: 5d60bdad15a6f8ceec188a1081e9381f181a5c5e 127.0.0.1:6301
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b39bdb5b6e720e9fbedd43e58b57661910dcc3d7 127.0.0.1:6304
slots: (0 slots) slave
replicates 5d60bdad15a6f8ceec188a1081e9381f181a5c5e
M: 7bc53b512772c3a1df3217facca283ff9564d32d 127.0.0.1:6303
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 5fef2bedd430bf86cdff63cb2f852aeb21e1b18f 127.0.0.1:6305
slots: (0 slots) slave
replicates 8c814d4a32763d47723398fcf8f596d7b6340afc
M: 8c814d4a32763d47723398fcf8f596d7b6340afc 127.0.0.1:6302
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 0123b160087743a5296807145b426d9b9cefcf21 127.0.0.1:6306
slots: (0 slots) slave
replicates 7bc53b512772c3a1df3217facca283ff9564d32d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
当使用redis-cli
连接集群的时候,直接连3个主节点的任意一个就可以,把cluster当成一个整理来看待,测试如下:
albert@home-pc:/usr/local/redis-cluster$ redis-cli -a foobared -p 6301
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6301> get name
(error) MOVED 5798 127.0.0.1:6302
127.0.0.1:6301> set name cluster-test
(error) MOVED 5798 127.0.0.1:6302
127.0.0.1:6301>
我们发现测试Redis命令的时候报错了,显示 (error) MOVED 5798 127.0.0.1:6302
,这说明 name
这个键不在端口为 6301 这个实例上,而在端口为 6302 这个实例上,使用 cluster nodes
命令可以查询集群的节点信息
127.0.0.1:6301> cluster nodes
b39bdb5b6e720e9fbedd43e58b57661910dcc3d7 127.0.0.1:6304@16304 slave 5d60bdad15a6f8ceec188a1081e9381f181a5c5e 0 1624711999000 1 connected
7bc53b512772c3a1df3217facca283ff9564d32d 127.0.0.1:6303@16303 master - 0 1624712000000 3 connected 10923-16383
5fef2bedd430bf86cdff63cb2f852aeb21e1b18f 127.0.0.1:6305@16305 slave 8c814d4a32763d47723398fcf8f596d7b6340afc 0 1624711998971 2 connected
5d60bdad15a6f8ceec188a1081e9381f181a5c5e 127.0.0.1:6301@16301 myself,master - 0 1624712000000 1 connected 0-5460
8c814d4a32763d47723398fcf8f596d7b6340afc 127.0.0.1:6302@16302 master - 0 1624711999978 2 connected 5461-10922
0123b160087743a5296807145b426d9b9cefcf21 127.0.0.1:6306@16306 slave 7bc53b512772c3a1df3217facca283ff9564d32d 0 1624712000991 3 connected
这种情况下可以手动连接端口为6302的节点,也可以在连接 6301 时添加一个 -c
参数,这样在当前实例找不到指定的键时会自动切换,下面测试一下:
albert@home-pc:/usr/local/redis-cluster$ redis-cli -a foobared -p 6301 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6301> set name cluster666
-> Redirected to slot [5798] located at 127.0.0.1:6302
OK
127.0.0.1:6302> get name
"cluster666"
127.0.0.1:6302>
自动跳转到了端口为6302的实例上,这波操作挺666
redis-cli
可以在集群的各个节点之间自动跳转,那是人家的本事,如果是你想自己连接集群,首先看看你使用的是那个库了。
如果使用的语言是C/C++,并且用的是最常见的hiredis,那么很抱歉它本身不提供集群的便利访问,像 MGET
,MSET
这种操作多个键的命令得自己花点力气了,不过如果你考虑换一个库那么这些问题就解决了。
hiredis-vip
是第一个选择,这个名字听起来好像充了前才能用,实际上是“唯品会 一家专门做特卖的网站”,它先搞出来开源的,可以把集群看成一个整体,连接任一节点就可以操作,但是这个库有个问题,不支持认证,所以后来人在这个基础上进行了二次开发。hiredis-vip源码
hiredis-cluster
就是这个二次创作的产物,现在算是官方推荐产品了,如果你想方便的操作集群,不妨下载这个库hiredis-cluster源码试试,另外redis-plus-plus也属于官方推荐产品,有时间可以了解一下。
今天拿 hiredis-cluster
开刀,看看它怎么用,改天再研究那个 redis-plus-plus
。
/usr/local/redis-cluster-cli
,进入目录下载源码:albert@home-pc:/usr/local/redis-cluster$ sudo mkdir -p /usr/local/redis-cluster-cli
albert@home-pc:/usr/local/redis-cluster$ cd /usr/local/redis-cluster-cli
albert@home-pc:/usr/local/redis-cluster-cli$ sudo wget https://github.com/Nordix/hiredis-cluster/archive/refs/tags/0.6.0.tar.gz
--2021-06-26 22:01:15-- https://github.com/Nordix/hiredis-cluster/archive/refs/tags/0.6.0.tar.gz
Resolving github.com (github.com)... 140.82.114.3
Connecting to github.com (github.com)|140.82.114.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/Nordix/hiredis-cluster/tar.gz/refs/tags/0.6.0 [following]
--2021-06-26 22:01:16-- https://codeload.github.com/Nordix/hiredis-cluster/tar.gz/refs/tags/0.6.0
Resolving codeload.github.com (codeload.github.com)... 13.229.189.0
Connecting to codeload.github.com (codeload.github.com)|13.229.189.0|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/x-gzip]
Saving to: ‘0.6.0.tar.gz’
0.6.0.tar.gz [ <=> ] 69.58K 19.4KB/s in 3.6s
2021-06-26 22:01:21 (19.4 KB/s) - ‘0.6.0.tar.gz’ saved [71249]
albert@home-pc:/usr/local/redis-cluster-cli$ sudo tar -zxvf 0.6.0.tar.gz
hiredis-cluster-0.6.0/
hiredis-cluster-0.6.0/.clang-format
...
...
hiredis-cluster-0.6.0/tests/test_utils.h
hiredis-cluster-0.6.0/win32.h
albert@home-pc:/usr/local/redis-cluster-cli$ ls
0.6.0.tar.gz hiredis-cluster-0.6.0
albert@home-pc:/usr/local/redis-cluster-cli$ cd hiredis-cluster-0.6.0/
albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0$
albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0$ sudo mkdir build && cd build
albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0/build$ sudo cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DDISABLE_TESTS=ON ..
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.11 or higher is required. You are running version 3.5.1
-- Configuring incomplete, errors occurred!
oh no!要求cmake版本最低3.11,而我的ubuntu16.04自带的cmake是3.5.1,好吧我先去升个级
$ wget http://www.cmake.org/files/v3.11/cmake-3.11.3.tar.gz
$ tar xf cmake-3.11.3.tar.gz
$ cd cmake-3.11.3
$ ./configure
$ make
$ sudo make install
make
命令开始编译:albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0/build$ sudo cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DDISABLE_TESTS=ON ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Detected version: 0.5.0
Downloading dependency 'hiredis'..
...
albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0/build$ sudo make
Scanning dependencies of target hiredis_cluster
[ 5%] Building C object CMakeFiles/hiredis_cluster.dir/adlist.c.o
[ 11%] Building C object CMakeFiles/hiredis_cluster.dir/command.c.o
[ 17%] Building C object CMakeFiles/hiredis_cluster.dir/crc16.c.o
[ 23%] Building C object CMakeFiles/hiredis_cluster.dir/dict.c.o
[ 29%] Building C object CMakeFiles/hiredis_cluster.dir/hiarray.c.o
[ 35%] Building C object CMakeFiles/hiredis_cluster.dir/hircluster.c.o
[ 41%] Building C object CMakeFiles/hiredis_cluster.dir/hiutil.c.o
[ 47%] Linking C shared library libhiredis_cluster.so
[ 47%] Built target hiredis_cluster
Scanning dependencies of target hiredis
[ 52%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/alloc.c.o
[ 58%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/async.c.o
[ 64%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/dict.c.o
[ 70%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/hiredis.c.o
[ 76%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/net.c.o
[ 82%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/read.c.o
[ 88%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/sds.c.o
[ 94%] Building C object _deps/hiredis-build/CMakeFiles/hiredis.dir/sockcompat.c.o
[100%] Linking C shared library libhiredis.so
[100%] Built target hiredis
/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0
目录,新建 cluster-cli.cpp
文件,编写测试代码如下:#include "hircluster.h"
#include
#include
int main(int argc, char **argv) {
struct timeval timeout = {1, 500000}; // 1.5s
redisClusterContext *cc = redisClusterContextInit();
redisClusterSetOptionAddNodes(cc, "127.0.0.1:6301");
redisClusterSetOptionConnectTimeout(cc, timeout);
redisClusterSetOptionPassword(cc, "foobared");
redisClusterSetOptionRouteUseSlots(cc);
redisClusterConnect2(cc);
if (cc && cc->err) {
printf("Error: %s\n", cc->errstr);
exit(-1);
}
redisReply *reply =
(redisReply *)redisClusterCommand(cc, "SET %s %s", "name", "cluster999");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
redisReply *reply2 = (redisReply *)redisClusterCommand(cc, "GET %s", "name");
printf("GET: %s\n", reply2->str);
freeReplyObject(reply2);
redisClusterFree(cc);
return 0;
}
albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0$ sudo gcc cluster-cli.cpp -I ./build/_deps/ -L ./build/ -Wl,-rpath=./build -lhiredis_cluster -L ./build/_deps/hiredis-build/ -Wl,-rpath=./build/_deps/hiredis-build/ -lhiredis -o cluster-cli
albert@home-pc:/usr/local/redis-cluster-cli/hiredis-cluster-0.6.0$ ./cluster-cli
SET: OK
GET: cluster999
hiredis-cluster
和 cluster-plus-plus
是使用的C/C++连接redis集的官方推荐库一念动山海,一念山海平~