注意:我现在用的是 Beta版 暂时不能用在项目上,我出现过cup与负载100%的情况,有时候会不知道为什么停掉,追求稳定还是用 amqp 更好一些。
安装 kafka
下载二进制版
解压
tar -xzf kafka_2.13-2.7.0.tgz
运行 zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties
另开一个终端窗口 开启 kafka
bin/kafka-server-start.sh config/server.properties &
创建一个topic(注意这个参数代码里要对应。用到的端口:9092)
bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
hyperf 安装 kafka 组件
可参考hyperf 官网 Hyperf
注意有坑
composer require hyperf/kafka
创建配置
php bin/hyperf.php vendor:publish hyperf/kafka
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\Kafka\Constants\KafkaStrategy;
return [
'default' => [
'connect_timeout' => -1,
'send_timeout' => -1,
'recv_timeout' => -1,
'client_id' => '',
'max_write_attempts' => 3,
'brokers' => [
'127.0.0.1:9092',
],
'bootstrap_server' => [
'127.0.0.1:9092',
],
'update_brokers' => true,
'acks' => 0,
'producer_id' => -1,
'producer_epoch' => -1,
'partition_leader_epoch' => -1,
'broker' => '127.0.0.1:9092',
'interval' => 0,
'session_timeout' => 60,
'rebalance_timeout' => 60,
'replica_id' => -1,
'rack_id' => '127.0.0.1:9092',
'group_retry' => 5,
'group_retry_sleep' => 1,
'group_heartbeat' => 3,
'offset_retry' => 5,
'auto_create_topic' => true,
'partition_assignment_strategy' => KafkaStrategy::RANGE_ASSIGNOR,
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => 60.0,
],
],
];
创建消费者(注意不能var_dump())
<?php
declare(strict_types=1);
namespace App\kafka;
use Hyperf\DbConnection\Db;
use Hyperf\Kafka\AbstractConsumer;
use Hyperf\Kafka\Annotation\Consumer;
use longlang\phpkafka\Consumer\ConsumeMessage;
/**
* @Consumer(topic="test", nums=1, groupId="hyperf", autoCommit=true)
*/
class KafkaConsumer extends AbstractConsumer
{
public function consume(ConsumeMessage $message): string
{
Db::table('user')->insert(['v'=>$message->getTopic().$message->getValue(),'time'=>time()]);
//var_dump($message->getTopic() . ':' . $message->getKey() . ':' . $message->getValue());
return $message->getTopic() . ':' . $message->getKey() . ':' . $message->getValue();
}
}
创建投递者(可在controller里创建方法)
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Kafka\Producer;
/**
* @AutoController()
*/
class IndexController extends AbstractController
{
public function index(Producer $producer)
{
$v = rand(100,999).'k';
$producer->send('test',$v, 'key');
}
}
运行项目会看到kafka的消费端被监听
访问接口投递数据,就会触发消费者将数据存到数据库