浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Apache Thrift

Example

Apache Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages. Instead of writing a load of boilerplate code to serialize and transport your objects and invoke remote methods, you can get right down to business.

The following example is a simple service to store user objects for a web front end.


      struct UserProfile {
        1: i32 uid,
        2: string name,
        3: string blurb
      }
      service UserStorage {
        void store(1: UserProfile user),
        UserProfile retrieve(1: i32 uid)
      }
      
      # Make an object
      up = UserProfile(uid=1,
                       name="Test User",
                       blurb="Thrift is great")

      # Talk to a server via TCP sockets, using a binary protocol
      transport = TSocket.TSocket("localhost", 9090)
      transport.open()
      protocol = TBinaryProtocol.TBinaryProtocol(transport)

      # Use the service we already defined
      service = UserStorage.Client(protocol)
      service.store(up)

      # Retrieve something as well
      up2 = service.retrieve(2)
posted on 2012-11-20 02:12  lexus  阅读(225)  评论(0编辑  收藏  举报