phluxor/phluxor-remote

phluxor/actor model 的远程库

资助包维护!
ytake

0.1.1 2024-09-12 05:47 UTC

This package is auto-updated.

Last update: 2024-09-12 05:47:40 UTC


README

关于

Phluxor Remote 是一个用于创建利用 Phluxor 动态系统进行远程动态系统开发的库。

Phluxor Remote 是一个库,允许你在 Phluxor 中创建远程动态系统。

它受到了 Proto.Remote 的启发。

使用 Phluxor Remote,你可以创建一个可以通过网络与其他动态系统通信的远程动态系统。

Phluxor Remote 使用 Swoole

两个节点之间使用 ProtoBuf 序列化格式。
使用 WebSocket 作为传输层。

使用方法

安装

$ composer require phluxor/phluxor-remote

节点1

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Phluxor\ActorSystem;
use Phluxor\Remote\Config;
use Phluxor\Remote\Kind;
use Phluxor\Remote\Remote;
use Test\ProtoBuf\HelloRequest;
use Test\ProtoBuf\HelloResponse;

\Swoole\Coroutine\run(function () {
    \Swoole\Coroutine\go(function () {
        $system = ActorSystem::create();
        $config = new Config('localhost', 50053, Config::withUseWebSocket(true));
        $remote = new Remote($system, $config);
        $remote->start();
        $props = ActorSystem\Props::fromFunction(
            new ActorSystem\Message\ReceiveFunction(
                function (ActorSystem\Context\ContextInterface $context) {
                    $message = $context->message();
                    if ($message instanceof HelloRequest) {
                        $context->respond(new HelloResponse([
                            'Message' => 'Hello from remote node',
                        ]));
                    }
                }
            )
        );
        $system->root()->spawnNamed($props, 'hello');
    });
});

节点2

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Phluxor\ActorSystem;
use Phluxor\Remote\Config;
use Phluxor\Remote\Kind;
use Phluxor\Remote\Remote;
use Test\ProtoBuf\HelloRequest;
use Test\ProtoBuf\HelloResponse;

\Swoole\Coroutine\run(function () {
    \Swoole\Coroutine\go(function () {
        $system = ActorSystem::create();
        $config = new Config('localhost', 50052, Config::withUseWebSocket(true));
        $remote = new Remote($system, $config);
        $remote->start();
        $future = $system->root()->requestFuture(
            new ActorSystem\Ref(new ActorSystem\ProtoBuf\Pid([
                'address' => 'localhost:50053',
                'id' => 'hello',
            ])),
            new HelloRequest(),
            1
        );
        $r = $future->result()->value();
        $r->getMessage(); // Hello from remote node! 
    });
});