zoukankan      html  css  js  c++  java
  • python 操作redis集群

    上代码

     
    1、对redis的单实例进行连接操作
    
    python3
    >>>import redis
    >>>r = redis.StrictRedis(host='localhost', port=6379, db=0,password='root')
    >>>r.set('lufei', 'guojialei')
    True
    >>>r.get('lufei')
    'bar'
    
    
    --------------------
    
    2、sentinel集群连接并操作
    
    
    [root@db01 ~]# redis-server /data/6380/redis.conf
    [root@db01 ~]# redis-server /data/6381/redis.conf
    [root@db01 ~]# redis-server /data/6382/redis.conf 
    [root@db01 ~]# redis-sentinel /data/26380/sentinel.conf &
    
    
    
    --------------------------------
    ## 导入redis sentinel包
    >>> from redis.sentinel import Sentinel  
    ##指定sentinel的地址和端口号
    >>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1)  
    ##测试,获取以下主库和从库的信息
    >>> sentinel.discover_master('mymaster')  
    >>> sentinel.discover_slaves('mymaster')  
    ##配置读写分离
    #写节点
    >>> master = sentinel.master_for('mymaster', socket_timeout=0.1)  
    #读节点
    >>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)  
    ###读写分离测试   key     
    >>> master.set('oldboy', '123')  
    >>> slave.get('oldboy')  
    '123'
    
    
    ----------------------
    redis cluster的连接并操作(python2.7.2以上版本才支持redis cluster,我们选择的是3.5)
    https://github.com/Grokzen/redis-py-cluster
    
    
    3、python连接rediscluster集群测试
    使用
    
    python3
    >>> 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")  
    True  
    >>>   
    'bar'
    ----------------------
     
  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/lingcai/p/10447297.html
Copyright © 2011-2022 走看看