I recently learned to use redis, installed a centos-7 virtual machine, referenced many blog posts on the Internet, and then cobbled together a little stuff to share with you.
Install redis 3.0
1. First download redis 3.0
- ·wget http://download.redis.io/releases/redis-3.0.0.tar.gz
- tar zxvf redis-3.0.0.tar.gz
- cd redis-3.0.0
2. Compile
- make
After the make command is executed and compiled, six executable files will be generated in the src directory, namely redis-server, redis-cli, redis-benchmark, redis-check-aof, redis-check-dump, and redis-sentinel.
At this point, you can actually start redis in the src directory:
3. Start the redis service in the background
- ./redis-server &
If the configuration is not modified, you will see the following status:
Then start the redis client:
- ./redis-cli
The above interface appears, indicating that you have successfully entered redis and can be tested:
The above is that we run redis after compiling, but usually, we install redis after compiling, so that we can use redis commands in all paths:
- make install
After installation, the executable file compiled by make will be copied to the /usr/local/bin directory, so that redis-server can be executed anywhere to start the redis service.
Note:
▲The default port number of redis is 6379, (according to the blog post of antirez, the author of redis, 6379 is the number corresponding to MERZ on the mobile phone button, and MERZ is taken from the name of Italian singer Alessia Merz. MERZ has long been used by antirez and Its friend is regarded as a synonym for stupidity.)
▲Redis has two storage methods, the default is the snapshot method, the implementation method is to periodically persist the memory snapshot (snapshot) to the hard disk, the disadvantage of this method is that if a crash occurs after persistence A piece of data will be lost. Therefore, under the impetus of perfectionists, the author added the aof method. aof is the append only mode, which saves the operation command to the log file while writing the memory data.
Running Redis as a background daemon
needs to start by reading the configuration file.
Note that the default daemonize parameter of the redis.conf file copied in the past is no, so redis will not run in the background. We can modify the redis.conf file, which is the decompressed file. under the redis root directory
- daemonize yes
- redis-server /usr/redis-3.0.0/redis.conf
View the redis process
- ps to | grep redis
redis cluster cluster construction
Establish a local test environment
(at least 3 master nodes are required for the cluster to work properly, here we need to create 6 redis nodes, three of which are master nodes, three are slave nodes, and the ip and port of the corresponding redis nodes correspond to The relationship is as follows)
- 127.0.0.1:7000
- 127.0.0.1:7001
- 127.0.0.1:7002
- 127.0.0.1:7003
- 127.0.0.1:7004
- 127.0.0.1:7005
Create the directory required by the cluster
- mkdir -p /usr/local/cluster
- cd /usr/local/cluster
- mkdir 7000 7001 7002 7003 7004 7005
Modify the configuration file redis.conf
- cp /usr/local/redis3.0.0/redis.conf /usr/local/cluster
- vi redis.conf
Modify the following options in the configuration file
- port 7000
- daemonize yes
- cluster-enabled yes
- cluster-config-file nodes.conf
- cluster-node-timeout 5000
- appendonly yes
After modifying the configuration items in the redis.conf configuration file, copy the configuration files to the 7000/ 7001/ 7002/ 7003/ 7004/ 7005 directories respectively
- cp /usr/local/cluster/redis.conf /usr/local/cluster/7000
- cp /usr/local/cluster/redis.conf /usr/local/cluster/7001
- cp /usr/local/cluster/redis.conf /usr/local/cluster/7002
- cp /usr/local/cluster/redis.conf /usr/local/cluster/7003
- cp /usr/local/cluster/redis.conf /usr/local/cluster/7004
- cp /usr/local/cluster/redis.conf /usr/local/cluster/7005
Note : After the copy is completed, you need to modify the port parameter in the redis.conf file under the 7001/7002/7003/7004/7005 directory, and change it to the name of the corresponding folder.
Start these 6 redis instances respectively
- cd /usr/local/cluster/7000
- redis-server redis.conf
- cd /usr/local/cluster/7001
- redis-server redis.conf
- cd /usr/local/cluster/7002
- redis-server redis.conf
- cd /usr/local/cluster/7003
- redis-server redis.conf
- cd /usr/local/cluster/7004
- redis-server redis.conf
- cd /usr/local/cluster/7005
- redis-server redis.conf
After startup, use the command ps aux|grep redis to check the startup status of redis. If the following result appears, the startup is successful (note: you must enter each directory, and then execute redis-server redis.conf).
Execute the redis create cluster command to create a cluster
- cd /usr/local/redis3.0/src
- ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境。错误内容:/usr/bin/env: ruby: No such file or directory
所以需要安装ruby的环境,这里推荐使用yum install ruby安装
- yum install ruby
然后再执行刚才的创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装
错误内容:
./redis-trib.rb:24:in `require’: no such file to load – rubygems (LoadError)
from ./redis-trib.rb:24
- yum install rubygems
再次执行第6步的命令,还会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装
错误内容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require': no such file to load -- redis (LoadError) from
require’
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
from ./redis-trib.rb:25
- gem install redis
出现错误:
ERROR: Could not find a valid gem ‘redis’ (>= 0), here is why:
Unable to download data from https://rubygems.org/ - Errno::ECONNRESET: Connection reset by peer - SSL_connect
(https://rubygems.org/latest_specs.4.8.gz)
解决方案:
可以求助万能的淘宝镜像站 https://ruby.taobao.org/
- gem sources -a https://ruby.taobao.org/
- #然后在执行:
- gem install redis
再次执行刚才的创建集群的命令,正常执行。
至此redis集群即搭建成功!
使用redis-cli命令进入集群环境
- redis-cli -c -h 127.0.0.1 -p 7000
- #用 cluster nodes 可以查看节点信息,
redis-trib默认用前3个实例作为Master,后3个作为Slave。
集群重启:
目前redis-trib的功能还比较弱,需要重启集群的话先手动kill掉各个进程,删除掉所有redis instance的aof、node-config、rdb文件(这样,之前的数据会丢失),然后重新启动就可以了,如果不删除原实例的文件,重启的时候会报错。
- ps -ef | grep redis | awk '{print $2}' | xargs kill
故障转移
在高可用性方面,Redis可算是能够”Auto”一把了!Redis Cluster重用了Sentinel的代码逻辑,不需要单独启动一个Sentinel集群,Redis Cluster本身就能自动进行Master选举和Failover切换。
执行:set name “zhangsan”,发现name被存到了 7000 节点上了。
下面我们故意kill掉7000结点,之后可以看到结点状态变成了fail,而Slave 7003被选举为新的Master。
尝试查询之前保存在7000上的Key “name”,可以看到7003顶替上来继续提供服务,整个集群没有受到影响。
性能检测:
redis 自己提供了一个性能测试工具redis-benchmark. redis-benchmark可以模拟N个机器,同时发送M个请求。
用法:
- redis-benchmark [-h ] [-p ] [-c ] [-n <requests]> [-k ]
- -h <hostname> Server hostname (default 127.0.0.1)
- -p <port> Server port (default 6379)
- -s <socket> Server socket (overrides host and port)
- -c <clients> Number of parallel connections (default 50) 并发客户端数
- -n <requests> Total number of requests (default 10000) 请求数量
- -d <size> Data size of SET/GET value in bytes (default 2) set 数据大小
- -k <boolean> 1=keep alive 0=reconnect (default 1) 是否采用keep alive模式
- -r <keyspacelen> Use random keys for SET/GET/INCR, random values for SADD
- Using this option the benchmark will get/set keys
- in the form mykey_rand:000000012456 instead of constant
- keys, the <keyspacelen> argument determines the max
- number of values for the random number. For instance
- if set to 10 only rand:000000000000 - rand:000000000009
- range will be allowed.
- -P <numreq> Pipeline <numreq> requests. Default 1 (no pipeline). 是否采用Pipeline模式请求,默认不采用
- -q Quiet. Just show query/sec values 仅仅显示查询时间
- --csv Output in CSV format 导出为CSV格式
- -l Loop. Run the tests forever 循环测试
- -t <tests> Only run the comma separated list of tests. The test
- names are the same as the ones produced as output.
- -I Idle mode. Just open N idle connections and wait
After testing, my notebook can handle more than 30,000 requests per second, and the performance is not bad.