thritf

thrift 创建java php ruby服务: http://reasoning-lipl.iteye.com/blog/1969747
thrift 数据格式:http://wenku.baidu.com/view/3034b9c2bb4cf7ec4afed00b
http://dongxicheng.org/search-engine/thrift-guide/
http://www.it165.net/pro/html/201304/5438.html
http://blog.163.com/scuqifuguang@126/blog/static/171370086201362273929684/
http://www.iteye.com/problems/71700
http://chinacheng.iteye.com/blog/838006

三、Ruby

server代码:server_demo.rb

Ruby代码  收藏代码
  1. require '../lib/thrift'  
  2. require '../thrift-demo/gen-rb/user_service'  
  3.   
  4. class UserServiceHandler  
  5.   def getUserNameById(userId)  
  6.       return "hello, user(id: #{userId})"  
  7.   end  
  8. end  
  9.   
  10. handler = UserServiceHandler.new()  
  11. processor = UserService::Processor.new(handler)  
  12. transport = Thrift::ServerSocket.new('localhost', 9090)  
  13. transportFactory = Thrift::BufferedTransportFactory.new()  
  14. server = Thrift::SimpleServer.new(processor, transport, transportFactory)  
  15.   
  16. puts "Starting the server..."  
  17. server.serve()  

 client代码:client_demo.rb

Ruby代码  收藏代码
  1. require '../lib/thrift'  
  2. require '../thrift-demo/gen-rb/user_service'  
  3.   
  4. begin  
  5.   transport = Thrift::BufferedTransport.new(Thrift::Socket.new("localhost", 9090))  
  6.   protocol = Thrift::BinaryProtocol.new(transport)  
  7.   client = UserService::Client.new(protocol)  
  8.   
  9.   transport.open()  
  10.   puts client.getUserNameById(24)  
  11.   transport.close()  
  12. rescue Thrift::Exception => tx  
  13.   puts 'Thrift.Exception: ', tx.message  
  14. end  

 ruby还是很简洁的,执行`thrift --gen rb UserService.thrift`生成ruby的接口类文件,然后依次运行server脚本和client脚本。

 

begin
  #...
rescue Thrift::TransportException => e
  raise e unless e.type == Thrift::TransportException::END_OF_FILE
  # do whatever if it is not end of file reached.
end
posted on 2014-09-25 09:33  qinyan20  阅读(476)  评论(0编辑  收藏  举报