Service governance The last word in service governance is services. In a company, there are programmers who play various languages. How to manage the services they develop is a problem.

In the previous chapter, we discussed what service governance requires and how to implement it. In this chapter, we “dismember” Thrift, a very important member of service governance

This is what he wrote in the last chapter

It can be used to call methods in other development languages

This ape man has written good service governance github.com/CrazyCodes/…

Noun explanation

Thrift is a software framework for the development of extensible, cross-language services. It combines a powerful software stack with a code generation engine, To build on C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, node.js, Smalltalk, And OCaml seamlessly integrated, efficient services between these programming languages.

That’s when you get confused? How to call across languages

The basic concept

How to call this requires us to talk about our powerful communication protocol.

http (tcp)

Hypertext transfer protocol, normal access to the browser what to read news, shopping must be used when the client and server handshake? The handshake flow is very complicated and performs all sorts of decoding (think about it for a moment for the sake of understanding).

rpc

Remote procedure call protocol,RPC uses client/server mode. The requester is a client and the service provider is a server. First, the client calling process sends an invocation message with process parameters to the server process and then waits for the reply message. On the server side, the process stays asleep until the call message arrives. When a call message arrives, the server gets the process parameters, calculates the result, sends the reply message, and waits for the next call message. Finally, the client calling process receives the reply message, gets the process result, and the call execution continues.

The advantages of RPC are so great that you can now understand that RPC communication is much faster than HTTP communication.

At this point, Facebook and Apache have developed a thrift based on the RPC communication protocol

Implementation method

  • Assuming you didn’t get it, let’s do it in real time.
  • First RPC we implement by using swoole, the other hand codes.
  • Divided into the client side and the server side to do the following demonstration

The client

First, we create a new client class that calls the UserSerivce class on the server side and the getUserInfo method in the UserService class.

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

var_dump($userInfo);
Copy the code

The only thing we need to do in the Client is to use the PHP magic __call method to call a method that doesn’t exist

class Client{ protected $serviceName; public function __construct($serviceName){ $this->serviceName = $serviceName; } public function __call($name, $arguments){ $rpcClient = new \swoole_client(SWOOLE_SOCK_TCP); $rpcClient - > connect (127.0.0.1, 9503,0.5); // The data we are going to send is predetermined, $rpcClient->send(json_encode(['service'=>$this->serviceName, 'action'=>$name, 'params'=>$arguments[0]]));  $rpcClient->close(); }}Copy the code

At this point, the data is sent to the server in JSON format via RPC protocol

The service side

$server = new swoole_server("127.0.0.1", 9503); $server->on('connect', function ($server, $fd){ echo "connection open: {$fd}\n"; }); $server->on('receive', function ($server, $fd, $reactor_id, $data) { $request = json_decode ($data, true); $request = json_decode ($data, true); $className = $request['service']; $app = new $className; $response = $app->{$request['action']}($request['params']); $server->send($fd, "Swoole: {$data}"); $server->close($fd); }); $server->on('close', function ($server, $fd) { echo "connection close: {$fd}\n"; }); $server->start();Copy the code

The articles

  • How PHP programmers can easily implement a service Governance Architecture (Part 1)

thanks

Zhou Mengkang mengkang.net/

communication

There is life, there is code.

Spread the positive energy of technology and continue to learn new knowledge.