ice調通過iceReplica用所有server instance的方法---客戶端控制服務端的負載均衡

I

  • 使用此方法,可以增量的通知Ice服務配置的改變,刷新每個服務進程的數據
  • 可以手動控制客戶端調用的負載均衡,客戶端程序決定將請求發往那個進程

上代碼:

import logging
import Ice,IceGrid
import time
from company.service import Handler, HandlerPrx


REC_LOCATOR = "****************************"
ICE_LOCATOR_CACH_TIMEOUT = "300"


def init_ice():
    global __rec, ice, base, communicator, rec_replicas
    p = Ice.createProperties()
    p.setProperty("Ice.Default.Locator", REC_LOCATOR)
    p.setProperty("Ice.Default.LocatorCacheTimeout", ICE_LOCATOR_CACH_TIMEOUT)
    p.setProperty("Ice.BackgroundLocatorCacheUpdates", "1")
    id = Ice.InitializationData()
    id.properties = p 

    ic = Ice.initialize(id)
    base = ic.stringToProxy("*********/Query")
    q = IceGrid.QueryPrx.checkedCast(base)

    # ICE initialize
    while(1):
        communicator = Ice.initialize(id)
        #logging.warn("-------------initing ICE------------------")
        try:
            base = communicator.stringToProxy("recommend")
            __rec = HandlerPrx.uncheckedCast(base)
            print "replicated proxy: ", __rec
            rec_replicas = q.findAllReplicas(__rec)
            print rec_replicas
            if not __rec:
                raise RuntimeError("Invalid Ice proxy")
            logging.warn("-------init completed, proxy is %s--------"%__rec)
            break
        except Exception, e:
            logging.error("CAN'T CONNECT TO ice proxy, retrying...%s", str(e))
            try:
                communicator.destroy()
            except:
                pass
            base = None
            __rec = None
            time.sleep(1)
            continue

if __name__ == "__main__":
    init_ice()
    print "normal call", __rec.getResponse("adsfadsf")
    for rr in rec_replicas:
        adapter_id = rr.ice_getAdapterId()
        print "==================", adapter_id
        rrt = HandlerPrx.checkedCast(rr)
        print "each call", rrt, rrt.getResponse("xxx")
  • 執行之後,服務端接受到的log如下,可以看出每個進程分別執行了一次(開啓了4個進程)
[INFO  2012-12-19 16:27:50,327 @ 435028] - Request: xxx
[WARN  2012-12-19 16:27:50,327 @ 435028] - parse input json error!
[INFO  2012-12-19 16:27:50,328  @ 435035] - Request: xxx
[WARN  2012-12-19 16:27:50,328 @ 435035] - parse input json error!
[INFO  2012-12-19 16:27:50,330  @ 435045] - Request: xxx
[WARN  2012-12-19 16:27:50,330  @ 435045] - parse input json error!
[INFO  2012-12-19 16:27:50,331  @ 435050] - Request: xxx
[WARN  2012-12-19 16:27:50,331  @ 435050] - parse input json error!

發佈了148 篇原創文章 · 獲贊 42 · 訪問量 195萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章