hyperf 创建 JSON RPC 服务
JSON RPC 服务
hyperf 框架为 PHP 打开了微服务的大门,而服务之间相互调用,又以 RPC 为基础。所以这个章节非常重要。但官方文档还是有些坑的,我以前就在这儿踩过坑。这里省略了接口类,只保留最主要的部分。
安装依赖
composer require hyperf/json-rpc
composer require hyperf/rpc-server
composer require hyperf/rpc-client
添加 JSON RPC Server 服务器
config/autoload/server.php
<?php
use Hyperf\Server\Server;
use Hyperf\Server\Event;
return [
    // 这里省略了该文件的其它配置
    'servers' => [
        [
            'name' => 'jsonrpc-http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9504,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
            ],
        ],
    ],
];
配置注册服务中心,可以选择使用 nacos 或者 consul,这里选择 nacos
composer require hyperf/service-governance-nacos
composer require hyperf/service-governance-consul
config/autoload/services.php 添加配置
<?php
return [
    'enable' => [
        'discovery' => true,
        'register' => true,
    ],
    'consumers' => [
         // 配置服务消提供者
         [
            'name' => 'UserService', //服务名称
            'service' => \App\JsonRpc\UserService::class,
            'protocol' => 'jsonrpc-http',
            'registry' => [
                'protocol' => 'nacos',
                'address' => 'http://127.0.0.1:8848',
            ],
        ],
    ],
    'providers' => [],
    'drivers' => [
        'nacos' => [
            'host' => '127.0.0.1',
            'port' => 8848,
            'username' => null,
            'password' => null,
        ],
    ],
];
定义服务提供者类
<?php
namespace App\JsonRpc;
use Hyperf\RpcServer\Annotation\RpcService;
// 注解中的的 publishTo 表示要发布的服务中心
#[RpcService(name: "UserService", protocol: "jsonrpc-http", server: "jsonrpc-http", publishTo:"nacos")]
class UserService {
    public function add(int $a, int $b):int
    {
        return $a + $b;
    }
}
定义服务消费者类
<?php
namespace App\JsonRpc;
use Hyperf\RpcClient\AbstractServiceClient;
class UserServiceClient extends AbstractServiceClient {
    protected string $serviceName = 'UserService';
    protected string $protocol = 'jsonrpc-http';
    public function add(int $a, int $b):int
    {
        return $this->__request(__FUNCTION__, compact('a', 'b'));
    }
}在控制器中使用务消费者类
<?php
declare(strict_types=1);
namespace App\Controller;
use App\JsonRpc\UserServiceClient;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
#[Controller]
class IndexController extends AbstractController
{
   // http://localhost:9501/index/test
    #[GetMapping(path: "test")]
    public function test()
    {
        $client = ApplicationContext::getContainer()->get(UserServiceClient::class);
        $sum = $client->add(13, 12);
        return $sum;
    }
}nacos 参考:
https://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html
直接下载 zip包,解压后,进入 bin 目录 执行 bash startup.sh -m standalone
启动服务
php bin/hyperf.php start
访问:http://localhost:9501/index/test

测试时,一定要保证你的 nacos 是启动成功的状态。

 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号