royalcms/laravel-jsonrpc-client

json-rpc 客户端的一个 Laravel 扩展包

v1.3.3 2021-01-23 09:36 UTC

This package is auto-updated.

Last update: 2024-09-23 17:27:26 UTC


README

Laravel jsonrpc-client 操作

安装

可以通过 Composer 安装 royalcms/laravel-jsonrpc-client,在composer.json的require部分引入,然后执行 composer installcomposer update(注意:composer update会更新你其他没有固定版本的组件)。

{
    "require": {
       
        "royalcms/laravel-jsonrpc-client": "~1.0"
        
    }
   
}

或者

在项目根目录执行

composer require royalcms/laravel-jsonrpc-client

使用

要使用sys-audit-log服务提供程序,在引导Laravel应用程序时必须注册该提供程序。基本上有两种方法。

config/app.php中找到providers键并注册JsonRpcHttpClient服务提供程序。

Laravel 5.1+

'providers' => [
    // ...
    Royalcms\Laravel\JsonRpcClient\JsonRpcClientServiceProvider::class,
]

配置

将配置文件移动到根目录下的config文件夹中。

$ php artisan vendor:publish

config/rpc-services.php

return [
    'services' => [
        [
            'services' => [
                'CalculatorService',
                'ProductService',
            ],
            'nodes' => [
                ['host' => '127.0.0.1', 'port' => 9503, 'path' => '/rpc'],
                ['host' => '127.0.0.1', 'port' => 9503, 'path' => '/rpc']
            ]
        ]
    ]
];

使用

use Royalcms\Laravel\JsonRpcClient\JsonRpcHttpClient;

class a extends JsonRpcHttpClient {
    /**
     * The service name of the target service.
     *
     * @var string
     */
    protected $serviceName = 'ProductService';

    /**
     * The protocol of the target service, this protocol name
     *
     * @var string
     */
    protected $protocol = 'jsonrpc-http';


    // 实现一个加法方法,这里简单的认为参数都是 int 类型
    public function list($where,$select,$page,$perPage)
    {
        return $this->__request(__FUNCTION__,compact('where','select','page','perPage'));
    }

}

$a = new a();

$resp = $a->list(" id > 10001 and status > 0 ",['id'],1,10);