Esempio n. 1
0
int DbProxy::updateRegistryInfo2Db(bool bRegHeartbeatOff)
{
    if (bRegHeartbeatOff)
    {
        TLOGDEBUG("updateRegistryInfo2Db not need to update reigstry status !" << endl);
        return 0;
    }

    map<string, string>::iterator iter;
    map<string, string> mapServantEndpoint = g_app.getServantEndpoint();
    if (mapServantEndpoint.size() == 0)
    {
        TLOGERROR("fatal error, get registry servant failed!" << endl);
        return -1;
    }

    try
    {
        string sql = "replace into t_registry_info (locator_id, servant, endpoint, last_heartbeat, present_state, tars_version) "
                      "values ";

        string sVersion = TARS_VERSION;
        sVersion += "_";
        sVersion += SERVER_VERSION;

        for (iter = mapServantEndpoint.begin(); iter != mapServantEndpoint.end(); iter++)
        {
            TC_Endpoint locator;
            locator.parse(iter->second);

            sql += (iter == mapServantEndpoint.begin() ? string("") : string(", ")) +
                    "('" + locator.getHost() + ":" + TC_Common::tostr<int>(locator.getPort()) + "', "
                    "'" + iter->first + "', '" + iter->second + "', now(), 'active', " +
                    "'" + _mysqlReg.escapeString(sVersion) + "')";
        }

        _mysqlReg.execute(sql);
        TLOGDEBUG(__FUNCTION__ << " affected:" << _mysqlReg.getAffectedRows() << endl);
    }
    catch (TC_Mysql_Exception& ex)
    {
        TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl);
        return -1;
    }
    catch (exception& ex)
    {
        TLOGERROR(__FUNCTION__ << " exception: " << ex.what() << endl);
        return -1;
    }

    return 0;
}
Esempio n. 2
0
/*
服务端路由
*/
int Servant::doGridRouter(JceCurrentPtr current, string & routekey)
{
    try
    {
        LOGINFO("[TAF] Servant::doGridRouter got a grid req, key:" << routekey << endl);

        TC_Endpoint point   = current->getBindAdapter()->getEndpoint();
        //当前服务是否是路由服务
        int32_t serverGrid = point.getGrid();

        //如果业务设置了路由器,使用路由器
        int32_t grid = ServantHelperManager::getInstance()->getGridByKey(current->getServantName(),routekey);

        if(grid == ThreadPrivateData::INVALID_GRID_CODE || grid == serverGrid)
        {
             return -1;
        }

        //检测是否来自自己(服务端adapter与通信器维护的grid是否相同  防止不一致导致灰度死循环)
        if(current->getIp() == point.getHost() && current->getPort() == point.getPort())
        {
            return  -1;
        }

        ServantProxyThreadData* sptd = ServantProxyThreadData::getData();

        if (sptd)
        {
            sptd->data()->_routeKey = routekey;

            sptd->data()->_gridCode = grid;
        }

        ServantPrx pPrx = Application::getCommunicator()->stringToProxy<ServantPrx>(current->getServantName());

        if(current->getPacketType() == JCENORMAL)
        {
            ResponsePacket rep;
            map<string,string> status = current->getRequestStatus();
            status.insert(std::make_pair(ServantProxy::STATUS_GRID_KEY, routekey));

            pPrx->taf_invoke(current->getPacketType(),current->getFuncName(),current->getRequestBuffer(),
                             current->getContext(),status,rep);
            current->sendResponse(rep.iRet, rep.sBuffer,rep.status,rep.sResultDesc);
            current->setResponse(false);
        }
        else
        {
            ServantCallbackPtr p = NULL;
            if (current->getMessageType() & JCEMESSAGETYPEASYNC)
            {
                p = new RouterCallback("RouterCallback",this,current);
            }
            pPrx->taf_invoke_async(current->getPacketType(),current->getFuncName(),current->getRequestBuffer(),
                             current->getContext(),current->getRequestStatus(),p);
            current->setResponse(false);
        }
        return 0;
    }
    catch(exception &ex)
    {
        LOG->error() << "[TAF]Servant::doGridRouter " << ex.what() << endl;
    }
    catch(...)
    {
        LOG->error() << "[TAF]Servant::doGridRouter unknown error" << endl;
    }
    return -1;
}