Laravel 實現 Kafka 訊息推送與接收處理

Explorer發表於2018-07-31

安裝環境要求
PHP 版本大於 7.0
Kafka Server 版本大於 0.8.0
消費模組 Kafka Server 版本需要大於 0.9.0

安裝
使用 Composer 安裝
新增 composer 依賴 nmred/kafka-php 到專案的 composer.json 檔案中即可,如:

 "require": {
        "php": ">=5.6.4",
        "laravel/lumen-framework": "5.4.*",
        "nmred/kafka-php": "dev-master"
    },

建立 KafkaService

<?php
namespace App\Http\Services;
use Kafka;

class KafkaService 
{
    public function __construct()
    {
        date_default_timezone_set('PRC');
    }

    /*
     * Produce
     */
    public function Producer($topic, $value , $url)
    {
        $config = \Kafka\ProducerConfig::getInstance();
        $config->setMetadataRefreshIntervalMs(10000);
        $config->setMetadataBrokerList($url);
        $config->setBrokerVersion('1.0.0');
        $config->setRequiredAck(1);
        $config->setIsAsyn(false);
        $config->setProduceInterval(500);
        $producer = new \Kafka\Producer(function () use($value,$topic){
            return [
                [
                    'topic' => $topic,
                    'value' => $value,
                    'key' => '',
                ],
            ];
        });
        $producer->success(function ($result){
            return "success";
        });
        $producer->error(function ($errorCode){
            var_dump($errorCode);
        });
        $producer->send(true);
    }

    /*
     * Consumer
     */
    public function consumer($group,$topics , $url){
        $config = \Kafka\ConsumerConfig::getInstance();
        $config->setMetadataRefreshIntervalMs(500);
        $config->setMetadataBrokerList($url);
        $config->setGroupId($group);
        $config->setBrokerVersion('1.0.0');
        $config->setTopics([$topics]);
        $config->setOffsetReset('earliest');
        $consumer = new \Kafka\Consumer();
        $consumer->start(function($topic, $part, $message) {
            echo "receive a message...\n";
            app('consumerKafka')->consumerData($message['message']['value']);  //你的接收處理邏輯
            var_dump($message['message']['value']);
        });
    }
}

執行produce方法生產訊息

<?php
namespace App\Http\Services;
use App\Http\Services\KafkaService;
class ProduceService
{
 public function produce()
    {
        $topic = env('topic_test'); //配置在env中
        $url = env('kafka_url_test'); //配置在env中
        $value =
            [
                'code' => 'test',
                'data_type' => 'personal',
                'action' => 'update',
                'data' =>
                    [
                        'id' => 1,
                        'name' => 'tom',
                        'gender' => 2
                    ],
                'redirect_url' => '',
                'operator' => 'system',
            ];
        $value = json_encode ($value, JSON_FORCE_OBJECT );
        $kafka = new KafkaService();
        $kafka->Producer($topic, $value , $url);
    }

}

執行php artisan consumer:kafka 消費訊息

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class ConsumerKafka extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'consumer:kafka';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '處理非同步kafka訊息';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->log('開始監聽訊息...');
        app('kafkaService')->consumer($group=env('KAFKA_GROUP'),$topics =env('KAFKA_TOPIC'), $url=env('KAFKA_URL'));
        return $this;
    }

    private function log($msg = '')
    {
        if (!$msg) {
            return $this;
        }
        if (php_sapi_name() == 'cli') {
            echo $msg, PHP_EOL;
        }
        app('myLog')->lumenLog($msg, 'kafka_consumer');
        return $this;
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章