当前位置: 首页 > news >正文

做兽设的网站网站开发还是安卓开发好

做兽设的网站,网站开发还是安卓开发好,深圳高端网站定制,泰安人才网首页如何在proto中定义一个rpc服务? syntax proto3; //声明protobuf的版本package fixbug; //声明了代码所在的包 #xff08;对于C来说就是namespace)//下面的选项#xff0c;表示生成service服务类和rpc方法描述#xff0c; 默认是不生成的 option cc_generi…如何在proto中定义一个rpc服务? syntax proto3; //声明protobuf的版本package fixbug; //声明了代码所在的包 对于C来说就是namespace)//下面的选项表示生成service服务类和rpc方法描述 默认是不生成的 option cc_generic_services true;message ResultCode{ //定义返回的错误码int32 errcode 1;bytes errmsg 2; }//常用的数据类型 数据、列表、映射表//定义登录请求消息类型 name pwd message LoginRequest{bytes name 1; //等于1表示这是第一个参数一般string的存储定义为bytesbytes pwd 2;// mapint32,string test 3; //映射表类型 }//定义登录响应消息类型 message LoginResponse{ResultCode result 1;bool success 3; }message GetFriendsListRequest{uint32 user_id 1; }message User{bytes name 1;uint32 age 2;enum Sex{MAN 0;WOMAN 1;}Sex sex 3; }message GetFriendsListResponse{ResultCode result 1;repeated User friend_list 2; //定义了一个列表数据类型 }//在protobuf里面怎么定义描述rpc方法的类型---service service UserService{rpc Login(LoginRequest) returns(LoginResponse);rpc GetFriendsList(GetFriendsListRequest) returns(GetFriendsListResponse); }关键字service用于定义rpc服务例如Userservice有两个rpc服务分别是Login和GetFriendsList 返回值分别就是上述定义的消息类型。 老样子先执行protoc xxx.proto --cpp_out./ 生成对应的xxx.pb.cc 和 xxx.pb.h文件。 需要注意的是选项option cc_generic_services true;得加上默认是不生成的。 这边来分析xxx.pb.h文件简单探究一下rpc调用过程 //!!!!xxx.pb.h class UserService : public ::PROTOBUF_NAMESPACE_ID::Service {protected:// This class should be treated as an abstract interface.inline UserService() {};public:virtual ~UserService();typedef UserService_Stub Stub;static const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* descriptor();virtual void Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::LoginRequest* request,::fixbug::LoginResponse* response,::google::protobuf::Closure* done);virtual void GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::GetFriendsListRequest* request,::fixbug::GetFriendsListResponse* response,::google::protobuf::Closure* done);// implements Service ----------------------------------------------const ::PROTOBUF_NAMESPACE_ID::ServiceDescriptor* GetDescriptor();void CallMethod(const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method,::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::PROTOBUF_NAMESPACE_ID::Message* request,::PROTOBUF_NAMESPACE_ID::Message* response,::google::protobuf::Closure* done);const ::PROTOBUF_NAMESPACE_ID::Message GetRequestPrototype(const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const;const ::PROTOBUF_NAMESPACE_ID::Message GetResponsePrototype(const ::PROTOBUF_NAMESPACE_ID::MethodDescriptor* method) const;private:GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UserService); }; //!!!!xxx.pb.cc void UserService::Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::LoginRequest*,::fixbug::LoginResponse*,::google::protobuf::Closure* done) {controller-SetFailed(Method Login() not implemented.);done-Run(); }void UserService::GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::GetFriendsListRequest*,::fixbug::GetFriendsListResponse*,::google::protobuf::Closure* done) {controller-SetFailed(Method GetFriendsList() not implemented.);done-Run(); }可以看到一个UserService这个类这个和message关键字有异曲同工之妙都是对应的一个类只不过继承的基类是有所不同的 message消息类型继承于抽象类message service服务类型继承于抽象类service。此外可以发现在类中实现了在proto文件中定义的两个rpc方法Login和GetFriendsList 其中两个参数是很熟悉的request和response也即这个rpc方法的需要用到的调用参数类型message基类指针表示可以接受任意消息类型。 由此可知UserService类是一个服务提供者Callee) 再来看另一个类 //!!!!xxx.pb.h class UserService_Stub : public UserService {public:UserService_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel);UserService_Stub(::PROTOBUF_NAMESPACE_ID::RpcChannel* channel,::PROTOBUF_NAMESPACE_ID::Service::ChannelOwnership ownership);~UserService_Stub();inline ::PROTOBUF_NAMESPACE_ID::RpcChannel* channel() { return channel_; }// implements UserService ------------------------------------------void Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::LoginRequest* request,::fixbug::LoginResponse* response,::google::protobuf::Closure* done);void GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::GetFriendsListRequest* request,::fixbug::GetFriendsListResponse* response,::google::protobuf::Closure* done);private:::PROTOBUF_NAMESPACE_ID::RpcChannel* channel_;bool owns_channel_;GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(UserService_Stub);//!!!!xxx.pb.cc void UserService_Stub::Login(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::LoginRequest* request,::fixbug::LoginResponse* response,::google::protobuf::Closure* done) {channel_-CallMethod(descriptor()-method(0),controller, request, response, done); } void UserService_Stub::GetFriendsList(::PROTOBUF_NAMESPACE_ID::RpcController* controller,const ::fixbug::GetFriendsListRequest* request,::fixbug::GetFriendsListResponse* response,::google::protobuf::Closure* done) {channel_-CallMethod(descriptor()-method(1),controller, request, response, done); }可以看见这个类没有无参构造有几个RpcChannel*参数传递的构造方法不妨看看RpcChannel类 class PROTOBUF_EXPORT RpcChannel {public:inline RpcChannel() {}virtual ~RpcChannel();// Call the given method of the remote service. The signature of this// procedure looks the same as Service::CallMethod(), but the requirements// are less strict in one important way: the request and response objects// need not be of any specific class as long as their descriptors are// method-input_type() and method-output_type().virtual void CallMethod(const MethodDescriptor* method,RpcController* controller, const Message* request,Message* response, Closure* done) 0;private:GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcChannel); };很显然这是个抽象基类需要用户自己实现派生类重写基类的CallMethod方法 在之后login和GetFriendsList两个方法基本调用方式一样都是通过_channel调用CallMethod方法执行对应rpc调用那么可知UserService_Stub是服务消费者Caller 如何发布一个本地rpc服务 #include iostream #include string #include user.pb.h class UserService : public fixbug::UserServiceRpc //使用rpc服务发布端(rpc服务提供者) { public:bool Login(std::string name, std::string pwd){std::coutdoing local service : Login std::endl;std::cout name: name pwd : pwd std::endl;} /*** 重写基类UserServiceRpc的虚函数 下面这些方法都是框架直接调用的* caller Login(LoginRequest) transmit callee* callee Login(LoginRequest) 调用下述的Login方法* */void Login(::google::protobuf::RpcController* controller,const ::fixbug::LoginRequest* request,::fixbug::LoginResponse* response,::google::protobuf::Closure* done){//框架给业务上报了请求参数LoginRequest 应用获取相应数据做本地业务std::string name request-name();std::string pwd request-pwd();//本地业务bool res Login(name, pwd);//把响应写入 包括错误码、错误消息、返回值response-set_success(0);fixbug::ResultCode* rc response-mutable_result();rc-set_errcode(0);rc-set_errmsg();//执行回调操作 执行响应对象数据的序列化和网络发送由框架来完成done-Run();} };继承对应的UserServiceRpc服务提供者重写其基类方法即可。 上面只是发布过程具体rpc调用待续。。。
http://wiki.neutronadmin.com/news/451698/

