#!/bin/bash nodes=(7000 7001 7002 7003 7004 7005) HOME_DIR=`pwd` function create(){ echo "create" if [ -d redis_cluster ];then rm -rf redis_cluster fi
mkdir redis_cluster cd redis_cluster
mkdir `echo ${nodes[*]}`
for var in ${nodes[*]} do cd $var touch $var-redis.conf echo "port" $var >> $var-redis.conf echo "cluster-enabled yes" >> $var-redis.conf echo "cluster-config-file nodes.conf" >> $var-redis.conf echo "cluster-node-timeout 5000" >> $var-redis.conf echo "appendonly yes" >> $var-redis.conf echo "daemonize yes" >> $var-redis.conf echo "pidfile redis.pid" >> $var-redis.conf cd ../ done run; 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
}
function run(){ echo run for var in ${nodes[*]} do cd $HOME_DIR/redis_cluster/$var/ redis-server $var-redis.conf cd $HOME_DIR/ echo Start redis ports ${var} finish. done } function stop(){ cd $HOME_DIR/redis_cluster/ DIRS=`ls -l | grep "^d" | awk '{print $NF}'` for d in $DIRS do PID=`cat $d/redis.pid` if [ -n "$PID" ];then kill -9 $PID rm -f $d/redis.pid fi done cd $HOME_DIR/ echo Stop redis ports ${DIRS} finish. }
case $1 in create) echo " create redis cluster 锛?000,7001,7002,7003,7004.7005,7006,7007" create ;; run) run ;; stop) stop ;; *) echo "Usage:[create|run|stop]" ;; esac
执行
1
sh createCluster.sh create
一路回车即可,注意构建集群需要使用redis-trib.rb,需要额外执行
1
gem install redis
结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[bigdata@trumanlab1 redis-4.0.0]$ redis-cli -c -h 127.0.0.1 -p 7000 127.0.0.1:7000> cluster nodes 63e698210378f4fda23c4070bea3b46f93952811 127.0.0.1:7000@17000 myself,master - 0 1500391716000 1 connected 0-5460 4091630217b64ece9a6fa55c26687a10c1f9a8b5 127.0.0.1:7001@17001 master - 0 1500391717501 2 connected 5461-10922 0b23853c3aa4f5b942a6239bc7bca71df36e121c 127.0.0.1:7005@17005 slave 12d05fb7c20bf001759f80cf3f65ad9df4b01af5 0 1500391716498 6 connected 12d05fb7c20bf001759f80cf3f65ad9df4b01af5 127.0.0.1:7002@17002 master - 0 1500391717000 3 connected 10923-16383 2e559b462362a9563f98b398af3984c05e25ef19 127.0.0.1:7003@17003 slave 63e698210378f4fda23c4070bea3b46f93952811 0 1500391716000 4 connected c4f8e0a95579c772c7506c195b3c7984c73312b4 127.0.0.1:7004@17004 slave 4091630217b64ece9a6fa55c26687a10c1f9a8b5 0 1500391716000 5 connected 127.0.0.1:7000> set a a -> Redirected to slot [15495] located at 127.0.0.1:7002 OK 127.0.0.1:7002> get a "a" 127.0.0.1:7002>
4. 特性尝鲜
4.1 注意事项
IMPORTANT: Redis Cluster users, please note that, as specified in the list of incompatibilities, Redis 4.0 cluster bus protocol is not compatible with Redis 3.2, so in order to upgrade, a mass reboot of the instances is needed and rolling upgrades are not possible. This change was needed in order to add compatibility for Containers/NAT, where the bus port at a fixed offset was not an acceptable design, so we had to change many things, resulting in the incompatible protocol.
Different replication fixes to PSYNC2, the new 4.0 replication engine.
Modules thread safe contexts were introduced. They are an >experimental API right now, but the API is considered to be stable and usable when needed.
SLOWLOG now logs the offending client name and address. Note that this is a backward compatibility breakage in case old code assumes that the slowlog entry is composed of exactly three entries.
The modules native data types RDB format changed.
The AOF check utility is now able to deal with RDB preambles.
GEORADIUS_RO and GEORADIUSBYMEMBER_RO variants, not supporting the STORE option, were added in order to allow read-only scaling of such queries.
HSET is now variadic, and HMSET is considered deprecated (but will be supported for years to come). Please use HSET in new code.
GEORADIUS huge radius (>= ~6000 km) corner cases fixed, certain elements near the edges were not returned.
DEBUG DIGEST modules API added.
HyperLogLog commands no longer crash on certain input (non HLL) strings.
# Load modules at startup. If the server is not able to load modules # it will abort. It is possible to use multiple loadmodule directives. # # loadmodule /path/to/my_module.so # loadmodule /path/to/other_module.so
When rewriting the AOF file, Redis is able to use an RDB preamble in the # AOF file for faster rewrites and recoveries. When this option is turned # on the rewritten AOF file is composed of two different stanzas: # # [RDB file][AOF tail] # # When loading Redis recognizes that the AOF file starts with the "REDIS" # string and loads the prefixed RDB file, and continues loading the AOF # tail. # # This is currently turned off by default in order to avoid the surprise # of a format change, but will at some point be used as the default. aof-use-rdb-preamble no
4.9 内存命令
新添加了一个 MEMORY 命令, 这个命令可以用于视察内存使用情况, 并进行相应的内存管理操作:
1 2 3 4 5 6
127.0.0.1:7002> memory help 1) "MEMORY USAGE <key> [SAMPLES <count>] - Estimate memory usage of key" 2) "MEMORY STATS - Show memory usage details" 3) "MEMORY PURGE - Ask the allocator to release memory" 4) "MEMORY MALLOC-STATS - Show allocator internal stats"
其中, 使用 MEMORY USAGE 子命令可以估算储存给定键所需的内存:
1 2 3 4 5 6
127.0.0.1:7002> set truman aibibang -> Redirected to slot [2113] located at 127.0.0.1:7000 OK 127.0.0.1:7000> memory usage truman (integer) 59
########################## CLUSTER DOCKER/NAT support ######################## # In certain deployments, Redis Cluster nodes address discovery fails, because # addresses are NAT-ted or because ports are forwarded (the typical case is # Docker and other containers). # # In order to make Redis Cluster working in such environments, a static # configuration where each node known its public address is needed. The # following two options are used for this scope, and are: # # * cluster-announce-ip # * cluster-announce-port # * cluster-announce-bus-port # # Each instruct the node about its address, client port, and cluster message # bus port. The information is then published in the header of the bus packets # so that other nodes will be able to correctly map the address of the node # publishing the information. # # If the above options are not used, the normal Redis Cluster auto-detection # will be used instead. # # Note that when remapped, the bus port may not be at the fixed offset of # clients port + 10000, so you can specify any port and bus-port depending # on how they get remapped. If the bus-port is not set, a fixed offset of # 10000 will be used as usually. # # Example: # # cluster-announce-ip 10.1.1.5 # cluster-announce-port 6379 # cluster-announce-bus-port 6380