1. 模塊開發;
1.1 準備工作、測試基本接口;
啓動鏡像
# docker 啓動進入 Swoft 鏡像
docker run -it --name swoft_rpc -p 8308:18307 \
-v /data/php/test/swoft/Swoft_rpc/:/swoft \
-w /swoft rpc/swoft:2.0.6 sh
# 查看 PHP 版本
php bin/swoft -V
# 返回:PHP: 7.3.4, Swoft: 2.0.8, Swoole: 4.4.5
# 啓動 Swoft rpc
swoftcli run -c rpc:start -b bin/swoft
創建數據表
-- 數據庫: `myreader`
CREATE TABLE `course_kinds` (
`item_id` int(11) NOT NULL,
`kind_name` varchar(50) DEFAULT NULL COMMENT '類別名稱',
`pid` int(11) DEFAULT '0' COMMENT '父級ID',
`pids` text COMMENT '所有父級ID',
`ext1` varchar(1000) DEFAULT NULL COMMENT '擴展字段1',
`ext2` varchar(1000) DEFAULT NULL COMMENT '擴展字段2'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `course_main` (
`item_id` int(11) NOT NULL,
`course_title` varchar(200) DEFAULT NULL,
`course_ltitle` varchar(200) DEFAULT NULL,
`course_price` decimal(10,2) DEFAULT '0.00',
`course_disc` tinyint(4) DEFAULT '10' COMMENT '默認10',
`course_status` tinyint(4) DEFAULT '0' COMMENT '0審覈中 1已發佈 2已下架',
`course_intr` text,
`course_body` longtext,
`course_pubtime` datetime DEFAULT NULL,
`course_edittime` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `course_metas` (
`item_id` int(11) NOT NULL COMMENT '主鍵',
`course_id` int(11) DEFAULT NULL COMMENT '課程ID',
`meta_name` varchar(100) DEFAULT NULL COMMENT '元信息名稱 ',
`meta_value` text COMMENT '元信息內容'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='包含了課程點擊量、收藏數、附件名或其他自定義等等';
ALTER TABLE `course_kinds` ADD PRIMARY KEY (`item_id`);
ALTER TABLE `course_main` ADD PRIMARY KEY (`item_id`);
ALTER TABLE `course_metas` ADD PRIMARY KEY (`item_id`),
ADD UNIQUE KEY `AK_uniquekey` (`course_id`,`meta_name`);
ALTER TABLE `course_kinds` MODIFY `item_id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `course_main` MODIFY `item_id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `course_metas` MODIFY `item_id` int(11) NOT NULL AUTO_INCREMENT;
INSERT INTO `course_main` (`item_id`, `course_title`, `course_ltitle`, `course_price`, `course_disc`, `course_status`, `course_intr`, `course_body`, `course_pubtime`, `course_edittime`) VALUES
(1, 'test', 'test', 40.00, 10, 0, 'intr', 'body', NULL, NULL);
INSERT INTO `course_metas` (`item_id`, `course_id`, `meta_name`, `meta_value`) VALUES
(1, 1, 'click', '10');
COMMIT;
部署代碼
- 新建接口,之後拷貝給客戶端
Swoft_rpc\App\Rpc\Service\ICourceService.php
<?php
namespace App\Rpc\Lib;
interface ICourse {
public function list($size);
public function get($id);
}
- 新建
Swoft_rpc\App\Rpc\Lib\ICource.php
<?php
namespace App\Rpc\Service;
use App\Rpc\Lib\ICourse;
use Swoft\Rpc\Server\Annotation\Mapping\Service;
/**
* Class CourseService
* @package App\Rpc\Service
* @Service()
*/
class CourseService implements ICourse {
public function list($size) {
return ["list"];
}
public function get($id) {
return ["get"];
}
}
- 測試代碼:根目錄創建
test.php
<?php
# 參考:https://www.swoft.org/documents/v2/core-components/rpc-server/#-swoft-
const RPC_EOL = "\r\n\r\n";
function request($host, $class, $method, $param, $version = '1.0', $ext = []) {
$fp = stream_socket_client($host, $errno, $errstr);
if (!$fp) {
throw new Exception("stream_socket_client fail errno={$errno} errstr={$errstr}");
}
$req = [
"jsonrpc" => '2.0',
"method" => sprintf("%s::%s::%s", $version, $class, $method),
'params' => $param,
'id' => '',
'ext' => $ext,
];
$data = json_encode($req) . RPC_EOL;
fwrite($fp, $data);
$result = '';
while (!feof($fp)) {
$tmp = stream_socket_recvfrom($fp, 1024);
if ($pos = strpos($tmp, RPC_EOL)) {
$result .= substr($tmp, 0, $pos);
break;
} else {
$result .= $tmp;
}
}
fclose($fp);
return json_decode($result, true);
}
// 修改爲主機地址和端口號
$ret = request('tcp://192.168.60.221:8308', \App\Rpc\Lib\ICourse::class, 'get', [1], "1.0");
var_dump($ret);
- 測試結果
// 容器內啓動 RPC 服務,本地終端運行 php test.php
// 如下返回,說明接口已經測試通
array(2) {
["jsonrpc"]=>
string(3) "2.0"
["result"]=>
array(1) {
[0]=>
string(3) "get"
}
}