30 Star 241 Fork 87

店滴云物联网开源框架 / ddiot-茶室民宿酒店棋牌室管理系统

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
http.md 3.26 KB
一键复制 编辑 原始数据 按行查看 历史
wangchunsheng 提交于 2024-04-07 01:45 . rpc增加数据库操作库

HTTP

配置

在根目录下的 .env
RPC_SERVER_IP = 127.0.0.1
RPC_SERVER_PORT = 8080

启动

建议在linux下使用
windows: swoole-cli rpc.php
linux: php rpc.php

扩展插件服务

common\rpc\AddonsRpcService.php

module 配置

以插件diandi_hotel为例,配置rpc模块
addons\diandi_hotel\rpc

代码示例

<?php

namespace addons\diandi_hotel\rpc;

use common\pdo\BaseAbstractServiceModule;use common\pdo\PdoPoolContainer;


class Device extends BaseAbstractServiceModule
{
    public static string $moduleName = 'Device';

    function moduleName(): string
    {
        return 'Device';
    }

    function ceshi()
    {
        $pool = PdoPoolContainer::getInstance()->get('pdoPool');
        $pdo = $pool->get();
        $statement = $pdo->prepare("select * from dd_user where id = ?");
        if (!$statement) {
            throw new \RuntimeException('Prepare failed');
        }

        $result = $statement->execute([11]);
        if (!$result) {
            throw new \RuntimeException('Execute failed');
        }
        $result = $statement->fetchAll();
//                if ($a + $b !== (int)$result[0][0]) {
//                    throw new \RuntimeException('Bad result');
//                }
    
        $pool->put($pdo);
        $this->response()->setMsg(['a'=>1221,'b'=>$result]);
    }


}

数据库连接池

最后的连接池释放很重要:$pool->put($pdo);
        $pool = PdoPoolContainer::getInstance()->get('pdoPool');
        $pdo = $pool->get();
        $statement = $pdo->prepare("select * from dd_user where id = ?");
        if (!$statement) {
            throw new \RuntimeException('Prepare failed');
        }

        $result = $statement->execute([11]);
        if (!$result) {
            throw new \RuntimeException('Execute failed');
        }
        $result = $statement->fetchAll();
//                if ($a + $b !== (int)$result[0][0]) {
//                    throw new \RuntimeException('Bad result');
//                }
    
        $pool->put($pdo);

缓存连接池

最后的连接池释放很重要:$pool->put($redis);
            $pool = PdoPoolContainer::getInstance()->get('redisPool');

            $redis = $pool->get();
            $result = $redis->set('foo', 'bar');
            if (!$result) {
                throw new RuntimeException('Set failed');
            }
            $result = $redis->get('foo');
            if ($result !== 'bar') {
                throw new RuntimeException('Get failed');
            }
            $pool->put($redis);

请求地址

http://127.0.0.1:8080/addons/device/ceshi

addons rpc服务名称

device rpc模块名称

ceshi rpc模块方法名称

其他服务

可以在 rpc模块方法 ceshi 中调用
use Simps\MQTT\Message\SubAck;
use Simps\MQTT\Protocol\ProtocolInterface;

$codes = [0];
$message_id = 8520;

$ack = new SubAck();
$ack->setCodes($codes)
    ->setMessageId($message_id);

$ack_data = $ack->getContents();
$ack_data = (string) $ack;

// MQTT5
$ack->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0)
    ->setCodes($codes)
    ->setMessageId($message_id)
    ->setProperties([
        'will_delay_interval' => 60,
        'message_expiry_interval' => 60,
    ]);

$ack_data = $ack->getContents();
$ack_data = (string) $ack;
PHP
1
https://gitee.com/wayfirer/ddiot.git
git@gitee.com:wayfirer/ddiot.git
wayfirer
ddiot
ddiot-茶室民宿酒店棋牌室管理系统
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891