thrift的简单使用(二)

thrift的简单使用(二)


1 关于RPC和Thrift框架


定义简单数据类文件:

    
    基本type 1
        
        namespace  py  thriftDemo.tag_info_tag_types  //  定义文件的空间
        
        
        enum  TagInfoErrorCode {
            //通用错误码
            TAG_INFO_COMMON_OK=0,//正常返回
            TAG_INFO_COMMON_REQ_PARAMS_ERROR=1,//请求参数错误
            TAG_INFO_COMMON_SERVER_ERROR=2,//服务异常
            TAG_INFO_COMMON_NOT_EXISTS=3,//查询对象不存在
            TAG_INFO_COMMON_NOT_FOUND=404,//操作对象不存在404notfound
        }
        
        
        /***1.2.请求头部结构体*/
        
        struct TagInfoReqHeader{
            /**@Description("uuid")*/
            1:i64 uuid,
            /**@Description("userid")*/
            2:i64 userid,
            }
        
        
        /***1.3.响应头部结构体*/
        struct  TagInfoRespHeader{
            /**@Description("uuid")*/
            1:i64 uuid,
            /**@Description("错误码")*/
            2:TagInfoErrorCode errorCode,
            /**@Description("错误信息")*/
            3:string errorMessage,
        }
        
        
        
        
        /**2.UserTag数据结构*/
        
        struct UserTag{
            /**@Description("id")*/
            1:i64 id,
            /**@Description("userid")*/
            2:i64 userid,
            /**@Description("tagname")*/
            3:string tagname,
            /**@Description("type")*/
            4:i64 type,
            /**@Description("create_time")*/
            5:string create_time,
            /**@Description("update_time")*/
            6:string update_time,
            }
        
        
        /**3.TipsInfo数据结构*/
        
        struct TipsInfo{
            /**@Description("tips_info_id")*/
            1:i64 tips_info_id,
            /**@Description("user_tag_id")*/
            2:i64 user_tag_id,
            /**@Description("key")*/
            3:string key,
            /**@Description("create_time")*/
            4:string create_time,
            /**@Description("num")*/
            5:i64 num,
            }
        
        
        //4. 一个标签信息
        
        struct TagInfo{
            1:i64   user_tag_id,
            2:string    tagname,
            3:list<TipsInfo>    tipsInfoList,
        }
        
        
        
        // 5 Taginfo 返回结果
        
        struct TagInfoResp{
            1:TagInfoReqHeader   respHeader,
            2:TagInfo       tagInfo,
        }
        
        
        
        // 6 getTagList  返回结果
        
        struct GetTagInfoListResp{
            1:TagInfoRespHeader  respHeader,
            2:list<TagInfo>    tagInfoList
        }
        
        // 7  deleteTag  删除 返回结果
        
        struct  DeleteTagResp{
            1:TagInfoRespHeader     respHeader,
        }
    
    
    基本type 2
    
        namespace  py thriftDemo.tag_info_article_types
        
        const map<string, string> MAPCONSTANT = {
            'hello':"world",
            "bye":"moon"
        }
        
        
        struct  Article{
            1:i32   id,
            2:string    title,
            3:string    content,
            4:string    author
        }
        
   


