royalcms/laravel-jsonrpc-server
Composer 安装命令:
composer require royalcms/laravel-jsonrpc-server
包简介
JsonRpc Server for Laravel Package.
README 文档
README
JSON-RPC 2.0
JSON-RPC 2.0 is a remote procedure call protocol encoded in JSON. It is a very simple protocol, defining only a few data types and commands. JSON-RPC allows for notifications (data sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered out of order.
Install
Require this package with composer using the following command:
$ composer require avto-dev/json-rpc-laravel "^2.0"
Installed
composeris required (how to install composer).
You need to fix the major version of package.
Usage example
Create routes
Register actions for your methods in ./routes/web.php using the facade RpcRouter:
<?php use AvtoDev\JsonRPC\RpcRouter; RpcRouter::on('please_sum_array_values', 'YourNamespace\\SomeController@sum'); RpcRouter::on('show_full_request', 'YourNamespace\\SomeController@showInfo');
This package already contains a simple controller implementation, which you can expand it or take it only as an example.
Add new route for RpcController:
<?php use Illuminate\Support\Facades\Route; Route::post('/rpc', 'AvtoDev\\JsonRpc\\Http\\Controllers\\RpcController');
Don't forget about to load RPC routes if you want to specify routes not in the file
./routes/web.php
Create RPC controller
Create a new controller containing procedures to be called by JSON request:
<?php namespace YourNamespace; use AvtoDev\JsonRpc\Requests\RequestInterface; class SomeController { /** * Get sum of array. * * @param RequestInterface $request * * @return int */ public function sum(RequestInterface $request): int { return (int) \array_sum($request->getParams()); } /** * Get info from request. * * @param RequestInterface $request * * @return mixed[] */ public function showInfo(RequestInterface $request): array { return [ 'params' => $request->getParams(), 'notification' => $request->isNotification(), 'method_name' => $request->getMethod(), 'request ID' => $request->getId(), ]; } }
Send RPC-request
$ curl 'http://localhost/rpc' \ -H 'Content-Type: application/json;charset=utf-8' \ --data '{"jsonrpc":"2.0","method":"please_sum_array_values","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}' { "jsonrpc": "2.0", "result": 55, "id": "UNIQ_REQUEST_ID" }
$ curl 'http://localhost/rpc' \ -H 'Content-Type: application/json;charset=utf-8' \ --data '{"jsonrpc":"2.0","method":"show_full_request","id":"UNIQ_REQUEST_ID","params":[1,2,3,4,5,6,7,8,9,10]}' { "jsonrpc": "2.0", "result": { "params": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "notification": false, "method_name": "please_sum_array_values", "request ID": "UNIQ_REQUEST_ID" }, "id": "UNIQ_REQUEST_ID" }
$ curl 'http://localhost/rpc' \ -H 'Content-Type: application/json;charset=utf-8' \ --data '{"jsonrpc":"2.0","method":"undefined_method","id":"UNIQ_REQUEST_ID"}' { "jsonrpc": "2.0", "error": { "code": -32601, "message": "Method not found" }, "id": "UNIQ_REQUEST_ID" }
Events
When the Kernel@handle method is called, some events are fired:
| Event class | Description |
|---|---|
ErroredRequestDetectedEvent |
Detected not valid request from stack |
RequestHandledEvent |
Means the remote method was successfully called |
RequestHandledExceptionEvent |
An exception was thrown while executing the request |
If necessary, you can create a Listener on this event and display received messages for debug.
The EventServiceProvider included with your Laravel application provides a convenient place to register all of your application's event listeners.
data.
ChangeLog
Changes log can be found here.
Support
If you will find any package errors, please, make an issue in current repository.
License
This is open-sourced software licensed under the MIT License.
royalcms/laravel-jsonrpc-server 适用场景与选型建议
royalcms/laravel-jsonrpc-server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 156 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「laravel」 「jsonrpc」 「royalcms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 royalcms/laravel-jsonrpc-server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 royalcms/laravel-jsonrpc-server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 royalcms/laravel-jsonrpc-server 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bitcoin JSON-RPC Service Provider for Laravel
A collection of server-side addons that might be of use for development of xml-rpc (and json-rpc) based applications
Kinikit - PHP Application development framework MVC component
JsonRpc Client for Laravel
ext-json wrapper with sane defaults
Flexible JSON RPC v2 client for PHP written in object-oriented style
统计信息
- 总下载量: 156
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-23