相关文章:

  • 网站开发视频百度云网站建设环境分析
  • 模板网站的好处学网站建设需要什么软件
  • 秦皇岛网站制作微商城建设谷德设计网官网
  • 免费企业网站建设要求做公司网站每年多少钱
  • 万网站wordpress 走马灯
  • 网站备案logo上海中学有哪些
  • 一级a做爰片免费网站丶给娃娃做衣服卖的网站
  • 中国建设部官方网站海外seo投放
  • seo兼职怎么收费宁波专业seo推广价格
  • 主流的网站开发语言网站开发技术文档范例
  • 空白的网站怎么建设常熟网站建设书生商友
  • 怎么申请个人网站长春网站改版
  • 沈阳网站设计定制网站建设付网站建设费
  • 学做网站论坛vip账号破解中国网站备案信息查询
  • 自贡网站设计新手怎么做淘宝店铺
  • 网站设计规划书中国城乡建设网
  • 网站制作时wordpress 图片分享主题
  • 昆山专业网站建设公司小程序开发指南
  • 帮人网站开发维护违法成都 网站制作
  • 苏州网站建设代理创建网站的注意事项
  • 网站开发一般包括有没有类似wordpress
  • 电视直播网站怎么做苏州苏网建设公司在建工程
  • 类似优酷网站建设价格河南红旗渠建设集团网站
  • wordpress手机网站模版注销主体备案与网站备案表
  • 淮安网站网页设计专业APP客户端做网站
  • 做网站图片大小大学生怎么做网站
  • 自建网站需要学哪些深圳建立企业网站
  • 模板建站网页东莞做网站有哪些
  • 网站地图怎么做的scrm和crm如何配合
  • 电子商务网站建设与管理心得十大短视频制作软件