定义服务接口(方法)


    include "tag_info_tag_types.thrift"
    include "tag_info_article_types.thrift"
    
    namespace  py thriftDemo.tag_info_tag
    
    
    service  TagInfoService{
    
    
    /***1.获取tag列表*/
        tag_info_tag_types.GetTagInfoListResp getTagInfoList(
        /**@Description(header")*/
        1:tag_info_tag_types.TagInfoReqHeader reqHeader,
        /**@Description("type")*/
        2:i64 type,
        /**@Description("userid")*/
        3:i64 userid,
        )
        /***2.增加一个tag*/
        tag_info_tag_types.TagInfoResp  addTag(
            1:tag_info_tag_types.TagInfoReqHeader   reqHeader,
            2:i64           type,
            3:i64           userid,
            4:string        tagname,
            6:list<string>  keys,
        )
        /*删除一个tag*/
        tag_info_tag_types.DeleteTagResp deleteTag(
            1: tag_info_tag_types.TagInfoReqHeader  reqHeader,
            2:i64   user_tag_id,
    
        )
    
        /*4 更新一个tag*/
    
         tag_info_tag_types.TagInfoResp  updateTag(
            1: tag_info_tag_types.TagInfoReqHeader  reqHeader,
            2:i64       user_tag_id,
            3:i64       type,
            4:i64       userid,
            5:string    tagname,
            6:list<string>  keys,
         )
    
    
     /*5 获取一个tag*/
    
         tag_info_tag_types.TagInfoResp  getTagInfoByUserTagID(
            1: tag_info_tag_types.TagInfoReqHeader  reqHeader,
            2:i64       user_tag_id,
         )
    
        // artile 相关
    
         list<string> getList(
            1:i64       callTime,
            2:string    name,
            3:map<string,string>    paramMap,
         )
    
            //     没有返回值
         void  put(
            1:tag_info_article_types.Article    newArticle
         )
    
}


定义好接口文件,生成对应的package 文件

thrift -o 目录 --gen py *.thrift


python 的客户端与服务端的实现

注意安装 thrift 版本 pip install thrift


    服务端
            
        import socket
        import sys
        
        from thriftDemo.tag_info_tag import TagInfoService
        from thriftDemo import tag_info_tag_types
        
        from thrift.transport import TSocket
        from thrift.transport import TTransport
        from thrift.protocol import TBinaryProtocol
        from thrift.server import TServer
        
        
        
        class  TagInfoHandler:
        
            def getTagInfoList(self, reqHeader, type, userid):
                r = tag_info_tag_types.ttypes.GetTagInfoListResp()
                r.respHeader = tag_info_tag_types.ttypes.TagInfoRespHeader()
                r.respHeader.uuid = reqHeader.uuid
                r.respHeader.errorCode = tag_info_tag_types.ttypes.TagInfoErrorCode.TAG_INFO_COMMON_OK
                r.respHeader.errorMessage = ""
        
                tagInfoList = []
                for i in range(4):
                    taginfo = tag_info_tag_types.ttypes.TagInfo()
                    taginfo.user_tag_id = int(i)
                    taginfo.tagname = "标签名称"+str(i)
                    taginfo.tipsInfoList = [
                        tag_info_tag_types.ttypes.TipsInfo(
                            tips_info_id=num,
                            user_tag_id=i,
                            key= "this is key" + str(num),
                            create_time="2018-04-11",
                            num=1
                        )
                        for num in range(3)
                    ]
                    tagInfoList.append(taginfo)
                r.tagInfoList = tagInfoList
                return r
        
        
        
        handler = TagInfoHandler()
        processor = TagInfoService.Processor(handler)
        transport = TSocket.TServerSocket("127.0.0.1",9090)
        tfactory = TTransport.TFramedTransportFactory()
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        
        server = TServer.TSimpleServer(processor,transport,tfactory,pfactory)
        print("starting  thrift  server in python")
        server.serve()
        print("启动!")
        
        
    
    客户端
    
        import socket
        import sys
        
        from thriftDemo.tag_info_tag import TagInfoService
        from thriftDemo import tag_info_tag_types
        
        from thrift.transport import TSocket
        from thrift.transport import TTransport
        from thrift.protocol import TBinaryProtocol
        from thrift.server import TServer
        
        tsocket = TSocket.TSocket("127.0.0.1",9090)
        transportFactory = TTransport.TFramedTransportFactory()
        transport = transportFactory.getTransport(tsocket)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client = TagInfoService.Client(protocol)
        transport.open()
        
        reqHeader = tag_info_tag_types.ttypes.TagInfoReqHeader(uuid=88,userid=66)
        ret = client.getTagInfoList(reqHeader,1,1)
        
        print(ret)
        
   

Buy me a 肥仔水!