In the previous post, I shared you the way how to install Apache Thrift in Ubuntu server. And now, I am writing the first application named Hello World with Apache Thrift in Python and Laravel 4.2
Concept:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
BROWSERS WEB SERVER Apache Thrift | | | | hello? | | |-------------->| | | | hello? | | |-------------->| | | | | | Hello World! | | |<--------------| | | | | | | | Hello World! | | |<--------------| | | | | | | | |
You could clone full source from here: https://github.com/thanhson1085/thrift-laravel
Please download the source code before reading next steps.
Step 1: Create thrift files hello.thirft and exceptions.thrift
hello.thrift:
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace py hello.thrift namespace php Hello.Thrift include "exceptions.thrift" service HelloService { string hello( 1: required string name ) throws ( 1: exceptions.EUnknown ie ) } |
exceptions.thrift:
1 2 3 4 5 6 7 8 9 |
namespace py hello.exceptions namespace php Hello.Exceptions /** * Unknown Exceptions */ exception EUnknown { 1: string message } |
Step 2: Run thrift compiler command to generate source code
1 2 3 4 |
thrift --gen py -out src/python hello.thrift thrift --gen php -out src/php hello.thrift thrift --gen py -out src/python exceptions.thrift thrift --gen php -out src/php exceptions.thrift |
See the details at gen.sh file
Step 3: Code the server-side
Code Hello Server hello-server.py (located src/python directory):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#!/usr/bin/env python import sys import logging from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TNonblockingServer from hello.thrift import HelloService from hello.handler import HelloHandler port = 9093 handler = HelloHandler() processor = HelloService.Processor(handler) transport = TSocket.TServerSocket(port=port) tfactory = TBinaryProtocol.TBinaryProtocolFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TNonblockingServer.TNonblockingServer(processor, transport, tfactory, pfactory, 10) # be able to run 10 threads # log to stdout log = logging.getLogger() log.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(threadName)s - %(message)s') ch = logging.StreamHandler(sys.stdout) ch.setLevel(1) ch.setFormatter(formatter) log.addHandler(ch) logging.info('Hello server starting on port %d', port) try: server.serve() except (KeyboardInterrupt, SystemExit): logging.info('Caught signal, stopping threads') logging.info('Threads stopped, terminating') |
Create Hander hander.py located in src/python/hello:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import logging from exceptions.ttypes import EUnknown class HelloHandler: ''' Implements thrift server logic. ''' def __init__(self): logging.info('Initializing handler') def __del__(self): logging.info('Disposing handler') # ------------------------------------------------------------------------- # -------------- Thrift - HelloService Implementation ------------- # ------------------------------------------------------------------------- def hello(self, name): logging.info('Run hello request') return 'Hello ' + name |
Step 4: Run a test
Finally, test the server with server.sh script:
1 |
bash run.sh |
The output will be as below:
1 2 3 |
$ bash run.sh INFO:root:Hello server starting on port 9093 2014-12-01 10:38:43,231 - INFO - MainThread - Hello server starting on port 9093 |
That means that you created server-side successfully.
Please keep reading Part 2 to know how to code the client-side with PHP Laravel 4.2