justdoing / ThriftDemo_PHP_CPP

Demo for Thrift RPC framework. PHP client call CPP server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thrift Demo. PHP client call CPP server

What is Thrift

Thrift is an interface definition language and binary communication protocol that is used to define and create services for numerous languages. It is used as a remote procedure call (RPC) framework and was developed at Facebook for "scalable cross-language services development". It combines a software stack with a code generation engine to build services that work efficiently to a varying degree and seamlessly between C#, C++ (on POSIX-compliant systems), Cappuccino, Cocoa, Delphi, Erlang, Go, Haskell, Java, Node.js, OCaml, Perl, PHP, Python, Ruby and Smalltalk. Although developed at Facebook, it is now an open source project in the Apache Software Foundation. The implementation was described in an April 2007 technical paper released by Facebook, now hosted on Apache. -- From Wiki Apache Thrift

This Demo

PHP client call CPP server.

Work Flow

  1. PHP client call with Request.
  2. CPP server response with Response.

image

Thrift interface definition

namespace cpp TTG
namespace php TTG

enum ResponseState {
    StateOk = 0,
    StateError = 1,
    StateEmpty = 2
}

struct Request {
    1: i32 studentID = 0
}

struct Response {
    1: i32 studentID = 0,
    2: string name,
    3: list<string> infos,
    4: ResponseState state
}

service TTGService {
    Response getStudentInfo(1: Request request);
}


Folders

CPP // CPP server source files
--CMakeLists.txt //CMake file
--... // Other cpp source files
PHP //PHP client source files
--Gen // Generated by thrift
--Thrift // Thrift php source files
--client.php // Client source file
ThriftGen // files generated by thrift
--gen-cpp
--gen-php
--TTG.thrift // definition file

How to run

Preparation

  • CMake, make, g++ ...
  • php, web server(apache or nginx, php-fpm)
  • thrift (How to install)

Compile and run

  1. cd ThriftDemo/CPP
  2. mkdir build
  3. cd build
  4. cmake ..
  5. make
  6. ./TTG.run // Run the CPP server
  7. Run ThriftDemo/PHP/client.php

About

Demo for Thrift RPC framework. PHP client call CPP server.


Languages

Language:PHP 97.9%Language:C++ 1.8%Language:Thrift 0.2%Language:CMake 0.1%