How PHP Programmers Simply Develop Service Governance Architecture (2)

  php, Service governance, swoole, thrift

The unique stroke of service governance is service. In a company there are programmers who play various languages. How to manage the services they develop in a unified way is a problem.

The previous chapter mainly talked about what the next service governance needs and how to implement it. In this chapter, we “dismembered” a very important member of the service governance, Thrift, in detail.

This is what he wrote when he explained in the previous chapter.

For the time being, it can be understood that it can be used to call methods in other development languages.

This ape-man has written a good service management …

Noun interpretation

Thrift is actually a software framework for developing extensible and cross-language services. It combines a powerful software stack and a code generation engine to build a seamless and efficient service between the programming languages C++,Java, GO, Python, PHP, Ruby, Erlang, Perl, Haskell, C #, Cocoa, JavaScript, Node. JS, SmallTalk, and Ocaml.

This time you are confused? , how to call across languages

Basic concept

How to invoke this requires us to talk about our strong communication protocol.

http (tcp)

Hypertext Transfer Protocol (HTP), which is always used when accessing the browser to watch news and shopping, requires the client and server to shake hands? Success can be displayed normally. The outflow of this intermediate handshake is very complicated, and various decoding codes are executed (for convenience of understanding, think so for the time being)


Remote procedure call protocol, RPC adopts client/server mode. The requestor is a client and the service provider is a server. First, the client calling process sends a calling message with process parameters to the service process, and then waits for the response message. On the server side, the process stays asleep until the call information arrives. When a call message arrives, the server obtains the process parameters, the calculation result, sends the reply message, and then waits for the next call message. Finally, the client calls the process to receive the reply message, obtains the process result, and then calls the execution to continue.

Rpc has many advantages. Now you can understand that rpc communication is much faster than http communication.

At this time facebook and apache became more powerful. They developed a set based on rpc communication protocol.thrift

Implementation method

  • The above assumption is that you don’t understand it. Here we will have a real fight.
  • First, rpc is implemented by using swoole, and other hand codes.
  • Divided into client and server to do the following demonstration


First of all, we new a client class, call the UserSerivce class of the server, and call the getUserInfo method in the UserService class.

$client = new Client('UserSerivce');
 $userInfo = $client->getUserInfo(1);

The only thing we need to do in Client is to use php magic method __call to call a non-existent method.

class Client{
 protected $serviceName;
 public function __construct($serviceName){
 $this->serviceName = $serviceName;
 public function __call($name, $arguments){
 $rpcClient = new \swoole_client(SWOOLE_SOCK_TCP);
 //The data we are going to send is predetermined, just like writing an Api that is open to the outside world.

At this time, the data is sent to the server in json format through rpc protocol

Server side

$server = new swoole_server("", 9503);
 $server->on('connect', function ($server, $fd){
 echo "connection open: {$fd}\n";
 $server->on('receive', function ($server, $fd, $reactor_id, $data) {
 // $data is the data sent by the client, we can do this to call the class, of course you must abide by PSR-4 Autoloader
 $request   = json_decode ($data, true);
 $className = $request['service'];
 $app       = new $className;
 $response  = $app->{$request['action']}($request['params']);
 $server->send($fd, "Swoole: {$data}");
 $server->on('close', function ($server, $fd) {
 echo "connection close: {$fd}\n";

Previous articles


Zhou mengkang