企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Swoole高性能共享内存 Table 先定结构在进行操作数据(原生swoole操作): ``` //创建表 $table = new Swoole\Table(1024); $table->column("id", Swoole\Table::TYPE_INT); $table->column("name", Swoole\Table::TYPE_STRING); $table->column("money", Swoole\Table::TYPE_FLOAT); $table->create(); //添加数据 $table->set("zq", [ 'id' => 1, 'name' => "zhiqiang", 'money' => 100, ]); //获取一行数据 $table->get("zq"); // 修改数据 // 字段递增 $table->incr("zq","money",2); //递减 $table->decr("zq","money",2); // 返回 table 中存在的条目数。 $table->count(); //遍历table中的数据 foreach($table as $item){ var_dump($item); } ``` think-swoole中的操作: 先对table表结构进行初始化config\swoole.php ``` 'tables' => [ 'user'=>[ 'size'=>1024, 'columns'=>[ [ 'name'=>'id', 'type'=>\Swoole\Table::TYPE_INT ], [ 'name'=>'name', 'type'=>\Swoole\Table::TYPE_STRING, 'size'=>32 ], [ 'name'=>'money', 'type'=>\Swoole\Table::TYPE_FLOAT ], ], ], ], ``` 操作数据: ``` $table = app('swoole.table.user'); $table->set("zq", [ 'id' => 1, 'name' => "zhiqiang", 'money' => 100 ]); //获取一行数据 $table->get("zq"); // 修改数据 // 字段递增 $table->incr("zq", "money", 2); //递减 $table->decr("zq", "money", 2); // 返回 table 中存在的条目数。 $table->count(); //遍历table中的数据 foreach ($table as $item) { var_dump($item); } // 检查 table 中是否存在某一个 key。 $table->exist('zq'); //获取实际占用内存尺寸,单位字节 $table->momorySize(); ``` RPC RPC(Remote Procedure Call):远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的思想。 详细介绍:https://developer.51cto.com/a... 解决分布式系统中,服务之间的调用问题。 远程调用时,要能够像本地调用一样方便,让调用者感知不到远程调用的逻辑。 节点角色说明: Server: 暴露服务的服务提供方 Client: 调用远程服务的服务消费方 Registry: 服务注册与发现的注册中心 think-swoole实现RPC功能 服务器端 接口定义app/rpc/interfaces/UserInterface.php ``` <?php namespace app\rpc\interfaces; interface UserInterface { public function create(); public function find(int $id); } ``` 实现接口app/rpc/services/UserService.php ``` <?php namespace app\rpc\services; use app\rpc\interfaces\UserInterface; class UserService implements UserInterface { public function create() { // TODO: Implement create() method. return "service create success"; } public function find(int $id) { // TODO: Implement find() method. return $id. "查询数据遍历"; } } ``` 注册rpc服务config/swoole.php ``` 'rpc' => [ 'server' => [ //开启rpc服务 'enable' => true, //rpc端口 'port' => 9000, 'services' => [ //注册服务 \app\rpc\services\UserService::class ], ], // 如果填写也是可以调用其他服务端 'client' => [ ], ], ``` 启动服务端:php think swoole start / php think swoole:rpc 客户端 ``` 'rpc' => [ 'server' => [ ], 'client' => [ 'tp6'=>[ //服务端的ip地址 'host'=>'127.0.0.1', //服务端对应的端口 'port'=>'9000' ] // 更多服务端 ], ], ``` 运行php think rpc:interface生成RPC接口文件app\rpc.php ``` ?php /** * This file is auto-generated. */ declare(strict_types=1); namespace rpc\contract\tp6; interface UserInterface { public function create(); public function find(int $id); } return ['tp6' => ['rpc\contract\tp6\UserInterface']]; ``` 在控制器调用 ``` public function index(\rpc\contract\tp6\UserInterface $user) { // $user->find(1); //$user->create(); } ```