Thriftphp server development

Source: Internet
Author: User
Thriftphp server-side development in the previous article (Thrift's java and php Data interaction) only introduced java as the server side, but "php is the best language ", naturally, thrift server developed by php is missing.

The business example used is the previous logon and registration example.


The server side is written by php code, and the client side is written by php and java.

PhpMulServer. php (note that apache or other web servers are required for running. because thrift does not provide server load balancer itself, you can use LVS, HAProxy, Nginx, and so on to perform server load balancer for HTTP requests)

 registerNamespace('Thrift', __DIR__ . '/../lib');$loader->registerDefinition('com', $GEN_DIR);$loader->register();if (php_sapi_name() == 'cli') {  ini_set("display_errors", "stderr");}use Thrift\Protocol\TBinaryProtocol;use Thrift\Transport\TPhpStream;use Thrift\Transport\TBufferedTransport;use Thrift\TMultiplexedProcessor;use com\penngo\User;class RegisterServiceHandler implements \com\penngo\RegisterServiceIf {    public function createUser($name, $psw){        $user = new User();        $user->id = 2;        $user->name = $name;        $user->password = $psw;        return $user;    }};class LoginServiceHandler implements \com\penngo\LoginServiceIf {    public function login($name, $psw){        $user = new User();        if($name == 'penngo' && $psw == '123'){            $user->id = 1;            $user->name = 'penngo';        }        return $user;    }};header('Content-Type', 'application/x-thrift');if (php_sapi_name() == 'cli') {  echo "\r\n";}$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));$protocol = new TBinaryProtocol($transport, true, true);$tMultiplexedProcessor = new TMultiplexedProcessor();$handler = new LoginServiceHandler();$loginServiceProcessor = new LoginServiceProcessor($handler);$tMultiplexedProcessor->registerProcessor("LoginService", $loginServiceProcessor);$registerService = new RegisterServiceHandler();$registerServiceProcessor = new RegisterServiceProcessor($registerService);$tMultiplexedProcessor->registerProcessor("RegisterService", $registerServiceProcessor);$transport->open();$tMultiplexedProcessor->process($protocol, $protocol);$transport->close();


Php client call

PhpMulClient. php

 registerNamespace('Thrift', __DIR__ . '/../lib');$loader->registerDefinition('com', $GEN_DIR);$loader->register();use Thrift\Protocol\TBinaryProtocol;use Thrift\Protocol\TMultiplexedProtocol;use Thrift\Transport\THttpClient;use Thrift\Transport\TBufferedTransport;use Thrift\Exception\TException;try {$socket = new THttpClient('localhost', 80, '/thrift/penngo/PhpMulServer.php');//   $socket = new THttpClient('localhost', 8090, '/thrift/penngo/PhpMulServer.php');  $transport = new TBufferedTransport($socket);  $protocol = new TBinaryProtocol($transport);  $loginProtocol = new TMultiplexedProtocol($protocol, 'LoginService');  $loginService = new LoginServiceClient($loginProtocol);  $user = $loginService->login('penngo', '123');  var_dump($user);  $registerProtocol = new TMultiplexedProtocol($protocol, 'RegisterService');  $registerService = new RegisterServiceClient($registerProtocol);  $user = $registerService->createUser('penngo', '123');  var_dump($user);//   $transport->close();} catch (TException $tx) {  print 'TException: '.$tx->getMessage()."\n";}?>


Java client

HttpClient. java

Package com. penngo. main; import org. apache. thrift. *; import org. apache. thrift. protocol. *; import org. apache. thrift. transport. *; import com. penngo. loginService; import com. penngo. registerService; import com. penngo. user; public class HttpClient {public static void main (String [] args) {try {THttpClient transport = new THttpClient (" http://localhost:80/thrift/penngo/PhpMulServer.php "); TProtocol protocol = new TBinaryProtocol (transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol (protocol," LoginService "); LoginService. client loginClient = new LoginService. client (mp1); TMultiplexedProtocol mp2 = new TMultiplexedProtocol (protocol, "RegisterService"); RegisterService. client registerClient = new RegisterService. client (mp2); // transport. open (); User user = loginClient. login ("penn Go "," 123 "); if (user! = Null) {System. out. println ("logon successful:" + user. getId () + "" + user. getName ();} else {System. out. println ("logon failed");} User user2 = registerClient. createUser ("test", "123"); if (user2! = Null) {System. out. println ("User created successfully:" + user2.getId () + "" + user2.getName ();} else {System. out. println ("failed to create user");} // transport. close ();} catch (TException x) {x. printStackTrace ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.