Home >Backend Development >PHP Tutorial >Detailed introduction to inter-service communication RPC
This article brings you a detailed introduction to RPC communication between services. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Microservices are now prevalent, and there are probably two ways to communicate between services: Api and Rpc.
The following two examples will let you understand the difference between Api and Rpc.
Lie Zi 1 Addition, deletion, modification and review of articles.
Api implementation:
Router::get('/article/{id}','ArticleController@get'); Router::post('/article','ArticleController@create'); Router::put('/article/{id}','ArticleController@edit'); Router::delete('/article/{id}','ArticleController@delete');
Then call the model in the controller Article
return Article::find($id)->toArray();
Rpc implementation
RpcServer::add('Article');
Yes, just one line of code
Liezi 2 calculator
If there is a calculator on machine A Counter is provided to other machines in the form of Rpc.
Calculator Counter code
class Counter { private $i = 0; public function __construct($i = 0) { $this->i = $i; } // 加法 public function add($v) { $this->i += $v; return $this; } // 减法 public function sub($v) { $this->i -= $v; return $this; } // 乘法 public function mul($v) { $this->i *= $v; return $this; } // 除法 public function p($v) { $this->i /= $v; return $this; } // 获取结果 public function get() { return $this->i; } }
Rpc implementation
RpcServer::add('Counter');
Rpc client call
$c = new ClientCounter(10); echo $c->add(3)->mul(2)->sub(10)->p(5)->get();
The above is the detailed content of Detailed introduction to inter-service communication RPC. For more information, please follow other related articles on the PHP Chinese website!