How PHP Programmers Simply Develop Service Governance Architecture (1)

  php, Service governance, soa, swoole

Without involving other languages and tools, let’s talk about how to implement service governance from PHP itself.

This ape-man has written a good service managementhttps://github.com/CrazyCodes …

Governing what?

This professional term can easily be found that the governance is service, and service is our project. Managing these service programs is called service governance.

There are now four services on the Server, namely

  • UserService
  • ShopService
  • GoodsService
  • LiveService

We call these services service providers (both internal service applications)

An application that invokes a service is called a service consumer, for example

  • User-Api
  • Shop-Api
  • Goods-Api
  • Live-Api

Service is internal service and Api is external service.

clipboard.png
Picture sourcehttp://blog.csdn.net/suifeng3 …

The issues considered in service governance are how to manage these four services, how to make them serve the outside world, and how to monitor the service process.

Relying on realization

Before implementing service governance, we need to know the following knowledge points

  • thrift
  • rpc
  • swoole

thrift

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

rpc

RPC (Remote Procedure Call)-Remote Procedure Call is a protocol that requests services from remote computer programs over a network without understanding the underlying network technology. RPC protocol assumes the existence of some transmission protocols, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multi-programs.
RPC uses 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.

Communication between service and service can be through RPC communication, of course, UDP can also be selected

swoole

As a cross-century product in PHP circles, the reason for using it is that SWOLE itself supports RPC communication, so we use Swoole Rpc method to realize communication between consumers and service providers in this chapter.

RPC communication implemented

A very important part of service governance is to let consumer a call service providers A,B,C without awareness. of course, in actual situations, this is never possible. it is not in a memory space at all. we need to simulate this usage by ourselves.

$userSerivce = $client->client ('UserService');
 $result = $userSerivce->getUserInfo (['user_id' => 100]);
 var_dump($result);

There is no UserService and no getUserInfo method in the consumer. these are all in the service provider’s application. how to call them?

First, we intercept a non-existent method by php’s __call method

public function __call($name, $arguments)
 {
 $client = new ClientRpc($this->serviceName);
 
 $response = $client->send ($this->serviceName, $name, $arguments);
 
 return (json_decode ($response, true));
 
 }

After obtaining, call the send method written by yourself, and swoole will appear.

class ClientRpc
 {
 protected $client;
 
 public function __construct($service_name, $centerConfig)
 {
 $this->client = new \swoole_client(SWOOLE_SOCK_TCP);
 
 $center = Dispatcher::loadBalance ($service_name, $centerConfig);
 $this->client->connect ($center['ip'], $center['port'], 0.5);
 }
 
 public function send($service, $action, $arguments)
 {
 $request = new Request();
 
 $request->setService ($service);
 $request->setAction ($action);
 $request->setParameters ($arguments[0]);
 //Reorganize the parameters, combine them into the desired format, and finally convert them into json and send them to the service provider.
 $this->client->send (json_encode ((array)$request));
 
 return $this->client->recv ();
 }
 
 public function __destruct()
 {
 $this->client->close ();
 unset($this->client);
 }
 }

Thanks

Zhou Mengkang [https://mengkang.net/]