zoukankan      html  css  js  c++  java
  • 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!
    

  • 相关阅读:
    c# 判断网络是否连接
    有关TSQL的10个好习惯
    相同文件只能一个进程读取
    我的单元测试方案
    又用了一把VBA
    深入理解字符串和字节数组转换
    如何清除应用程序承载 WebBrowser 控件时缓存
    VB也绿色
    ASP.Net网站开发的单元测试方案
    Nunit使用心得
  • 原文地址:https://www.cnblogs.com/catkins/p/5270386.html
Copyright © 2011-2022 走看看