|
| 1 | +# Kafka Support |
| 2 | + |
| 3 | +{% hint style="success" %} |
| 4 | +This module is available as part of **Ecotone Enterprise.** |
| 5 | +{% endhint %} |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```php |
| 10 | +composer require ecotone/kafka |
| 11 | +``` |
| 12 | + |
| 13 | +## Configuration |
| 14 | + |
| 15 | +In order to use **Kafka Support** we need to add **KafkaBrokerConfiguration** to our **Dependency Container.**  |
| 16 | + |
| 17 | +{% tabs %} |
| 18 | +{% tab title="Symfony" %} |
| 19 | +```php |
| 20 | +# config/services.yaml |
| 21 | +# You need to have RabbitMQ instance running on your localhost, or change DSN |
| 22 | + Ecotone\Kafka\Configuration\KafkaBrokerConfiguration: |
| 23 | + class: Ecotone\Kafka\Configuration\KafkaBrokerConfiguration |
| 24 | + arguments: |
| 25 | + bootstrapServers: |
| 26 | + - localhost:9094 |
| 27 | +``` |
| 28 | +{% endtab %} |
| 29 | + |
| 30 | +{% tab title="Laravel" %} |
| 31 | +```php |
| 32 | +# Register AMQP Service in Provider |
| 33 | + |
| 34 | +use Ecotone\Kafka\Configuration\KafkaBrokerConfiguration; |
| 35 | + |
| 36 | +public function register() |
| 37 | +{ |
| 38 | + $this->app->singleton(KafkaBrokerConfiguration::class, function () { |
| 39 | + return new KafkaBrokerConfiguration(['localhost:9094']); |
| 40 | + }); |
| 41 | +} |
| 42 | +``` |
| 43 | +{% endtab %} |
| 44 | + |
| 45 | +{% tab title="Lite" %} |
| 46 | +```php |
| 47 | +use Ecotone\Kafka\Configuration\KafkaBrokerConfiguration; |
| 48 | + |
| 49 | +$application = EcotoneLiteApplication::boostrap( |
| 50 | + [ |
| 51 | + KafkaBrokerConfiguration::class => new KafkaBrokerConfiguration(['localhost:9094']) |
| 52 | + ] |
| 53 | +); |
| 54 | +``` |
| 55 | +{% endtab %} |
| 56 | +{% endtabs %} |
| 57 | + |
| 58 | +{% hint style="info" %} |
| 59 | +We register our **KafkaBrokerConfiguration** under the class name **Ecotone\Kafka\Configuration\KafkaBrokerConfiguration**. This will help Ecotone resolve it automatically, without any additional configuration. |
| 60 | +{% endhint %} |
| 61 | + |
| 62 | +## Message Channel |
| 63 | + |
| 64 | +To create Kafka Backed [Message Channel](../modelling/asynchronous-handling/), we need to create [Service Context](../messaging/service-application-configuration.md).  |
| 65 | + |
| 66 | +```php |
| 67 | +class MessagingConfiguration |
| 68 | +{ |
| 69 | + #[ServiceContext] |
| 70 | + public function orderChannel() |
| 71 | + { |
| 72 | + return KafkaMessageChannelBuilder::create("orders"); |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Now `orders` channel will be available in our Messaging System.  |
| 78 | + |
| 79 | +{% hint style="success" %} |
| 80 | +Message Channels simplify to the maximum integration with Message Broker. \ |
| 81 | +From application perspective all we need to do, is to provide channel implementation.\ |
| 82 | +Ecotone will take care of whole publishing and consuming part.  |
| 83 | +{% endhint %} |
| 84 | + |
| 85 | +### Customize Topic Name |
| 86 | + |
| 87 | +By default the queue name will follow channel name, which in above example will be "orders".\ |
| 88 | +However we can use "orders" as reference name in our Application, yet name queue differently: |
| 89 | + |
| 90 | +```php |
| 91 | +#[ServiceContext] |
| 92 | +public function orderChannel() |
| 93 | +{ |
| 94 | + return KafkaMessageChannelBuilder::create( |
| 95 | + channelName: "orders", |
| 96 | + topicName: "crm_orders" |
| 97 | + ); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### Customize Group Id |
| 102 | + |
| 103 | +We can also customize the group id, which by default with following channel name: |
| 104 | + |
| 105 | +```php |
| 106 | +#[ServiceContext] |
| 107 | +public function orderChannel() |
| 108 | +{ |
| 109 | + return KafkaMessageChannelBuilder::create( |
| 110 | + channelName: "orders", |
| 111 | + groupId: "crm_application" |
| 112 | + ); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Custom Publisher and Consumer |
| 117 | + |
| 118 | +To create [custom publisher or consumer](../modelling/microservices-php/) provide [Service Context](../messaging/service-application-configuration.md). |
| 119 | + |
| 120 | +{% hint style="success" %} |
| 121 | +Custom Publishers and Consumers are great for building integrations for existing infrastructure or setting up a customized way to communicate between applications. With this you can take over the control of what is published and how it's consumed. |
| 122 | +{% endhint %} |
| 123 | + |
| 124 | +### Custom Publisher |
| 125 | + |
| 126 | +```php |
| 127 | +class MessagingConfiguration |
| 128 | +{ |
| 129 | + #[ServiceContext] |
| 130 | + public function distributedPublisher() |
| 131 | + { |
| 132 | + return KafkaPublisherConfiguration::createWithDefaults( |
| 133 | + topicName: 'orders' |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +Then Publisher will be available for us in Dependency Container under **MessagePublisher** reference. |
| 140 | + |
| 141 | +### Custom Consumer |
| 142 | + |
| 143 | +To set up Consumer, consuming from given topics, all we need to do, is to mark given method with KafkaConsumer attribute: |
| 144 | + |
| 145 | +```php |
| 146 | +#[KafkaConsumer('ordersConsumers', 'orders')] |
| 147 | +public function handle(string $payload, array $metadata): void |
| 148 | +{ |
| 149 | + // do something |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +Then we run it as any other [asynchronous consumer](../modelling/asynchronous-handling/), using **ordersConsumer** name. |
| 154 | + |
| 155 | +### Custom Topic Configuration |
| 156 | + |
| 157 | +We can also customize topic configuration. For example to create reference name for Consumers and publishers, which internally in Kafka will map to different name |
| 158 | + |
| 159 | +```php |
| 160 | +class MessagingConfiguration |
| 161 | +{ |
| 162 | + #[ServiceContext] |
| 163 | + public function distributedPublisher() |
| 164 | + { |
| 165 | + return TopicConfiguration::createWithReferenceName("orders", 'crm_orders'); |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
0 commit comments