Php programmer’s simple operation of service governance architecture (2)


This article describes the php programmer to simply carry out the service governance architecture operation. For your reference, the details are as follows:

The last word of service governance is service. There are programmers who play various languages in a company. How to manage the services they develop in a unified way is a problem.

The previous chapter mainly talks about what service governance needs and how to achieve it. In this chapter, we will “dismember” a very important member of service governance, thrift

This is what he wrote in the last chapter

Temporarily, it can be understood as a method that can be used to call other development languages

This ape man has written a good service governance

Explanation of terms

Thrift is actually a software framework for the development of extensible and cross language services. It combines powerful software stack and code generation engine to build in C + +, Java, go, python, PHP, ruby, Erlang, Perl, Haskell, C ා, cocoa, JavaScript, Node.js , Smalltalk, and Ocaml these programming languages are seamless and efficient services.

Now you’re wondering how to call it across languages

Basic concepts

How to call this needs to talk about our powerful communication protocol.

http (tcp)

Hypertext transmission protocol is used when you visit the browser normally. You need to shake hands between the client and the server when you are watching news and shopping. If you succeed, you can display it normally. The outflow of the middle handshake is very complicated, and all kinds of decoding and coding are carried out


RPC adopts client / server mode. The requester is a client and the service provider is a server. First, the client invoking process sends a call 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 message arrives. When a calling message arrives, the server obtains the process parameters, calculates the result, sends the reply information, and then waits for the next call information. Finally, the client calls the process to receive the reply information, gets the process result, and then calls the execution to proceed.

RPC has many advantages. Now you can understand that RPC communication is much faster than HTTP communication.

At this time, Facebook and Apache are very good. They have developed a set of thrift based on RPC communication protocol

Implementation method

  • If you don’t understand the above, here we are fighting.
  • First of all, we use swote to implement RPC, other hand codes.
  • Divided into client and server to do the next demonstration


First, we create a new client class to call the server’s userservice class, and call the getUserInfo method in the userservice class.

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


In client, we only need to do one thing, using PHP magic method__ Call to call a method that does not exist

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 agreed in advance, just like the API for opening to the outside world

At this time, the data is sent to the server in JSON format through the RPC Protocol


$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 comply with 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

  • How PHP programmers develop simple service governance architecture (1)

More about PHP related content interested readers can see this site topic: “PHP object-oriented programming introductory tutorial”, “PHP array (array) operation skills”, “PHP basic syntax introductory course”, “PHP operation and operator Usage Summary”, “PHP character string (string) Usage Summary”, “PHP + MySQL database operation introduction tutorial” and “PHP common database operation” Summary of writing skills

I hope this article will help you with PHP programming.