Home > Backend Development > PHP Tutorial > Detailed introduction to inter-service communication RPC

Detailed introduction to inter-service communication RPC

不言
Release: 2023-04-04 15:52:01
forward
4015 people have browsed it

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');
Copy after login

Then call the model in the controller Article

return Article::find($id)->toArray();
Copy after login

Rpc implementation

RpcServer::add('Article');
Copy after login

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;
    }
}
Copy after login

Rpc implementation

RpcServer::add('Counter');
Copy after login

Rpc client call

$c = new ClientCounter(10);
echo $c->add(3)->mul(2)->sub(10)->p(5)->get();
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template