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!