店滴多商户开源框架 2024-04-07 11:51:49 27次浏览 0条评论 0 0 0

代码仓库:yii真正的微服务框架

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;

数据库操作

分页查询


PdoQuery::getInstance()->findPaginated('dd_user',1,11);

本地服务器调用rpc

本地服务器默认rpc服务端口为 9600
<?php
/**
 * This file is part of EasySwoole.
 *
 * @link https://www.easyswoole.com
 * @document https://www.easyswoole.com
 * @contact https://www.easyswoole.com/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

$data = [
    'service' => 'ServiceOne', // 需要调用的服务名称
    'module'  => 'ModuleOne', // 需要调用的服务下的子模块名称
    'action'  => 'action',  // 需要调用的服务下的子模块的方法名称
    'arg'     => ['a', 'b', 'c'], // 需要传递的参数
];

$raw = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

// tcp://127.0.0.1:9600(示例请求地址) 是 rpc 服务端的地址,这里是本地,所以使用 127.0.0.1
// 开发者需要根据实际情况调整进行调用
$fp = stream_socket_client('tcp://127.0.0.1:9600');
fwrite($fp, pack('N', strlen($raw)) . $raw); // pack 数据校验

$try = 3;
$data = fread($fp, 4);
if (strlen($data) < 4 && $try > 0) {
    $data .= fread($fp, 4);
    $try--;
    usleep(1);
}

// 做长度头部校验
$len = unpack('N', $data);
$data = '';
$try = 3;
if (strlen($data) < $len[1] && $try > 0) {
    $data .= fread($fp, $len[1]);
    $try--;
    usleep(1);
}

if (strlen($data) != $len[1]) {
    echo 'data error';
} else {
    $data = json_decode($data, true);
    // 这就是服务端返回的结果
    var_dump($data);
}

fclose($fp);
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册