install
mac
brew install thrift # thrift
gem install thrift # ruby
pip install thrift # python
example
Reportez-vous à https://www.ibm.com/developerworks/jp/opensource/library/os-cloud-apache-thrift/
Remarque: essayez d'abord avec Python 2
Faites-le presque selon la page
proj.thrift
# proj.thrift
namespace py demoserver
namespace rb demoserver
/* All operands are 32-bit integers called a Value */
typedef i32 Value
typedef i32 Result
/* Math service exposes an some math function */
service MyMath
{
Result add( 1: Value op1, 2: Value op2 ),
Result mul( 1: Value op1, 2: Value op2 ),
Result min( 1: Value op1, 2: Value op2 ),
Result max( 1: Value op1, 2: Value op2 )
}
thrift --gen py proj.thrift
thrift --gen rb proj.thrift
Écrivez la partie traitement dans ce
server.py
#!/usr/bin/python
import sys
sys.path.append('./gen-py')
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import demoserver.MyMath
class MathImpl( demoserver.MyMath.Iface ):
def add( self, op1, op2 ):
return op1 + op2
def mul( self, op1, op2 ):
return op1 * op2
def max( self, op1, op2 ):
return max([op1, op2])
def min( self, op1, op2 ):
return min([op1, op2])
if __name__ == '__main__':
processor = demoserver.MyMath.Processor( MathImpl() )
transport = TSocket.TServerSocket( port = 18181 )
tbfactory = TTransport.TBufferedTransportFactory()
pbfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TThreadedServer( processor, transport, tbfactory, pbfactory )
print('Starting the Math Server...')
server.serve();
client.rb
# Make thrift-generated code visible
$:.push('./gen-rb')
require 'thrift'
require 'my_math'
begin
# Build up the Thrift stack
transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', 18181))
protocol = Thrift::BinaryProtocol.new(transport)
client = Demoserver::MyMath::Client.new(protocol)
transport.open()
# Try an add operation
result = client.add( 1, 5 )
puts result.inspect
# Try a max operation
result = client.max( 9, 7 )
puts result.inspect
transport.close()
Dans le lien de référence
python server.py
ruby client.rb
6 ## 1+5 réponses
9 ##Réponse maximale pour 9 et 7
Recommended Posts