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
- require '../lib/thrift'
- require '../thrift-demo/gen-rb/user_service'
- class UserServiceHandler
- def getUserNameById(userId)
- return "hello, user(id: #{userId})"
- end
- end
- handler = UserServiceHandler.new()
- processor = UserService::Processor.new(handler)
- transport = Thrift::ServerSocket.new('localhost', 9090)
- transportFactory = Thrift::BufferedTransportFactory.new()
- server = Thrift::SimpleServer.new(processor, transport, transportFactory)
- puts "Starting the server..."
- server.serve()
client代码:client_demo.rb
- require '../lib/thrift'
- require '../thrift-demo/gen-rb/user_service'
- begin
- transport = Thrift::BufferedTransport.new(Thrift::Socket.new("localhost", 9090))
- protocol = Thrift::BinaryProtocol.new(transport)
- client = UserService::Client.new(protocol)
- transport.open()
- puts client.getUserNameById(24)
- transport.close()
- rescue Thrift::Exception => tx
- puts 'Thrift.Exception: ', tx.message
- 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