{"id":19756805,"url":"https://github.com/imiphp/imi-kafka","last_synced_at":"2026-02-27T17:43:19.262Z","repository":{"id":52214624,"uuid":"364108480","full_name":"imiphp/imi-kafka","owner":"imiphp","description":"支持在 imi 框架中使用 Kafka 客户端","archived":false,"fork":false,"pushed_at":"2023-12-29T07:19:33.000Z","size":251,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"2.0","last_synced_at":"2024-05-23T02:02:57.164Z","etag":null,"topics":["kafka","kafka-client","mq"],"latest_commit_sha":null,"homepage":"https://github.com/Yurunsoft/imi","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imiphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-05-04T01:33:40.000Z","updated_at":"2023-11-11T11:13:09.000Z","dependencies_parsed_at":"2023-10-12T06:03:54.001Z","dependency_job_id":"4880eedf-c5ea-4708-9585-e600420c0f87","html_url":"https://github.com/imiphp/imi-kafka","commit_stats":{"total_commits":47,"total_committers":2,"mean_commits":23.5,"dds":"0.021276595744680882","last_synced_commit":"e0b13b7f46a35d00559831c867c3b708a9817e23"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-kafka","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-kafka/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-kafka/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-kafka/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imiphp","download_url":"https://codeload.github.com/imiphp/imi-kafka/tar.gz/refs/heads/2.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224207897,"owners_count":17273674,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kafka","kafka-client","mq"],"created_at":"2024-11-12T03:16:58.565Z","updated_at":"2026-02-27T17:43:14.211Z","avatar_url":"https://github.com/imiphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# imi-kafka\n\n[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-kafka.svg)](https://packagist.org/packages/imiphp/imi-kafka)\n[![Php Version](https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg)](https://secure.php.net/)\n[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.7.0-brightgreen.svg)](https://github.com/swoole/swoole-src)\n[![IMI License](https://img.shields.io/github/license/imiphp/imi-kafka.svg)](https://github.com/imiphp/imi-kafka/blob/master/LICENSE)\n\n## 介绍\n\n支持在 imi 框架中使用 Kafka 客户端\n\n支持消息发布和消费\n\n本组件基于 [龙之言](https://longlang.org/) 组织的 [longlang/phpkafka](https://github.com/longyan/phpkafka) 组件，该组件由宇润主导开发。\n\n\u003e 本仓库仅用于浏览，不接受 issue 和 Pull Requests，请前往：\u003chttps://github.com/imiphp/imi\u003e\n\n## Composer\n\n本项目可以使用composer安装，遵循psr-4自动加载规则，在你的 `composer.json` 中加入下面的内容:\n\n```json\n{\n    \"require\": {\n        \"imiphp/imi-kafka\": \"~2.0.0\"\n    }\n}\n```\n\n然后执行 `composer update` 安装。\n\n## 使用说明\n\n可以参考 `example` 目录示例，包括完整的消息发布和消费功能。\n\n在项目 `config/config.php` 中配置：\n\n```php\n[\n    'components'    =\u003e  [\n        // 引入组件\n        'Kafka'   =\u003e  'Imi\\Kafka',\n    ],\n]\n```\n\n连接池配置：\n\n```php\n[\n    'pools'    =\u003e    [\n        'kafka'    =\u003e [\n            'sync'    =\u003e [\n                'pool'    =\u003e [\n                    'class'        =\u003e \\Imi\\Kafka\\Pool\\KafkaSyncPool::class,\n                    'config'       =\u003e [\n                        'maxResources'    =\u003e 10,\n                        'minResources'    =\u003e 0,\n                    ],\n                ],\n                'resource'    =\u003e [\n                    'bootstrapServers' =\u003e KAFKA_BOOTSTRAP_SERVERS,\n                    'groupId'          =\u003e 'test',\n                    // 其它配置请参考：https://github.com/longyan/phpkafka/blob/master/doc/consumer.md#%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0\n                ],\n            ],\n            'async'    =\u003e [\n                'pool'    =\u003e [\n                    'class'        =\u003e \\Imi\\Kafka\\Pool\\KafkaCoroutinePool::class,\n                    'config'       =\u003e [\n                        'maxResources'    =\u003e 10,\n                        'minResources'    =\u003e 1,\n                    ],\n                ],\n                'resource'    =\u003e [\n                    'bootstrapServers' =\u003e KAFKA_BOOTSTRAP_SERVERS,\n                    'groupId'          =\u003e 'test',\n                ],\n            ],\n        ],\n    ]\n]\n```\n\n默认连接池：\n\n```php\n[\n    'beans' =\u003e  [\n        'Kafka'  =\u003e  [\n            'defaultPoolName'   =\u003e  'kafka',\n        ],\n    ],\n]\n```\n\n### 生产者\n\n```php\nuse Imi\\Kafka\\Pool\\KafkaPool;\nuse longlang\\phpkafka\\Producer\\ProduceMessage;\n\n// 获取生产者对象\n$producer = KafkaPool::getInstance();\n\n// 发送\n$producer-\u003esend('主题 Topic', '消息内容');\n// send 方法定义\n// public function send(string $topic, ?string $value, ?string $key = null, array $headers = [], ?int $partitionIndex = null, ?int $brokerId = null): void\n\n// 批量发送\n$producer-\u003esendBatch([\n    new ProduceMessage($topic, 'v1', 'k1'),\n    new ProduceMessage($topic, 'v2', 'k2'),\n]);\n// sendBatch 方法定义\n// public function sendBatch(ProduceMessage[] $messages, ?int $brokerId = null): void\n```\n\n### 消费者\n\n**消费者类：**\n\n```php\n\u003c?php\n\nnamespace ImiApp\\Kafka\\Test;\n\nuse Imi\\Bean\\Annotation\\Bean;\nuse Imi\\Kafka\\Annotation\\Consumer;\nuse Imi\\Kafka\\Base\\BaseConsumer;\nuse Imi\\Redis\\Redis;\nuse longlang\\phpkafka\\Consumer\\ConsumeMessage;\n\n/**\n * @Bean(\"TestConsumer\")\n * @Consumer(topic=\"queue-imi-1\", groupId=\"test-consumer\")\n */\nclass TestConsumer extends BaseConsumer\n{\n    /**\n     * 消费任务\n     */\n    protected function consume(ConsumeMessage $message): void\n    {\n        $messageValue = $message-\u003egetValue();\n    }\n}\n```\n\n**消费进程：**\n\n```php\n\u003c?php\n\nnamespace ImiApp\\Process;\n\nuse Imi\\Aop\\Annotation\\Inject;\nuse Imi\\App;\nuse Imi\\Kafka\\Contract\\IConsumer;\nuse Imi\\Process\\Annotation\\Process;\nuse Imi\\Process\\BaseProcess;\n\n/**\n * @Process(name=\"TestProcess\")\n */\nclass TestProcess extends BaseProcess\n{\n    /**\n     * @Inject(\"TestConsumer\")\n     *\n     * @var \\ImiApp\\Kafka\\Test\\TestConsumer\n     */\n    protected $testConsumer;\n\n    public function run(\\Swoole\\Process $process): void\n    {\n        $this-\u003erunConsumer($this-\u003etestConsumer);\n        \\Swoole\\Coroutine::yield();\n    }\n\n    private function runConsumer(IConsumer $consumer): void\n    {\n        go(function () use ($consumer) {\n            try\n            {\n                $consumer-\u003erun();\n            }\n            catch (\\Throwable $th)\n            {\n                /** @var \\Imi\\Log\\ErrorLog $errorLog */\n                $errorLog = App::getBean('ErrorLog');\n                $errorLog-\u003eonException($th);\n                sleep(3);\n                $this-\u003erunConsumer($consumer);\n            }\n        });\n    }\n}\n```\n\n### 注解说明\n\n### @Consumer\n\n消费者注解\n\n| 属性名称 | 说明 |\n|-|-\n| topic | 主题名称，支持字符串或字符串数组 |\n| groupId | 分组ID |\n| poolName | 连接池名称，不传则使用配置中默认的 |\n\n### 队列组件支持\n\n本组件额外实现了 [imiphp/imi-queue](https://github.com/imiphp/imi-queue) 的接口，可以用 Queue 组件的 API 进行调用。\n\n只需要将队列驱动配置为：`KafkaQueueDriver`\n\n配置示例：\n\n```php\n[\n    'components'    =\u003e  [\n        'Kafka'  =\u003e  'Imi\\Kafka',\n    ],\n    'beans' =\u003e  [\n        'AutoRunProcessManager' =\u003e  [\n            'processes' =\u003e  [\n                // 加入队列消费进程，非必须，你也可以自己写进程消费\n                'QueueConsumer',\n            ],\n        ],\n        'imiQueue'  =\u003e [\n            // 默认队列\n            'default'   =\u003e 'QueueTest1',\n            // 队列列表\n            'list'  =\u003e [\n                // 队列名称\n                'QueueTest1' =\u003e [\n                    // 使用的队列驱动\n                    'driver'        =\u003e 'KafkaQueueDriver',\n                    // 消费协程数量\n                    'co'            =\u003e 1,\n                    // 消费进程数量；可能会受进程分组影响，以同一组中配置的最多进程数量为准\n                    'process'       =\u003e 1,\n                    // 消费循环尝试 pop 的时间间隔，单位：秒（仅使用消费者类时有效）\n                    'timespan'      =\u003e 0.1,\n                    // 进程分组名称\n                    'processGroup'  =\u003e 'a',\n                    // 自动消费\n                    'autoConsumer'  =\u003e true,\n                    // 消费者类\n                    'consumer'      =\u003e 'QueueTestConsumer',\n                    // 驱动类所需要的参数数组\n                    'config'        =\u003e [\n                        // Kafka 连接池名称\n                        'poolName' =\u003e 'kafka',\n                        // 分组ID，如果不传或为null则使用连接池中的配置\n                        'groupId'  =\u003e 'g1',\n                    ],\n                ],\n            ],\n        ],\n    ]\n]\n```\n\n消费者类写法，与`imi-queue`组件用法一致。\n\n## 免费技术支持\n\nQQ群：17916227 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png \"点击加群\")](https://jq.qq.com/?_wv=1027\u0026k=5wXf4Zq)，如有问题会有人解答和修复。\n\n## 运行环境\n\n- [PHP](https://php.net/) \u003e= 7.4\n- [Composer](https://getcomposer.org/) \u003e= 2.0\n- [Swoole](https://www.swoole.com/) \u003e= 4.7.0\n\n## 版权信息\n\n`imi-kafka` 遵循 MIT 开源协议发布，并提供免费使用。\n\n## 捐赠\n\n\u003cimg src=\"https://cdn.jsdelivr.net/gh/imiphp/imi@2.0/res/pay.png\"/\u003e\n\n开源不求盈利，多少都是心意，生活不易，随缘随缘……\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-kafka","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimiphp%2Fimi-kafka","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-kafka/lists"}