定制 vzina/jsonrpc 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vzina/jsonrpc

Composer 安装命令:

composer require vzina/jsonrpc

包简介

Webman plugin vzina/jsonrpc

README 文档

README

定义服务端

服务进程配置 process.php

return [
    'jsonrpc' => [
        'handler' => Vzina\Jsonrpc\Server::class,
        // 服务端协议配置 => 客户端协议配置
        // 'json://0.0.0.0:8788' => 'json://127.0.0.1:8788'
        // 'http://0.0.0.0:8788' => 'http://127.0.0.1:8788'
        'listen' => 'http://0.0.0.0:8788',
        'count' => cpu_count() * 4,
        'user' => '',
        'group' => '',
        'reusePort' => false,
        'eventLoop' => Workerman\Events\Fiber::class, // 建议使用协程功能
        'context' => [],
        'constructor' => [
            'requestClass' => Vzina\Jsonrpc\Request::class,
            'logger' => support\Log::channel(),
            'appPath' => app_path()
        ]
    ]
]
namespace app\Service;

use Vzina\Jsonrpc\Attribute\RpcServer;

#[RpcServer]
class TestService
{
    public function rpc(): array
    {
        return ['test', time()];
    }
}

定义客户端

namespace app\Client;

use support\Container;
use Vzina\Jsonrpc\Ast\AbstractProxyService;use Vzina\Jsonrpc\ServiceClient;

class TestService
{
    protected ServiceClient $serviceClient;
    
    public function __construct() 
    {
        $this->serviceClient = new ServiceClient(static::class, [
            'address' => ['http://127.0.0.1:8788'],
            'timeout' => 60,
            'pool' => [
                'max_connections' => 10,
            ]
        ]);
    }

    public function rpc(): array
    {
        return $this->serviceClient->__call(__FUNCTION__, func_get_args());
    }
}

// 或
class TestService extends AbstractProxyService
{
    public function rpc(): array
    {
        return $this->client->__call(__FUNCTION__, func_get_args());
    }
}

// 修改配置文件 rpc_services.php
[
    'name' => TestService::class, // 指定客户端类
    // ... other
]

// 使用
$r = Container::get(TestService::class)->rpc();
var_dump($r);

定义自动客户端

namespace app\contracts;

interface TestServiceInterface
{
    public function rpc(): array;
}

// 修改配置文件 rpc_services.php
[
    'auto_services' => app_path('contracts'), // 指定客户端扫描目录
    // ... other
]

// 使用
$r = Container::get(TestServiceInterface::class)->rpc();
var_dump($r);

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-31