Redis多執行個體重啟指令碼

來源:互聯網
上載者:User

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 的:請點這裡

推薦閱讀:

Redis叢集明細文檔

Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis串連Redis

Redis系列-安裝部署維護篇

CentOS 6.3安裝Redis

Redis設定檔redis.conf 詳解

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.