手把手編寫hyperf JsonRpc demo


1 服務端 provider.php

composer.json文件

"require": {
        "php": ">=7.2",
        "ext-swoole": ">=4.4",
        "hyperf/cache": "~1.1.0",
        "hyperf/command": "~1.1.0",
        "hyperf/config": "~1.1.0",
        "hyperf/contract": "~1.1.0",
        "hyperf/database": "~1.1.0",
        "hyperf/db-connection": "~1.1.0",
        "hyperf/devtool": "~1.1.0",
        "hyperf/di": "~1.1.0",
        "hyperf/dispatcher": "~1.1.0",
        "hyperf/event": "~1.1.0",
        "hyperf/exception-handler": "~1.1.0",
        "hyperf/framework": "~1.1.0",
        "hyperf/guzzle": "~1.1.0",
        "hyperf/http-server": "~1.1.0",
        "hyperf/logger": "~1.1.0",
        "hyperf/memory": "~1.1.0",
        "hyperf/paginator": "~1.1.0",
        "hyperf/pool": "~1.1.0",
        "hyperf/process": "~1.1.0",
        "hyperf/redis": "~1.1.0",
        "hyperf/utils": "~1.1.0",
        "hyperf/json-rpc": "~1.1.0",
        "hyperf/rpc": "~1.1.0",
        "hyperf/rpc-client": "~1.1.0",
        "hyperf/rpc-server": "~1.1.0",
        "hyperf/service-governance": "~1.1.0",
        "hyperf/config-apollo": "~1.1.0",
        "hyperf/constants": "~1.1.0",
        "hyperf/async-queue": "~1.1.0",
        "hyperf/amqp": "~1.1.0",
        "hyperf/model-cache": "~1.1.0",
        "hyperf/elasticsearch": "~1.1.0",
        "hyperf/tracer": "~1.1.0",
        "hyperf/consul": "^1.1",
        "hyperf/circuit-breaker": "^1.1",
        "hyperf/rate-limit": "^1.1"
    },

consul.php

<?php

return [
    'uri' => 'http://127.0.0.1:8500',
];

dependencies.php

<?php declare(strict_types=1);

/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

use Hyperf\JsonRpc\JsonRpcPoolTransporter;
use Hyperf\JsonRpc\JsonRpcTransporter;

return [
    JsonRpcTransporter::class => function () {
        return make(JsonRpcPoolTransporter::class);
    },
];

 

server.php

'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9601,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'jsonrpc-http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9604,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'jsonrpc',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 9603,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
            ],
            'settings' => [
                'open_eof_split' => true,
                'package_eof' => "\r\n",
            ],
        ],
    ],

新建服務

CalculatorService.php
<?php

namespace App\JsonRpc;

use Hyperf\RpcServer\Annotation\RpcService;

/**
 * 注意,如希望通過服務中心來管理服務,需在注解內增加 publishTo 屬性
 * @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http",publishTo="consul")
 */
class CalculatorService implements CalculatorServiceInterface
{
    // 實現一個加法方法,這里簡單的認為參數都是 int 類型
    public function add(int $a, int $b): int
    {
        $add =  $a + $b;
        var_dump($add);
        // 這里是服務方法的具體實現
        return $add;
    }
}
CalculatorServiceInterface.php
<?php

namespace App\JsonRpc;


/**
 * Class CalculatorService
 * @package App\JsonRpc
 */
interface CalculatorServiceInterface
{
    public function add(int $a, int $b): int;
}

2 消費端

consul.php

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

return [
    'uri' => 'http://127.0.0.1:8500',
];

dependencies.php

<?php declare(strict_types=1);

/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

use Hyperf\JsonRpc\JsonRpcPoolTransporter;
use Hyperf\JsonRpc\JsonRpcTransporter;

return [
    JsonRpcTransporter::class => function () {
        return make(JsonRpcPoolTransporter::class);
    },
];

server.php

'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'jsonrpc-http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9504,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'jsonrpc',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 9503,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                SwooleEvent::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
            ],
            'settings' => [
                'open_eof_split' => true,
                'package_eof' => "\r\n",
            ],
        ],
    ],

services.php

<?php

use Hyperf\Server\Server;
use Hyperf\Server\SwooleEvent;

return [
    'consumers' => [
        [
            // name 需與服務提供者的 name 屬性相同
            'name' => 'CalculatorService',
            // 服務接口名,可選,默認值等於 name 配置的值,如果 name 直接定義為接口類則可忽略此行配置,如 name 為字符串則需要配置 service 對應到接口類
            'service' => \App\JsonRpc\CalculatorServiceInterface::class,
            // 對應容器對象 ID,可選,默認值等於 service 配置的值,用來定義依賴注入的 key
            'id' => \App\JsonRpc\CalculatorServiceInterface::class,
            // 服務提供者的服務協議,可選,默認值為 jsonrpc-http
            'protocol' => 'jsonrpc-http',
            // 負載均衡算法,可選,默認值為 random
            'load_balancer' => 'random',
            // 這個消費者要從哪個服務中心獲取節點信息,如不配置則不會從服務中心獲取節點信息
            'registry' => [
                'protocol' => 'consul',
                'address'  => 'http://127.0.0.1:8500',
            ],
            // 如果沒有指定上面的 registry 配置,即為直接對指定的節點進行消費,通過下面的 nodes 參數來配置服務提供者的節點信息
            'nodes' => [
                ['host' => '127.0.0.1', 'port' => 9604],
            ],
        ]
    ],
];

消費服務
<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use Hyperf\Utils\ApplicationContext;
use App\JsonRpc\CalculatorServiceInterface;

class IndexController extends AbstractController
{

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        $client = ApplicationContext::getContainer()->get(CalculatorServiceInterface::class);
        $result = $client->add(1, 2);

        var_dump($result);
        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}
 
         

啟動服務 啟動消費  啟動consul  

完成

 


免責聲明!

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



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