1. Go to this page and download the library: Download kafka/workflow library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kafka / workflow example snippets
use Wf\Kafka\Producer\OutboxWriter;
use Wf\Kafka\Producer\ProducerOptions;
DB::transaction(function () use ($order) {
// Business logic của bạn
$order = Order::create($orderData);
// Ghi vào outbox — cùng transaction với business logic
OutboxWriter::write(
topic: 'order-events',
eventType: 'ORDER_CREATED',
payload: $order->toArray(),
options: ProducerOptions::make()->withKey((string) $order->id)
);
});
DB::transaction(function () use ($orders) {
// ...
$payloads = collect($orders)->map->toArray()->all();
OutboxWriter::writeBatch(
topic: 'order-events',
eventType: 'ORDER_CREATED',
payloads: $payloads
);
// Trả về: int — số bản ghi đã ghi
});
use Wf\Kafka\Exceptions\TransientInfraException;
use Wf\Kafka\Exceptions\PoisonPillException;
->handle(function (array $payload, array $meta) {
// Lỗi tạm thời (mạng, API timeout, DB down)
// → Package route vào DLQ, KHÔNG commit offset
// → Message được re-deliver khi consumer restart
if ($apiIsDown) {
throw new TransientInfraException("Payment API timeout sau 3s.");
}
// Dữ liệu không thể xử lý được (Poison Pill)
// → Package route vào EDL, commit offset (không block queue)
if (!in_array($payload['status'], ['PENDING', 'COMPLETED'])) {
throw new PoisonPillException("Unknown status: {$payload['status']}");
}
// Bất kỳ exception nào khác cũng → EDL
OrderService::process($payload);
})
bash
php artisan kafka:install # Detect OS → cài tự động
php artisan kafka:install --check # Chỉ kiểm tra, không cài
php artisan kafka:install --force # Không hỏi confirm
dockerfile
FROM php:8.2-fpm-alpine AS base
RUN apk add --no-cache librdkafka librdkafka-dev $PHPIZE_DEPS \
&& pecl install rdkafka \
&& docker-php-ext-enable rdkafka \
&& apk del --no-cache $PHPIZE_DEPS librdkafka-dev \
&& rm -rf /tmp/pear /var/cache/apk/*