基于workerman-jsonrpc二次封装的一个package

简介#

think-workerman-jsonrpc 是基于 workerman-jsonrpc 二次封装的一个 package。里面有 rpc 客户端,rpc 服务端和 rpc 调用监控服务系统

安装#

通过 composer#

composer require streetlamp/think-workerman-jsonrpc

服务端使用说明#

需要在 linux 环境下启动

服务#

在项目根目录新建 server.php 启动文件

#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
new \streetlamp\rpcServer\RpcServer();

服务配置说明#

服务配置文件为 /src/server_config.php,内容如下:

<?php
 return [
            //服务端配置
            'rpc_server' => [
                //worker进程数
                'processes'         => 1,
                //通信协议
                'protocol'          => '\streetlamp\rpcServer\JsonNL',
                //地址
                'host'              => '0.0.0.0',
                //端口
                'port'              => 2015,
                'socket'            => '',
                //统计数据的协议地址
                'statistic_address' => 'udp://127.0.0.1:9200',
                //服务进程名
                'worker_name'       => 'vitec',
                //日志路径
                'log_file'          => RUNTIME_PATH . 'workerman/log.log',
                //服务
                'service'           => [
                    'User' => \app\test\logic\User::class
                ]
            ]
        ];

实例化 new \streetlamp\rpcServer\RpcServer () 对象时可以传入自定义的配置文件绝对路径,或者直接传入一个数组。

一、传入绝对路径#

#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
new \streetlamp\rpcServer\RpcServer("/var/www/html/application/config.php");

二、传入数组#

#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
 $config = [
            'rpc_server' => [
                'processes'         => 1,
                'protocol'          => '\streetlamp\rpcServer\JsonNL',
                'host'              => '0.0.0.0',
                'port'              => 2015,
                'socket'            => '',
                'statistic_address' => 'udp://127.0.0.1:9200',
                'worker_name'       => 'vitec',
                'log_file'          => RUNTIME_PATH . 'workerman/log.log',
                'service'           => ['User' => \app\test\logic\User::class]
            ]
        ];
new \streetlamp\rpcServer\RpcServer($config);

命令说明#

一、守护进程启动#

php server.php start -d

二、重启启动#

php server.php restart

三、平滑重启 / 重新加载配置#

php server.php reload

四、查看服务状态#

php server.php status

五、停止#

php server.php stop

thinkPHP5.0 框架使用例子#

新增启动服务文件 server.php,在项目根目录

#!/usr/bin/env php
<?php
define('APP_PATH', __DIR__ . '/application/');
define('BIND_MODULE', 'rpc/RpcServer');
// 加载框架引导文件
require __DIR__ . '/thinkphp/start.php';

新增服务类

<?php

namespace app\rpc\controller;

class RpcServer extends \streetlamp\rpcServer\RpcServer
{
}

然后直接执行 php server.php start 则可开启服务。

客户端使用说明#

客户端同步调用#

<?php
// User对应服务端配置中service里面的映射类,$config为配置文件的绝对路径或数组

$config = 'client_config.php';

$user_client = streetlamp\rpcClient\RpcClient::instance('User',$config);

// getInfoByUid对应User类中的getInfoByUid方法

$ret_sync = $user_client->getInfoByUid($uid);

客户端异步调用#

调用的方法添加”asend_” 前缀,接收数据时添加”arecv_” 前缀。

<?php
// User对应服务端配置中service里面的映射类,$config为配置文件的绝对路径或数组

$config = 'client_config.php';

$user_client = streetlamp\rpcClient\RpcClient::instance('User',$config);

// getInfoByUid对应User类中的getInfoByUid方法
//异步调用User::getInfoByUid方法
user_client = $user_client->asend_getInfoByUid($uid);

这里是其它的业务代码
....................
....................

// 需要数据的时候异步接收数据
$ret_async2 = $user_client->arecv_getInfoByUid($uid);

客户端配置说明#

客户端配置文件为 /src/client_config.php,内容如下:

<?php

//客户端配置
return [
 //驱动方式
 'type'               => 'workerman', 
 //服务端连接池
 'rpc_server_address' => [ 'tcp://127.0.0.1:2015' ], 
 //重连次数
 'reconnect_count'    => 1
];

$user_client = streetlamp\rpcClient\RpcClient::instance (‘User’,$config);$config 可以传入自定义的配置文件绝对路径,或者直接传入一个数组。

一、传入绝对路径#

<?php
$user_client = streetlamp\rpcClient\RpcClient::instance('User',"/var/www/html/application/config.php");

二、传入数组#

<?php
$user_client = streetlamp\rpcClient\RpcClient::instance(
            'User',
            [
                'type'               => 'workerman',
                'rpc_server_address' => ['tcp://127.0.0.1:2015'],
                'reconnect_count'    => 1
            ]
        );

统计服务端使用说明#

需要在 linux 环境下启动

服务#

在项目根目录新建 statistic.php 启动文件

#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
new \streetlamp\Statistics\StatisticsServer();

统计服务端配置说明#

统计服务端配置文件为 /src/statistics_server_config.php,内容如下:

<?php
//统计服务端配置
return [
     //web页面端口
     'web_port'        => 55757, 
     //接收统计数据端口
     'statistics_port' => 9200, 
     //web页面域名配置
     'host'            => ''
 ];

实例化 new \streetlamp\Statistics\StatisticsServer (); 对象时可以传入自定义的配置文件绝对路径,或者直接传入一个数组。

一、传入绝对路径#

#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
new \streetlamp\Statistics\StatisticsServer("/var/www/html/application/config.php");

二、传入数组#

#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
$config = [
   //web页面端口
  'web_port' => 55757,
  //接收统计数据端口
  'statistics_port' => 9200,
  //web页面域名配置
  'host' => ''
];
new \streetlamp\Statistics\StatisticsServer($config);

命令说明#

一、守护进程启动#

php statistic.php start -d

二、重启启动#

php statistic.php restart

三、平滑重启 / 重新加载配置#

php statistic.php reload

四、查看服务状态#

php statistic.php status

五、停止#

php statistic.php stop

统计监控页面#

访问地址:http://ip:55757 (端口为配置文件中的 web 页面端口)

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。