Redis多实例重启脚本
redis属于单进程的服务,它主要受内存、CPU、磁盘IO(主要是做持久化),如果服务器配置比较高,多核CPU、高内存的服务器,可以考虑做redis多实例。做多实例之前,首先要考虑CPU和内存的利用,我在测试的时候发现,redis在QPS为6-8W左右的时候,这个redis所在的逻辑CPU核的负载就在100%左右,所以要优化CPU使用这块,目前一般是是做网卡软中断来实现平衡这种单进程使用CPU过高的情况,不过需要网卡支持网卡软中断,效果不错。
多实例redis的管理,涉及到监控、服务的管理等,这里只说redis多实例的重启。以下是多实例的重启脚本,仅供交流参考:
#!/usr/bin/env Python
# -*- coding: utf-8 -*-
import redis,threading,sys,socket,time,os
from multiprocessing import Pool
def port_check(host,port):
POOL = redis.ConnectionPool(host=host, port=port)
my_server = redis.Redis(connection_pool=POOL)
try:
a = my_server.ping()
if a:
return 1
else:
return 0
except Exception,e:
return 0
def shutdown_server(host,port):
flag = port_check(host,port)
if flag == 1:
POOL = redis.ConnectionPool(host=host, port=port)
my_server = redis.Redis(connection_pool=POOL)
my_server.shutdown()
while 1:
flag = port_check(host,port)
if flag == 0:
print "%s:%s is Shutdown OK" % (host,port)
break
else:
time.sleep(1)
else:
print "%s:%s is Shutdown alreaday" % (host,port)
def start_server(host,port):
flag = port_check(host,port)
if flag == 0:
start_conm = "/usr/local/bin/redis-server /usr/local/redis/etc/redis_%s.conf" % port
os.popen(start_conm)
time.sleep(3)
i = 0
while 1:#每5s检测一次redis ping,持续一分钟,1分钟没有起来认为启动失败
flag = port_check(host,port)
if flag == 1:
print "%s:%s is Start OK" % (host,port)
break
else:
if i > 12:
print "%s:%s is Start Error" % (host,port)
break
else:
time.sleep(5)
i = i + 1
else:
print "%s:%s is Start alreaday" % (host,port)
def restart(host,port):
shutdown_server(host,port)
start_server(host,port)
def main():
server_list = ["127.0.0.1:6379","127.0.0.1:16379"]
pool = Pool(processes=3)
for i in server_list:
aa = i.split(':')
host = aa[0]
port = int(aa[1])
results = pool.apply_async(restart, (host,port))
pool.close()
pool.join()
if results.successful():
print 'successful'
if __name__=="__main__":
main()
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里
推荐阅读: