Laravel 实现 Kafka 消息推送与接收处理 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区

[Laravel 实现 Kafka 消息推送与接收处理 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区]

安装环境要求
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;
    }
}

Original url: Access

Created at: 2018-10-10 17:08:52

Category: default

Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论