Without touching on other languages and tools, how do we implement service governance from PHP itself
This ape man has written good service governance github.com/CrazyCodes/…
Governing what?
The technical term is easy to see governing services, and services are our projects. Managing these service solutions is called service governance.
There are now four services on the Server, respectively
- UserService
- ShopService
- GoodsService
- LiveService
These services we call service providers (both providing applications for internal services)
The application that invokes the service is called a service consumer, for example
- User-Api
- Shop-Api
- Goods-Api
- Live-Api
A Service is an internal Service and an Api is an external Service
Service governance concerns how to manage these four services, how to make them external services, how to monitor the service process
Relying on the implementation
There are a few things you need to know before implementing service governance
- thrift
- rpc
- swoole
thrift
It can be used to call methods in other development languages
rpc
Remote Procedure Call (RPC) is a protocol that requests services from Remote computer programs over the network without understanding the underlying network technology. The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans both the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprograms.
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.
Services can communicate with each other through RPC or UDP
swoole
Swoole itself supports RPC communication, so in this chapter we use the Swoole RPC method to achieve communication between consumers and service providers
Implementing RPC Communication
A very important part of service governance is to let consumer A call service providers A,B,C without any awareness. Of course, in reality, this is never possible, not in A memory space at all. We need to simulate this way of use by ourselves
$userSerivce = $client->client ('UserService');
$result = $userSerivce->getUserInfo (['user_id' => 100]);
var_dump($result);
Copy the code
There is no UserService in the consumer, and there is no getUserInfo method. These are in the service provider’s application. How to call them?
Start by intercepting a nonexistent method through 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));
}
Copy the code
Once you get it, you call your own send method, and Swoole comes out
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]); $this->client->send (json_encode ((array)$request)); return $this->client->recv (); } public function __destruct() { $this->client->close (); unset($this->client); }}Copy the code
thanks
Zhou Mengkang [mengkang.net/]
communication
There is life, there is code.
Spread the positive energy of technology and continue to learn new knowledge.