This bundle provides a simple way to integrate Symfony Messenger with the Confluent Schema Registry.
Open a command console, enter your project directory and execute:
$ composer require koco/avro-regy
After adding the composer requirement, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
return [
// ...
Koco\AvroRegy\AvroRegyBundle::class => ['all' => true],
];
Create config/avro_regy.yaml
and configure similar to the following example:
avro_regy:
base_uri: '%env(SCHEMA_REGISTRY_URL)%'
file_naming_strategy: subject
options:
register_missing_schemas: true
register_missing_subjects: true
serializers:
catalogue:
schema_dir: '%kernel.project_dir%/src/Catalogue/Domain/Model/Event/Avro/'
orders:
schema_dir: '%kernel.project_dir%/src/Orders/Domain/Model/Event/Avro/'
Each serializer that you configure, will be available as it's own service. The service id is avro_regy.serializer.<name>
.
In config/messenger.yaml
it can be called like so:
framework:
messenger:
transports:
catalogue:
dsn: '%env(MESSENGER_TRANSPORT_CATALOGUE_KAFKA_DSN)%'
serializer: avro_regy.serializer.catalogue
options:
...
Then, write your serializer like so:
namespace App\Catalogue\Infrastructure\Messenger\Serializer;
use App\Catalogue\Domain\Model\Event\ProductCreated;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
class ProductCreatedSerializer implements SerializerInterface
{
public function decode(array $encodedEnvelope): Envelope
{
$record = $encodedEnvelope['body'];
return new Envelope(new ProductCreated(
$record['id'],
$record['name'],
$record['description'],
));
}
public function encode(Envelope $envelope): array
{
/** @var ProductCreated $event */
$event = $envelope->getMessage();
return [
'key' => $event->getId(),
'headers' => [],
'body' => [
'id' => $event->getId(),
'name' => $event->getName(),
'description' => $event->getDescription(),
],
];
}
}
And tag it:
App\Catalogue\Infrastructure\Messenger\Serializer\ProductCreatedSerializer:
tags:
- {
name: 'avro_regy.serializer.catalogue',
qualified_name: 'com.Example.Catalogue.ProductCreated',
class_name: 'App\Catalogue\Domain\Model\Event\ProductCreated',
key_subject: 'catalogue.product_created-key',
value_subject: 'catalogue.product_created-value'
}