手动编写redis代理软件脚本 实现类似tweproxy 和codes的功能
已经实现内容如下:
起1个代理ip和端口 redisl客户端连接代理 然后脚本通过一致性hash分发key到后端的redis
脚本如下
import SocketServer,sys
from threading import Thread
import socket
from hash_ring import HashRing
redis_servers = ['127.0.0.1:6480',
'127.0.0.1:6481',
'127.0.0.1:6482']
ring = HashRing(redis_servers)
class service(SocketServer.BaseRequestHandler):
def handle(self):
data = 'dummy'
print "Client connected with ", self.client_address
while len(data):
data = self.request.recv(1024)
if data:
key =(data.split("\n"))[4]
server = ring.get_node(key)
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(((server.split(":"))[0],int((server.split(":"))[1])))
s.sendall(data)
r_data = s.recv(1024)
print server
self.request.send(r_data)
print "Client exited"
self.request.close()
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
try:
t = ThreadedTCPServer(('',6379), service)
t.serve_forever()
except KeyboardInterrupt:
sys.exit(1)
转载于:https://blog.51cto.com/kkkkkk/1760030