基於protobuf的RPC實現

可以對照使用google protobuf RPC實現echo service一文看,細節本文不再描述。

google protobuf只負責消息的打包和解包,並不包含RPC的實現,但其包含了RPC的定義。假設有下面的RPC定義:

service MyService {
        rpc Echo(EchoReqMsg) returns(EchoRespMsg) 
    }

那麼要實現這個RPC需要最少做哪些事?總結起來需要完成以下幾步:

客戶端

RPC客戶端需要實現google::protobuf::RpcChannel。主要實現RpcChannel::CallMethod接口。客戶端調用任何一個RPC接口,最終都是調用到CallMethod。這個函數的典型實現就是將RPC調用參數序列化,然後投遞給網絡模塊進行發送。

void CallMethod(const ::google::protobuf::MethodDescriptor* method,
                  ::google::protobuf::RpcController* controller,
                  const ::google::protobuf::Message* request,
                  ::google::protobuf::Message* response,
                  ::google::protobuf::Closure* done) {
        ...
        DataBufferOutputStream outputStream(...) // 取決於你使用的網絡實現
        request->SerializeToZeroCopyStream(&outputStream);
        _connection->postData(outputStream.getData(), ...
        ...
    }

服務端

服務端首先需要實現RPC接口,直接實現MyService中定義的接口:

class MyServiceImpl : public MyService {
        virtual void Echo(::google::protobuf::RpcController* controller,
            const EchoReqMsg* request,
            EchoRespMsg* response,
            ::google::protobuf::Closure* done) {
            ...
            done->Run();
        }
    }

標示service&method

基於以上,可以看出服務端根本不知道客戶端想要調用哪一個RPC接口。從服務器接收到網絡消息,到調用到MyServiceImpl::Echo還有很大一段距離。

解決方法就是在網絡消息中帶上RPC接口標識。這個標識可以直接帶上service name和method name,但這種實現導致網絡消息太大。另一種實現是基於service name和method name生成一個哈希值,因爲接口不會太多,所以較容易找到基本不衝突的字符串哈希算法。

無論哪種方法,服務器是肯定需要建立RPC接口標識到protobuf service對象的映射的。

這裏提供第三種方法:基於option的方法。

protobuf中option機制類似於這樣一種機制:service&method被視爲一個對象,其有很多屬性,屬性包含內置的,以及用戶擴展的。用戶擴展的就是option。每一個屬性有一個值。protobuf提供訪問service&method這些屬性的接口。

首先擴展service&method的屬性,以下定義這些屬性的key:

extend google.protobuf.ServiceOptions {
      required uint32 global_service_id = 1000; 
    }
    extend google.protobuf.MethodOptions {
      required uint32 local_method_id = 1000;
    }

應用層定義service&method時可以指定以上key的值:

service MyService
    {
        option (arpc.global_service_id) = 2302; 

        rpc Echo(EchoReqMsg) returns(EchoRespMsg) 
        {
            option (arpc.local_method_id) = 1;
        }
        rpc Echo_2(EchoReqMsg) returns(EchoRespMsg) 
        {
            option (arpc.local_method_id) = 2;
        }
        ...
    }

以上相當於在整個應用中,每個service都被賦予了唯一的id,單個service中的method也有唯一的id。

然後可以通過protobuf取出以上屬性值:

void CallMethod(const ::google::protobuf::MethodDescriptor* method,
                  ::google::protobuf::RpcController* controller,
                  const ::google::protobuf::Message* request,
                  ::google::protobuf::Message* response,
                  ::google::protobuf::Closure* done) {
        ...
        google::protobuf::ServiceDescriptor *service = method->service();
        uint32_t serviceId = (uint32_t)(service->options().GetExtension(global_service_id));
        uint32_t methodId = (uint32_t)(method->options().GetExtension(local_method_id));
        ...
    }

考慮到serviceId methodId的範圍,可以直接打包到一個32位整數裏:

uint32_t ret = (serviceId << 16) | methodId;

然後就可以把這個值作爲網絡消息頭的一部分發送。

當然服務器端是需要建立這個標識值到service的映射的:

bool MyRPCServer::registerService(google::protobuf::Service *rpcService) {
        const google::protobuf::ServiceDescriptor = rpcService->GetDescriptor();
        int methodCnt = pSerDes->method_count();

        for (int i = 0; i < methodCnt; i++) {
            google::protobuf::MethodDescriptor *pMethodDes = pSerDes->method(i);
            uint32_t rpcCode = PacketCodeBuilder()(pMethodDes); // 計算出映射值
            _rpcCallMap[rpcCode] = make_pair(rpcService, pMethodDes); // 建立映射
        }
        return true;
    }

服務端收到RPC調用後,取出這個標識值,然後再從_rpcCallMap中取出對應的service和method,最後進行調用:

google::protobuf::Message* response = _pService->GetResponsePrototype(_pMethodDes).New();
    // 用於迴應的closure
    RPCServerClosure *pClosure = new (nothrow) RPCServerClosure( 
            _channelId, _pConnection, _pReqMsg, pResMsg, _messageCodec, _version);
    RPCController *pController = pClosure->GetRpcController();
    ...
    // protobuf 生成的CallMethod,會自動調用到Echo接口
    _pService->CallMethod(_pMethodDes, pController, _pReqMsg, pResMsg, pClosure);

參考

發佈了707 篇原創文章 · 獲贊 48 · 訪問量 97萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章