• 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!
    

  • 相关阅读:
    AntSword 中国蚁剑的下载安装配置(附下载文件)
    开园第一笔
    四舍五入小技巧
    PAT B# 1025 反转链表
    WebService如何根据对方提供的xml生成对象
    解决Web部署 svg/woff/woff2字体 404错误
    解决TryUpdateModel对象为空的问题
    IIS集成模式下,URL重写后获取不到Session值
    SQLServer清空数据库中所有的表并且ID自动归0
    win2003 64位系统IIS6.0 32位与64位间切换
  • 原文地址:https://www.cnblogs.com/catkins/p/5270386.html
Copyright © 2020-2023  润新知