skolodyazhnyy/message-bus-client

该包已被废弃且不再维护。未建议替代包。

简单的 Magento 消息总线客户端

0.2.0 2016-08-04 15:05 UTC

This package is not auto-updated.

Last update: 2020-03-20 17:36:36 UTC


README

这是一个非常基础的 Magento 服务合同客户端。它允许通过 AMQP 或 HTTP 协议发布和消费来自 Magento 消息总线的消息。它非常适合快速原型设计,因为它使用 PHP 数组而不是对象,支持多个版本和多个协议。

用法

以下有一些如何组合事物的示例,但你可以在 examples 文件夹中找到更多完整的示例。

定义服务

// Create a client
$bus = new ServiceBus($driver);

// Define a new service "calc"
$service = $bus->define('calc');

// Bind methods to the service
$service->bind(
    CallbackBinding::make()
        ->on('add', '1.0', function(Request $request) {
            return new Response($request->getArgument('a') + $request->getArgument('b'));
        })
);

// Call method of the service
// You can also call method asynchronously using $service->publish(...), but you won't receive direct response
$promise = $service->call(new Request('add', '1.0', ['a' => 10, 'b' => 65]));

// Resolve response
$response = $promise->resolve();

// Receive result
$response->getValue(); // gives 75

调用远程服务

$bus = new ServiceBus($driver);

// Fetch service endpoint
$endpoint = $bus->discover('calc');

// Call method of the service
// You can also call method asynchronously using $endpoint->publish(...), but you won't receive direct response
$promise = $endpoint->call(new Request('add', '1.0', ['a' => 10, 'b' => 65]));

// Resolve response and receive result
$promise->resolve()->getValue(); // gives 75

使用 Promise 回退

如果你想要构建真正可靠的软件,你需要考虑在同步调用失败时有一个回退。Promise 允许你设置一个简单的回调,在 RPC 调用失败时,这个回调会被调用以获取“回退”结果。

$promise = $endpoint->call(...);

$result = $promise->fallback(function(\Exception $exception) {
       // You may build different behaviour based on $exception
       
       return $this->storeage->get('call_result');
    })
    ->resolve(1);
    
$this->storeage->save('call_result', $result);

其他示例