Hyperf jsonrpc 服務的搭建


JSON-RPC,是一個無狀態且輕量級的遠程過程調用(RPC)傳送協議,其傳遞內容透過  JSON 為主。
我們需要布置兩台服務,一台Provider提供服務,另外一台Consumer消費服務
 

准備工作

1、所需類庫

composer require hyperf/json-rpc
composer require hyperf/rpc-server
composer require hyperf/rpc-client
composer require hyperf/consul
composer require hyperf/service-governance

2、工具

Mac環境下
安裝consul   brew install consul 
啟動consul   brew services start consul
 

3、Provider的配置 

1、服務提供類
<?php
namespace App\Rpc;
use Hyperf\RpcServer\Annotation\RpcService;
/**
* @RpcService(name="CalculatorService",protocol="jsonrpc-http",server="jsonrpc-http")
*/
class CalculatorService implements CalculatorServiceInterface
{
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
public function minus(int $a, int $b): int { return $a - $b; } } 
2、接口類
<?php
namespace App\Rpc;
interface CalculatorServiceInterface
{
    public function add(int $a, int $b): int;
    public function minus(int $a, int $b): int;
} 
 
 
3、配置文件 service.php
'servers' => [
    ...
    [
        'name' => 'jsonrpc-http',
        'type' => Server::SERVER_HTTP,
        'host' => '0.0.0.0',
        'port' => 9802,
        'sock_type' => SWOOLE_SOCK_TCP,
        'callbacks' => [
            SwooleEvent::ON_REQUEST => [Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
        ],
    ],
],

 

 

3、Consumer 的配置 

1、接口類
<?php
namespace App\Rpc;
interface CalculatorServiceInterface { public function add(int $a, int $b): int; public function minus(int $a, int $b): int; } 
 
2、配置文件 services.php
return [
    'consumers' => [
        [
            'name'    => 'CalculatorService',
            'service' => \App\Rpc\CalculatorServiceInterface::class,
            'nodes'   => [
                [
                   'host' => '0.0.0.0',
                    'port' => 9802
                ],
            ],
        ],
    ],
]; 
3、調用

 

<?php
namespace App\Controller;
use App\Rpc\CalculatorServiceInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
/**
 * @AutoController()
 */
class IndexController extends AbstractController
{
    /**
     * @Inject()
     * @var CalculatorServiceInterface
     */
    private $calculatorService;

    public function rpc()
    {
        return $this->calculatorService->minus(10,2);
    }
}

 

4、使用consul 

如果需要使用 consul來管理服務,則需要做如下操作

1).修改Provider具體的服務類,注解添加屬性 publishTo
/**
* @RpcService(name="CalculatorService",protocol="jsonrpc-http",server="jsonrpc-http",publishTo="consul") 
*/
2).Provider服務發布
php bin/hyperf.php vendor:publish hyperf/consul

執行完畢后會形成一個配置文件 consul.php

3).Conusmer修改配置

return [
    'consumers' => [
        [
            'name'    => 'CalculatorService',
            'service' => \App\Rpc\CalculatorServiceInterface::class,
       registry' => [

'protocol' => 'consul',
 'address' => 'http://127.0.0.1:8500',//對應Provider 中 consul.php配置項 ]
     ], 
  ]
,
];

 

4)重新啟動Provider和Consumer

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM