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

  • 相关阅读:
    微信平台的开发与集成
    自定义控件定义样式
    Android万能分辨率适应法
    Openfire配置过程,以及与php交互注意事项。
    Android 最近的一些新的功能
    自定义Ratingbar 评分控件
    解决android有的手机拍照后上传图片被旋转的问题
    Fragment 嵌套使用 Activity has been destoryed
    实现图文混排方法 类似于网易那样的
    Android 自定义View及其在布局文件中的使用示例
  • 原文地址:https://www.cnblogs.com/catkins/p/5270386.html
Copyright © 2020-2023  润新知