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

Time:2020-10-28

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

Without involving other languages and tools, we will talk about how to realize service governance from PHP itself

This ape man has written a good service governance https://github.com/CrazyCodes/Service-Govern.git

Governing what?

It is easy to find that the governance is service, and service is our project. Managing these service solutions is called service governance.

Now there are four services on the server, which are

  • UserService
  • ShopService
  • GoodsService
  • LiveService

These services are called service providers

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 while API is external service

The problem of service governance is how to manage these four services, how to provide external services 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

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

rpc

RPC (remote procedure call) – remote procedure call, it is a kind of protocol that requests service from remote computer program through network without knowing the underlying network technology. RPC Protocol assumes the existence of some transport protocols, such as TCP or UDP, to carry information between communication programs. In OSI network communication model, RPC crosses the transport layer and application layer. RPC makes it easier to develop applications including network distributed multiprogramming.
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.

Services can communicate with each other through RPC or UDP

swoole

It is a cross century product in PHP circle. The reason why it is used is because swote supports RPC communication. In this chapter, we use the method of swote RPC to realize the communication between consumers and service providers

Realize RPC communication

A very important part of service governance is to let consumer a call service providers a, B, C without perception. Of course, in reality, this is never possible. It is not in a memory space at all. We need to simulate this usage mode ourselves


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

There is no userservice or getUserInfo method in the consumer. These are all in the application of the service provider. How to call them?

First of all, through PHP’s__ Call method to intercept a nonexistent 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 oneself, swoole appears.

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 format you want, and send them to the service provider in JSON
    $this->client->send (json_encode ((array)$request));
      
    return $this->client->recv ();
  }
    
  public function __destruct()
  {
    $this->client->close ();
    unset($this->client);
  }
}

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.