Distributed Bus
Ecotone
provides support for communication in distributed architecture.
The support covers sending command directly to specific service (application) or publishing events, that other services can subscribe too. In order for
Ecotone
how to route messages you need to register Service Name (Application Name).Distribution Bus is Message Gateway just like CommandBus or EventBus.
The bus is responsible for distribution of your command and events.
interface DistributedBus
{
// Command distribution
public function sendCommand(string $destination, string $routingKey, string $command, string $sourceMediaType = MediaType::TEXT_PLAIN, array $metadata = []) : void;
public function convertAndSendCommand(string $destination, string $routingKey, object|array $command, array $metadata = []) : void;
// Event distribution
public function publishEvent(string $routingKey, string $event, string $sourceMediaType = MediaType::TEXT_PLAIN, array $metadata = []) : void;
public function convertAndPublishEvent(string $routingKey, object|array $event, array $metadata) : void;
// general message distribution
public function sendMessage(string $destination, string $targetChannelName, string $payload, string $sourceMediaType = MediaType::TEXT_PLAIN, array $metadata = []): void;
}
routingKey
- is a routing key under which CommandHandler is registered on targeted Service
public function changeBillingDetails(DistributedBus $distributedCommandBus)
{
$distributedCommandBus->sendCommand(
"billing", // destination
"billing.changeDetails", // routingKey
'["personId":"123","billingDetails":"01111"]',
"application/json"
);
}
On the consumer side register distributed command handler
#[Distributed]
#[CommandHandler("billing.changeDetails")]
public function changeBillingDetails(ChangeBillingDetails $command) : void
{
// do something with billing details
}
To expose command handler for service distribution mark it with
Distributed
attribute.Run consumer for your registered distributed consumer. It will be available under your Service Name
List:
Symfony
Laravel
Lite
bin/console ecotone:list
+--------------------+
| Endpoint Names |
+--------------------+
| billings |
+--------------------+
artisan ecotone:list
+--------------------+
| Endpoint Names |
+--------------------+
| billing |
+--------------------+
$consumers = $messagingSystem->list()
Run it:
Symfony
Laravel
Lite
bin/console ecotone:run billing -vvv
artisan ecotone:run billing -vvv
$messagingSystem->run("billing");
routingKey
- is a routing key under which event will be published
public function changeBillingDetails(DistributedBus $eventBus)
{
$eventBus->publishEvent(
"billing.detailsWereChanged", // routingKey
'{"personId":123,"billingDetails":0001}',
"application/json"
);
}
#[Distributed]
#[EventHandler("billing.detailsWereChanged")]
public function registerTicket(BillingDetailsWereChanged $event) : void
{
// do something with event
}
To start listening for distributed events under specific key, provide
Distributed
attribute.You may distribute general message, if you don't want to include given endpoint as Command or Event Handler.
targetChannelName
- is a target channel on the destination side
public function changeBillingDetails(DistributedBus $distributedCommandBus)
{
$distributedCommandBus->sendCommand(
"billing", // destination
"billing.changeDetails", // targetChannelName
'["personId":"123","billingDetails":"01111"]',
"application/json"
);
}
#[Distributed]
#[ServiceActivator("billing.changeDetails")]
public function changeBillingDetails(ChangeBillingDetails $command) : void
{
// do something with billing details
}
Last modified 5mo ago