Redis基础和集群

来源:互联网 发布:测试淘宝标题 编辑:程序博客网 时间:2024/05/29 18:27
redis
make
make distclean
./redis-server
./redis-server /path/to/redis.conf
./redis-server --port 9999 --slaveof 127.0.0.1 6379
./redis-server /etc/redis/6379.conf --loglevel debug
./redis-cli


set
get
incr
del
repire
ttl
wait:synchronous
list RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP
set SADD, SREM, SISMEMBER, SMEMBERS and SUNION
sorted set ZADD ZRANGE
Hashes hset hgetall hget hincrby hdel
cluster nodes
cluster info


serve port 6379
data port 16379
offset 10000
16384(2^14) hash slots
Hash tag
master-slave model


1.
redis.conf
cluster-enabled <yes/no>
cluster-config-file <filename>
cluster-node-timeout <milliseconds>
cluster-slave-validity-factor <factor>
cluster-migration-barrier <count>
cluster-require-full-coverage <yes/no>


port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes


mkdir cluster-test
cd cluster-test
mkdir 7001 7002 7003 7004 7005 7006


cd 7001
../redis-server ./redis.conf
...


Node ID


gem install redis


./redis-trib.rb create --replicas 1 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 127.0.0.1:7006


2.
utils/create-cluster
To create a cluster, follow this steps:


1. Edit create-cluster and change the start / end port, depending on the
number of instances you want to create.
2. Use "./create-cluster start" in order to run the instances.
3. Use "./create-cluster create" in order to execute redis-trib create, so that
an actual Redis cluster will be created.
4. Now you are ready to play with the cluster. AOF files and logs for each instances are created in the current directory.


In order to stop a cluster:


1. Use "./craete-cluster stop" to stop all the instances. After you stopped the instances you can use "./create-cluster start" to restart them if you change ideas.
2. Use "./create-cluster clean" to remove all the AOF / log files to restat with a clean environment.


Use the command "./create-cluster help" to get the full list of features.


create-cluster start
create-cluster create
the first node will start at port 30001 by default
create-cluster stop


redis
https://pypi.python.org/pypi/redis/2.10.5
import redis
redis.Redis(self, host='localhost', port=6379, db=0, password=None, socket_timeout=None, socket_connect_timeout=None, socket_keepalive=None, 
socket_keepalive_options=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', encoding_errors='strict', charset=None, errors=None, decode_responses=False, retry_on_timeout=False, ssl=False, ssl_keyfile=None, ssl_certfile=None, ssl_cert_reqs=None, ssl_ca_certs=None, max_connections=None)


r.set('guo','shuai')
r.get('guo')
r['guo']
r.keys()
r.dbsize()
r.delete('guo')
r.save()
r.flushdb()
r.info()


pipeline


pyredis
http://pyredis.readthedocs.org/en/latest/


redis-py-cluster
https://pypi.python.org/pypi/redis-py-cluster/1.1.0
from rediscluster import StrictRedisCluster
startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
# Note: decode_responses must be set to True when used with python3
rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
rc.set("foo", "bar")
print(rc.get("foo"))


Adding a new node
./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7001
Adding a new node as a replica
./redis-trib.rb add-node --slave 127.0.0.1:7008 127.0.0.1:7001
./redis-trib.rb add-node --slave --master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 127.0.0.1:7007 127.0.0.1:7001
Removing a node
./redis-trib del-node 127.0.0.1:7000 `<node-id>`


Resharding the cluster
./redis-trib.rb reshard 127.0.0.1:7000
./redis-trib.rb check 127.0.0.1:7000
./redis-trib.rb reshard --from <node-id> --to <node-id> --slots <number of slots> --yes <host>:<port>


CLUSTER REPLICATE <master-node-id>





0 0