卡桑那 / 小鬼
Laravel 的 JSON-RPC 通信
Requires
- php: >=7.2
- clue/buzz-react: ^2.5
- datto/json-rpc: ^6.0
- illuminate/database: ^6.0 || ^7.0 || ^8.0
- illuminate/pipeline: ^6.0 || ^7.0 || ^8.0
- illuminate/validation: ^6.0 || ^7.0 || ^8.0
- laravie/codex-security: ^1.0
- laravie/stream: ^1.3
- nyholm/psr7: ^1.2
- orchestra/canvas-core: ^4.7 || ^5.0 || ^6.0
- react/event-loop: ^1.1
- react/promise: ^2.5
- symfony/psr-http-message-bridge: ^1.3 || ^2.0
Requires (Dev)
- clue/block-react: ^1.3
- clue/mq-react: ^1.2
- mockery/mockery: ^1.3.1
- orchestra/canvas: ^4.5 || ^5.0 || ^6.0
- orchestra/testbench: ^4.0 || ^5.0 || ^6.0
Suggests
- clue/block-react: Allow to use traditional, blocking environment with Minions (^1.3).
- clue/mq-react: Allow to limit concurrent JSON-RPC Client requests on Minions (^1.2).
- katsana/minions-polyfill: Allow to use Laravel Routing as RPC Server Polyfill (^1.0).
- katsana/minions-server: Allow to use ReactPHP as RPC Server (^1.0).
- dev-master / 3.x-dev
- 2.x-dev
- v2.6.2
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- v0.5.0
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- v0.0.2
- v0.0.1
This package is auto-updated.
Last update: 2024-09-15 12:52:34 UTC
README
安装
小鬼可以通过 composer 安装
composer require "katsana/minions"
设置
该包将自动注册服务提供者。
接下来,您需要发布小鬼配置文件
php artisan vendor:publish --provider="Minions\MinionsServiceProvider" --tag="config"
设置项目 ID
每个项目都需要一个唯一的 Project ID,用于识别授权的 RPC 请求。您可以在 config/minions.php
配置文件中设置项目 ID
<?php return [ // ... 'id' => 'project-id', // ... ];
配置项目
接下来,您需要设置项目客户端和服务器信息
<?php return [ // ... 'projects' => [ 'client-project-id' => [ 'token' => 'secret-token', 'signature' => 'secret-signature', ], 'server-project-id' => [ 'endpoint' => 'http://server-project-id:8084', 'token' => 'another-secret-token', 'signature' => 'another-secret-signature', ], ], // ... ];
endpoint
仅在从客户端项目配置服务器项目连接时需要。服务器永远不会向客户端项目发送请求。- 每个项目应有一对唯一的
token
和secret
,这将作为消息验证的一种形式仅由客户端和服务器共享。
安全性
token
用于请求的 Authorization
头部中的令牌,而 signature
用于签名通过请求发送的消息。
对于运行在私有内网上的项目,您可以通过将值设置为
null
而跳过设置token
和signature
。
请求处理器
为了接收来自客户端的请求,我们首先需要在服务器上创建一个请求处理器,例如,假设我们想要创建一个 Add
请求。
<?php namespace App\JsonRpc; use Minions\Http\Request; use Minions\Http\ValidatesRequests; class MathAdd { use ValidatesRequests; /** * Handle the incoming request. * * @param \Minions\Http\Request $request * * @return mixed */ public function __invoke(Request $request) { return \array_sum($request->all()); } /** * Authorize the incoming request. * * @param \Minions\Http\Request $request * * @return bool */ public function authorize(Request $request): bool { return true; } }
您可以使用
php artisan minions:make MathAdd
来生成基础存根文件App\JsonRpc\MathAdd
。
注册路由
要注册路由,您只需将请求处理器添加到 routes/rpc.php
<?php use Minions\Router; Router::rpc('math.add', 'App\JsonRpc\MathAdd');
您可以使用以下命令生成 routes/rpc.php
php artisan vendor:publish --provider="Minions\Http\MinionsServiceProvider" --tag="routes"
检查授权
您可以通过覆盖 authorize()
方法来检查项目是否有权使用请求。
/** * Authorize the incoming request. * * @param \Minions\Http\Request $request * * @return bool */ public function authorize(Request $request): bool { return $request->id() === 'platform'; // only allow access from `platform` }
验证请求
您可以使用 Laravel 验证来验证请求。 小鬼 还引入了 Minions\Http\ValidatesRequests
特性,您可以导入并公开 validate()
和 validateWith()
方法。例如
<?php namespace App\JsonRpc; use App\User; use Minions\Http\Request; use Minions\Http\ValidatesRequests; class User { use ValidatesRequests; /** * Handle the incoming request. * * @param \Minions\Http\Request $request * * @return mixed */ public function __invoke(Request $request) { $this->validate($request, [ 'email' => ['required', 'email'], ]); return User::where('email', '=', $request['email'])->firstOrFail(); } }
发送请求
要发送请求,您可以创建以下代码
<?php use Minions\Client\Message; use Minions\Client\ResponseInterface; use Minions\Minion; Minion::broadcast('server-project-id', Minion::message( 'math.add', [1, 2, 3, 4] ))->then(function (ResponseInterface $response) { assert(10, $response->getRpcResult()); }); Minion::run();
运行 RPC 服务器
要运行小鬼 RPC 服务器,您有两个选项
- 使用 katsana/minions-server 的 ReactPHP RPC 服务器
- 使用 katsana/minions-polyfill 的 Laravel 路由(填充)RPC 服务器
请查阅每个选项的文档,了解安装和使用指南。