php :Hyperf 实现 kafka 消息队列

 注意:我现在用的是 Beta版 暂时不能用在项目上,我出现过cup与负载100%的情况,有时候会不知道为什么停掉,追求稳定还是用 amqp 更好一些。

安装 kafka

Apache 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的消费端被监听

 访问接口投递数据,就会触发消费者将数据存到数据库

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值