標籤:路徑 disable pen get list 支援 environ eth 重啟
日常寫代碼過程中,經常需要串連redis進行操作。下面我就介紹下python串連redis的兩種方法
方法一、使用host 和 port 串連
請看代碼:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import redis # 引入python的redis庫4 5 r = redis.StrictRedis(host="192.168.163.229", port=6379) # 建立StrictRedis對象6 r.lpush("city", "shanghai") # 開始操作redis(利用lpush方法向city中加入"shanghai")
代碼解析:
line 3 :引入redis庫。如果未安裝redis庫,請先安裝該庫。安裝方法這裡就不贅述了。
line 5: 建立StricRedis對象,傳入的參數是host 和port,分別為redis主機的ip和port。 當然也可以建立Redis對象。redis庫中Redis類和StricRedis類都可以操作redis,只不過兩者有區別,區別這裡不作介紹
line 6: 利用StricRedis中的方法操作redis,非常方便
方法二、使用unix socket串連redis
請看代碼:
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 import redis # 引入python的redis庫4 5 r = redis.StrictRedis(unix_socket_path="/tmp/redis.socket") # 建立StrictRedis對象6 r.lpush("city", "hangzhou") # 開始操作redis(利用lpush方法向city中加入"hangzhou")
代碼解析:除了建立對象時的參數不一樣,其他一樣。
關於redis socket串連。
預設情況下,當安裝好了redis,並且以預設的設定檔啟動,則redis的串連方式只有host:port,那麼如何配置redis的socket串連方式呢?
修改redis.conf檔案
1 TCP listen() backlog. 2 # 3 # In high requests-per-second environments you need an high backlog in order 4 # to avoid slow clients connections issues. Note that the Linux kernel 5 # will silently truncate it to the value of /proc/sys/net/core/somaxconn so 6 # make sure to raise both the value of somaxconn and tcp_max_syn_backlog 7 # in order to get the desired effect. 8 tcp-backlog 511 9 10 # Unix socket.11 #12 # Specify the path for the Unix socket that will be used to listen for13 # incoming connections. There is no default, so Redis will not listen14 # on a unix socket when not specified.15 #16 unixsocket /tmp/redis.sock17 unixsocketperm 70018 19 # Close the connection after a client is idle for N seconds (0 to disable)20 timeout 021 22 # TCP keepalive.23 #24 # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence25 # of communication. This is useful for two reasons:26 #27 # 1) Detect dead peers.28 # 2) Take the connection alive from the point of view of network29 # equipment in the middle.30 #31 # On Linux, the specified value (in seconds) is the period used to send ACKs.32 # Note that to close the connection the double of the time is needed.33 # On other kernels the period depends on the kernel configuration.
其中修改的項是
unixsocket /tmp/redis.sockunixsocketperm 700
其中unixsocket 後面的目錄為sock檔案的絕對路徑。
修改完配置,重啟redis,就可以使用socket方式進行串連了。linux命令列可以使用redsi-cli -s /tmp/redis.sock 進行socket方式的redis串連。
一般生產環境,redis是使用代理進行串連的。因此如何配置twemproxy代理的socket呢?別急,聽我慢慢道來
在需要串連redis的機器安裝tw代理。如何安裝twemproxy?傳送門
安裝完成之後進行配置。
cd /usr/local/twemproxy # 進入twemproxy目錄mkdir conf # 建立conf目錄,預設沒有該目錄cd confvim ethan.ymlethan: listen: /tmp/ethan.socket 0666 hash: fnv1a_64 hash_tag: "{}" distribution: ketama auto_eject_hosts: false timeout: 400 redis: true servers: - 192.168.163.229:6379:1
其中ethan 為自訂的名稱,只要全域唯一即可。
listen 為tw偵聽的socket檔案。
hash: hash函數,支援md5,crc16,crc32,finv1a_32等十多種;
timeout: The timeout value in msec that we wait for to establish a connection to the server or receive a response from a server. By default, we wait indefinitely.
即逾時時間。
servers 可以定義多個redis伺服器,最後面的數字表示負載平衡的權重。
配置完成之後,啟動twemproxy:
/usr/local/twemproxy/sbin/nutcracker -c /usr/local/twemproxy/conf/ethan.yml &
如果沒有報錯,證明socket已經配置完成。
此時就可以通過檔案/tmp/ethan.socket串連redis了。
python串連redis的兩種方法