vzina / jsonrpc
Webman plugin vzina/jsonrpc
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/vzina/jsonrpc
Requires
- php: >=8.1
- symfony/property-access: ~6.0
- symfony/serializer: ~6.0
- vzina/attributes: dev-master
This package is auto-updated.
Last update: 2026-01-08 10:29:46 UTC
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);