Steps:
(1) Create Thrift Syntax rules file, named Xuejiehome.thrift
namespace PHP xuejiehome_thrift
namespace Py Xuejiehome_thrift
Service Xuejiehome {
void execute (1:string date, 2:string action)
String Progress (1:string date, 2:string action)
}
Note: Define two callable function names and parameters, execute and progress
(2) Install thrift and generate PHP corresponding class
wget http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.2/thrift-0.9.2.tar.gz
Tar zxvf thrift-0.9.2.tar.gz
CD thrift-0.9.2
./bootstrap.sh
./configure
Make && make install
Thrift--gen PHP Xuejiehome_thrift # Build folder gen-php
(3) Put the gen-php folder generated in the previous step into the thrift directory
(4) Copy the thrift extension file under the Thrift source code to the thrift directory
CP Thrift-0.9.2/lib/php/lib/thrift thrift/
Ls-al Thrift
Wyl-mac:thrift wyl$ Ls-al
Total 0
DRWXRWXRWX 4 Wyl Staff 136 3 26 10:53.
DRWXRWXRWX Wyl Staff 510 4 23 09:27.
DRWXRWXRWX Wyl Staff 408 3 10:53 Thrift
DRWXRWXRWX 3 Wyl Staff 102 3 10:53 gen-php
(5) Writing Python server
#!/usr/bin/env python
#-*-Encoding:utf-8-*-
From Vendor.core Import Config
Config.init ()
From Vendor.xuejiehome_thrift import Ttypes, Constants,xuejiehome
From Thrift.transport import Tsocket, Ttransport
From Thrift.protocol import Tbinaryprotocol
From Thrift.server import Tserver
From Src.handler Import Handler
Import Signal
Import sys, OS
VERSION = "0.5.1"
def signal_handle (n):
If n = 2:
Print "Process terminated"
Sys.exit (0)
Def signal_start ():
Signal.signal (signal. SIGINT, Signal_handle)
Class Server ():
def run (self):
Signal_start ()
Processor = None
h = Handler ()
Processor = Xuejiehome. Processor (h)
Transport = Tsocket.tserversocket (Config.thrift.host, Config.thrift.port)
Tfactory = Ttransport.tbufferedtransportfactory ()
Pfactory = Tbinaryprotocol.tbinaryprotocolfactory ()
Server = Tserver.tforkingserver (processor, transport, tfactory, Pfactory)
Print "Thrift Server version: [%s]"% version
Print "Listen:%s:%s"% (Config.thrift.host, Config.thrift.port)
Print "\033[33m[info] start handle server\033[0m"
Server.serve ()
if __name__ = = "__main__":
If Len (SYS.ARGV) > 1 and sys.argv[1] = = "Daemon":
#创建守护进程
Print "\033[33m[info] Start daemon mode.\033[0m"
Try
If Os.fork () > 0:
Os._exit (0)
Except OSError, error:
print ' \033[31m[error]fork #1 failed:%d (%s) \033[0m '% (Error.errno, error.strerror)
Os._exit (1)
Os.chdir ('/')
Os.setsid ()
Os.umask (0)
Serve = Server ()
Serve.run ()
(6) Writing PHP client--thriftclient.php
<?php
namespace Xj\corebundle\services;
Use Thrift\classloader\thriftclassloader;
Use Thrift\protocol\tbinaryprotocol;
Use Thrift\transport\tsocket;
Use thrift\transport\thttpclient;
Use Thrift\transport\tbufferedtransport;
Use thrift\exception\texception;
Class Thriftbase {
Public function __construct (commonconfig $config) {
$this->path=dirname ($_server[' document_root ']). ' /src/jy/corebundle/services/thrift ';
$this->host = $config->thrift[' thrift_host '];
$this->port = $config->thrift[' Thrift_port '];
$this->timeout = $config->thrift[' thrift_timeout '];
Require (' thrift/thrift/classloader/thriftclassloader.php ');
}
Public function Connect ($host, $port, $timeout) {
$GEN _dir = "{$this->path}/gen-php";
$loader = new Thriftclassloader ();
$loader->registernamespace (' Thrift ', $this->path);
$loader->registerdefinition (' jy\corebundle\services ', $GEN _dir);
$loader->register ();
$socket = new Tsocket ($this->host, $this->port);
$socket->setrecvtimeout ($timeout);
$this->transport = new Tbufferedtransport ($socket, 1024, 1024);
$this->protocol = new Tbinaryprotocol ($this->transport);
}
Public Function __destruct () {
$this->transport->close ();
}
}
Class Thriftrefreshclient extends thriftbase{
function Call_progress ($date) {
$this->connect ($this->host, $this->port, $this->timeout);
Include "{$this->path}/gen-php/xuejiehome_thrift/xuejiehome.php";
$client = new \xuejiehome_thrift\xuejiehomeclient ($this->protocol);
$this->transport->open ();
$client->progress ($date, ' test '); # Call the Progress function
}
}