<?php require_once __DIR__ . '/../vendor/autoload.php'; use ServiceBus\Broadcaster\NullBroadcaster; use ServiceBus\Discovery\NullDiscovery; use ServiceBus\Identity; use ServiceBus\Service\Handling\CallableHandler; use ServiceBus\Service\Middleware\Delegate\Router\SimpleRouter; use ServiceBus\Service\Middleware\RoutingMiddleware; use ServiceBus\ServiceBus; use ServiceBus\Protocol\AMQP; $env = function ($key, $default = null) { return isset($_SERVER[$key]) ? $_SERVER[$key] : $default; }; // Driver only matter for sending requests. This example shows how to handle requests, // so discovery and broadcaster are not important. $bus = new ServiceBus(new NullDiscovery(), new NullBroadcaster()); // Define a service and few methods $service = $bus->define(new Identity('calc'))->apply(new RoutingMiddleware(new SimpleRouter(['add' => function ($a, $b) { return $a + $b; }, 'sub' => function ($a, $b) { return $a - $b; }, 'div' => function ($a, $b) { return $a / $b; }]))); $connection = \ButterAMQP\ConnectionBuilder::make()->create($env('AMQP_URL', 'amqp://*****:*****@localhost/%2f')); // Create topology $topology = (new AMQP\Topology($connection))->addExchange(new AMQP\Topology\Exchange('calc', 'topic'))->addQueue(new AMQP\Topology\Queue('calc', 0, [new AMQP\Topology\Binding('calc', 'add'), new AMQP\Topology\Binding('calc', 'sub'), new AMQP\Topology\Binding('calc', 'div')])); // Create consumer $consumer = new AMQP\Consumer($connection, new AMQP\Handler($service)); $consumer->consume($topology->queue('calc'));
<?php require_once __DIR__ . '/../vendor/autoload.php'; use ServiceBus\Broadcaster; use ServiceBus\Discovery\ArrayDiscovery; use ServiceBus\Endpoint; use ServiceBus\Identity; use ServiceBus\ServiceBus; use ServiceBus\Message\Request; use ServiceBus\Protocol\AMQP; $env = function ($key, $default = null) { return isset($_SERVER[$key]) ? $_SERVER[$key] : $default; }; $amqpUrl = $env('AMQP_URL', 'amqp://*****:*****@localhost/%2f'); $connection = \ButterAMQP\ConnectionBuilder::make()->create($amqpUrl); $driver = new AMQP\Driver($connection, 'outbox'); // Use AMQP driver for category_repository endpoint to send message over AMQP $bus = new ServiceBus(new ArrayDiscovery([new Endpoint(new Identity('category_repository', [$amqpUrl . '?exchange=category-repository']), $driver)]), new Broadcaster\NullBroadcaster()); // Publish a message asynchronously $bus->discover('category_repository')->publish(new Request('category_repository.updated', '1.0', ['category' => ['name' => 'Fruits', 'position' => 10]]));
<?php require_once __DIR__ . '/../vendor/autoload.php'; use ServiceBus\Discovery\ArrayDiscovery; use ServiceBus\ServiceBus; use ServiceBus\Message\Request; use ServiceBus\Protocol\AMQP; $env = function ($key, $default = null) { return isset($_SERVER[$key]) ? $_SERVER[$key] : $default; }; $connection = \ButterAMQP\ConnectionBuilder::make()->create($env('AMQP_URL', 'amqp://*****:*****@localhost/%2f')); // Use AMQP broadcaster to send messages over AMQP protocol $bus = new ServiceBus(new ArrayDiscovery([]), new AMQP\Broadcaster($connection, 'outbox')); // Broadcast a message to all subscribers $bus->broadcast(new Request('category_repository.updated', '1.0', ['category' => ['name' => 'Fruits', 'position' => 10]]));
/** * Service bus should broadcast messages using given driver. */ public function testBroadcast() { $request = new Request(''); $this->broadcaster->expects($this->once())->method('broadcast')->with($request); $this->serviceBus->broadcast($request); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use ServiceBus\Broadcaster\NullBroadcaster; use ServiceBus\Discovery\ArrayDiscovery; use ServiceBus\Endpoint; use ServiceBus\Identity; use ServiceBus\ServiceBus; use ServiceBus\Message\Request; use ServiceBus\Protocol\JSONRPC; $env = function ($key, $default = null) { return isset($_SERVER[$key]) ? $_SERVER[$key] : $default; }; $driver = new JSONRPC\Driver(); $bus = new ServiceBus(new ArrayDiscovery([new Endpoint(new Identity('calc', [$env('CALC_URL', 'http://localhost:8000/json-rpc')]), $driver)]), new NullBroadcaster()); // Make a call $promise = $bus->discover('calc')->call(new Request('div', '1', ['a' => 10, 'b' => 5])); // Wait for response $response = $promise->resolve(); // Should be 2 var_export($response->getValue()); echo PHP_EOL;
<?php use ButterAMQP\ConnectionBuilder; use ServiceBus\Discovery\LookupDiscovery; use ServiceBus\Discovery\Registry\InMemoryRegistry; use ServiceBus\Discovery\Registry\LookupRegistry; use ServiceBus\Discovery\RegistryDiscovery; use ServiceBus\Driver\ArrayDriver; use ServiceBus\DriverFactory; use ServiceBus\Endpoint; use ServiceBus\Identity; use ServiceBus\Message\Request; use ServiceBus\Protocol\JSONRPC; use ServiceBus\Protocol\AMQP; use ServiceBus\ServiceBus; $amqp = ConnectionBuilder::make()->create('amqp://localhost:5672/%2f'); $drivers = new ArrayDriver([new JSONRPC\Driver(), new AMQP\Driver($amqp)]); // This endpoint will be used to fetch discovery information. $discovery = new Endpoint(new Identity('discovery', ['jsonrpc://discovery', 'amqp://discovery?exchange=inbox.discovery']), $drivers); // Lookup registry will try to fetch service identity from in-memory registry and if its not there // will use discovery service. $registry = new LookupRegistry($discovery, new InMemoryRegistry()); $bus = new ServiceBus(new RegistryDiscovery($registry, $drivers), new AMQP\Broadcaster($amqp, 'outbox')); $pong = $bus->discover('ping-pong')->call(new Request('ping', ['payload' => $payload = uniqid('ping')]))->resolve()->getValue(); echo $pong['payload'] == $payload ? 'Service ping-pong working fine!' : 'Reply payload does not match'; echo PHP_EOL;
<?php require_once __DIR__ . '/../vendor/autoload.php'; use ServiceBus\Broadcaster; use ServiceBus\Discovery\ArrayDiscovery; use ServiceBus\Endpoint; use ServiceBus\Identity; use ServiceBus\ServiceBus; use ServiceBus\Message\Request; use ServiceBus\Protocol\AMQP; $env = function ($key, $default = null) { return isset($_SERVER[$key]) ? $_SERVER[$key] : $default; }; $amqpUrl = $env('AMQP_URL', 'amqp://*****:*****@localhost/%2f'); $connection = \ButterAMQP\ConnectionBuilder::make()->create($amqpUrl); // Use AMQP driver for category_repository endpoint to send message over AMQP $driver = new AMQP\Driver($connection, 'inbox.category_repository'); $bus = new ServiceBus(new ArrayDiscovery([new Endpoint(new Identity('category_repository', [$amqpUrl . '?exchange=category-repository']), $driver)]), new Broadcaster\NullBroadcaster()); // Make a call $promise = $bus->discover('category_repository')->call(new Request('category_repository.find', '1.0', ['query' => ['offset' => 10, 'size' => 10]])); // Wait for response $response = $promise->resolve();