{"id":19756806,"url":"https://github.com/imiphp/imi-amqp","last_synced_at":"2025-04-30T12:30:41.127Z","repository":{"id":48170544,"uuid":"230051569","full_name":"imiphp/imi-amqp","owner":"imiphp","description":"支持在 imi 框架中使用 支持 AMQP 协议的消息队列，如：RabbitMQ","archived":false,"fork":false,"pushed_at":"2023-12-29T07:19:30.000Z","size":350,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"2.0","last_synced_at":"2025-04-14T22:02:12.701Z","etag":null,"topics":["amqp","amqp-client","imi-amqp","mq","php","rabbitmq","rabbitmq-client","rabbitmq-consumer","rabbitmq-producer","swoole"],"latest_commit_sha":null,"homepage":"","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":"2019-12-25T06:13:23.000Z","updated_at":"2024-12-16T03:19:57.000Z","dependencies_parsed_at":"2023-10-12T07:14:14.034Z","dependency_job_id":"5ee45537-a635-4a69-831d-02ac63181e0a","html_url":"https://github.com/imiphp/imi-amqp","commit_stats":{"total_commits":95,"total_committers":3,"mean_commits":"31.666666666666668","dds":0.03157894736842104,"last_synced_commit":"a591d215d00ecc38a0678816ef276d1ad9caba74"},"previous_names":[],"tags_count":77,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-amqp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-amqp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-amqp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-amqp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imiphp","download_url":"https://codeload.github.com/imiphp/imi-amqp/tar.gz/refs/heads/2.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250882725,"owners_count":21502365,"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":["amqp","amqp-client","imi-amqp","mq","php","rabbitmq","rabbitmq-client","rabbitmq-consumer","rabbitmq-producer","swoole"],"created_at":"2024-11-12T03:16:58.576Z","updated_at":"2025-04-30T12:30:40.848Z","avatar_url":"https://github.com/imiphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# imi-amqp\n\n[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-amqp.svg)](https://packagist.org/packages/imiphp/imi-amqp)\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-amqp.svg)](https://github.com/imiphp/imi-amqp/blob/master/LICENSE)\n\n## 介绍\n\n支持在 imi 框架中使用 支持 AMQP 协议的消息队列，如：RabbitMQ\n\n支持消息发布和消费\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-amqp\": \"~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        'AMQP'   =\u003e  'Imi\\AMQP',\n    ],\n]\n```\n\n连接池配置：\n\n```php\n[\n    'pools'    =\u003e    [\n        'rabbit'    =\u003e  [\n            'sync'    =\u003e    [\n                'pool'    =\u003e    [\n                    'class'        =\u003e    \\Imi\\AMQP\\Pool\\AMQPSyncPool::class,\n                    'config'    =\u003e    [\n                        'maxResources'    =\u003e    10,\n                        'minResources'    =\u003e    0,\n                    ],\n                ],\n                'resource'    =\u003e    [\n                    'host'      =\u003e '127.0.0.1',\n                    'port'      =\u003e 5672,\n                    'user'      =\u003e 'guest',\n                    'password'  =\u003e 'guest',\n                ]\n            ],\n            'async'    =\u003e    [\n                'pool'    =\u003e    [\n                    'class'        =\u003e    \\Imi\\AMQP\\Pool\\AMQPCoroutinePool::class,\n                    'config'    =\u003e    [\n                        'maxResources'    =\u003e    10,\n                        'minResources'    =\u003e    1,\n                    ],\n                ],\n                'resource'    =\u003e    [\n                    'host'      =\u003e '127.0.0.1',\n                    'port'      =\u003e 5672,\n                    'user'      =\u003e 'guest',\n                    'password'  =\u003e 'guest',\n                ]\n            ],\n        ],\n    ]\n]\n```\n\n默认连接池：\n\n```php\n[\n    'beans' =\u003e  [\n        'AMQP'  =\u003e  [\n            'defaultPoolName'   =\u003e  'rabbit',\n        ],\n    ],\n]\n```\n\n### 连接配置项\n\n| 属性名称 | 说明 |\n|-|-\n| host | 主机 |\n| port | 端口 |\n| user | 用户名 |\n| vhost | vhost，默认`/` |\n| insist | insist |\n| loginMethod | 默认`AMQPLAIN` |\n| loginResponse | loginResponse |\n| locale | 默认`en_US` |\n| connectionTimeout | 连接超时 |\n| readWriteTimeout | 读写超时 |\n| keepalive | keepalive，默认`false` |\n| heartbeat | 心跳时间，默认`0` |\n| channelRpcTimeout | 频道 RPC 超时时间，默认`0.0` |\n| sslProtocol | ssl 协议，默认`null` |\n\n### 消息定义\n\n继承 `Imi\\AMQP\\Message` 类，可在构造方法中对属性修改。\n\n根据需要可以覆盖实现`setBodyData`、`getBodyData`方法，实现自定义的消息结构。\n\n```php\n\u003c?php\nnamespace AMQPApp\\AMQP\\Test2;\n\nuse Imi\\AMQP\\Message;\n\nclass TestMessage2 extends Message\n{\n    /**\n     * 用户ID\n     *\n     * @var int\n     */\n    private $memberId;\n\n    /**\n     * 内容\n     *\n     * @var string\n     */\n    private $content;\n\n    public function __construct()\n    {\n        parent::__construct();\n        $this-\u003eroutingKey = 'imi-2';\n        $this-\u003eformat = \\Imi\\Util\\Format\\Json::class;\n    }\n\n    /**\n     * 设置主体数据\n     *\n     * @param mixed $data\n     * @return self\n     */\n    public function setBodyData($data)\n    {\n        foreach($data as $k =\u003e $v)\n        {\n            $this-\u003e$k = $v;\n        }\n    }\n\n    /**\n     * 获取主体数据\n     *\n     * @return mixed\n     */\n    public function getBodyData()\n    {\n        return [\n            'memberId'  =\u003e  $this-\u003ememberId,\n            'content'   =\u003e  $this-\u003econtent,\n        ];\n    }\n\n    /**\n     * Get 用户ID\n     *\n     * @return int\n     */ \n    public function getMemberId()\n    {\n        return $this-\u003ememberId;\n    }\n\n    /**\n     * Set 用户ID\n     *\n     * @param int $memberId  用户ID\n     *\n     * @return self\n     */ \n    public function setMemberId(int $memberId)\n    {\n        $this-\u003ememberId = $memberId;\n\n        return $this;\n    }\n\n    /**\n     * Get 内容\n     *\n     * @return string\n     */ \n    public function getContent()\n    {\n        return $this-\u003econtent;\n    }\n\n    /**\n     * Set 内容\n     *\n     * @param string $content  内容\n     *\n     * @return self\n     */ \n    public function setContent(string $content)\n    {\n        $this-\u003econtent = $content;\n\n        return $this;\n    }\n\n}\n```\n\n**属性列表：**\n\n名称 | 说明 |  默认值\n-|-|-\nbodyData | 消息主体内容，非字符串 | `null` |\nproperties | 属性 | `['content_type'  =\u003e 'text/plain', 'delivery_mode' =\u003e AMQPMessage::DELIVERY_MODE_PERSISTENT,]` |\nroutingKey | 路由键 | 空字符串 |\nformat | 如果设置了，发布的消息是编码后的`bodyData`，同理读取时也会解码。实现了`Imi\\Util\\Format\\IFormat`的格式化类。支持`Json`、`PhpSerialize` | `null` |\nmandatory | mandatory标志位 | `false` |\nimmediate | immediate标志位 | `false` |\nticket | ticket | `null` |\n\n### 发布者\n\n必选注解：`@Publisher`\n\n可选注解：`@Queue`、`@Exchange`、`@Connection`\n\n不配置 `@Connection` 注解，可以从连接池中获取连接\n\n```php\n\u003c?php\nnamespace AMQPApp\\AMQP\\Test;\n\nuse Imi\\Bean\\Annotation\\Bean;\nuse Imi\\AMQP\\Annotation\\Queue;\nuse Imi\\AMQP\\Base\\BasePublisher;\nuse Imi\\AMQP\\Annotation\\Consumer;\nuse Imi\\AMQP\\Annotation\\Exchange;\nuse Imi\\AMQP\\Annotation\\Publisher;\nuse Imi\\AMQP\\Annotation\\Connection;\n\n/**\n * @Bean(\"TestPublisher\")\n * @Connection(host=\"127.0.0.1\", port=5672, user=\"guest\", password=\"guest\")\n * @Publisher(tag=\"tag-imi\", queue=\"queue-imi-1\", exchange=\"exchange-imi\", routingKey=\"imi-1\")\n * @Queue(name=\"queue-imi-1\", routingKey=\"imi-1\")\n * @Exchange(name=\"exchange-imi\")\n */\nclass TestPublisher extends BasePublisher\n{\n\n}\n```\n\n### 消费者\n\n必选注解：`@Consumer`\n\n可选注解：`@Queue`、`@Exchange`、`@Connection`\n\n不配置 `@Connection` 注解，可以从连接池中获取连接\n\n```php\n\u003c?php\nnamespace AMQPApp\\AMQP\\Test;\n\nuse Imi\\Redis\\Redis;\nuse Imi\\Bean\\Annotation\\Bean;\nuse Imi\\AMQP\\Annotation\\Queue;\nuse Imi\\AMQP\\Base\\BaseConsumer;\nuse Imi\\AMQP\\Contract\\IMessage;\nuse Imi\\AMQP\\Annotation\\Consumer;\nuse Imi\\AMQP\\Annotation\\Exchange;\nuse Imi\\AMQP\\Enum\\ConsumerResult;\nuse Imi\\AMQP\\Annotation\\Connection;\n\n/**\n * 启动一个新连接消费\n * \n * @Bean(\"TestConsumer\")\n * @Connection(host=\"127.0.0.1\", port=5672, user=\"guest\", password=\"guest\")\n * @Consumer(tag=\"tag-imi\", queue=\"queue-imi-1\", message=\\AMQPApp\\AMQP\\Test\\TestMessage::class)\n */\nclass TestConsumer extends BaseConsumer\n{\n    /**\n     * 消费任务\n     *\n     * @param \\AMQPApp\\AMQP\\Test\\TestMessage $message\n     */\n    protected function consume(IMessage $message): int\n    {\n        var_dump(__CLASS__, $message-\u003egetBody(), get_class($message));\n        Redis::set('imi-amqp:consume:1:' . $message-\u003egetMemberId(), $message-\u003egetBody());\n        return ConsumerResult::ACK;\n    }\n\n}\n\n```\n\n### 注解说明\n\n### @Publisher\n\n发布者注解\n\n| 属性名称 | 说明 |\n|-|-\n| queue | 队列名称 |\n| exchange | 交换机名称 |\n| routingKey | 路由键 |\n\n### @Consumer\n\n消费者注解\n\n| 属性名称 | 说明 |\n|-|-\n| tag | 消费者标签 |\n| queue | 队列名称 |\n| exchange | 交换机名称 |\n| routingKey | 路由键 |\n| message | 消息类名，默认：`Imi\\AMQP\\Message` |\n| mandatory | mandatory标志位 |\n| immediate | immediate标志位 |\n| ticket | ticket |\n\n### @Queue\n\n队列注解\n\n| 属性名称 | 说明 |\n|-|-\n| name | 队列名称 |\n| routingKey | 路由键 |\n| passive | 被动模式，默认`false` |\n| durable | 消息队列持久化，默认`true` |\n| exclusive | 独占，默认`false` |\n| autoDelete | 自动删除，默认`false` |\n| nowait | 是否非阻塞，默认`false` |\n| arguments | 参数 |\n| ticket | ticket |\n\n### @Exchange\n\n交换机注解\n\n| 属性名称 | 说明 |\n|-|-\n| name | 交换机名称 |\n| type | 类型可选：`direct`、`fanout`、`topic`、`headers` |\n| passive | 被动模式，默认`false` |\n| durable | 消息队列持久化，默认`true` |\n| autoDelete | 自动删除，默认`false` |\n| internal | 设置是否为rabbitmq内部使用, `true`表示是内部使用, `false`表示不是内部使用 |\n| nowait | 是否非阻塞，默认`false` |\n| arguments | 参数 |\n| ticket | ticket |\n\n### @Connection\n\n连接注解\n\n| 属性名称 | 说明 |\n|-|-\n| poolName | 不为 `null` 时，无视其他属性，直接用该连接池配置。默认为`null`，如果`host`、`port`、`user`、`password`都未设置，则获取默认的连接池。 |\n| host | 主机 |\n| port | 端口 |\n| user | 用户名 |\n| vhost | vhost，默认`/` |\n| insist | insist |\n| loginMethod | 默认`AMQPLAIN` |\n| loginResponse | loginResponse |\n| locale | 默认`en_US` |\n| connectionTimeout | 连接超时 |\n| readWriteTimeout | 读写超时 |\n| keepalive | keepalive，默认`false` |\n| heartbeat | 心跳时间，默认`0` |\n| channelRpcTimeout | 频道 RPC 超时时间，默认`0.0` |\n| sslProtocol | ssl 协议，默认`null` |\n\n### 队列组件支持\n\n本组件额外实现了 [imiphp/imi-queue](https://github.com/imiphp/imi-queue) 的接口，可以用 Queue 组件的 API 进行调用。\n\n只需要将队列驱动配置为：`AMQPQueueDriver`\n\n配置示例：\n\n```php\n[\n    'components'    =\u003e  [\n        'AMQP'  =\u003e  'Imi\\AMQP',\n    ],\n    'beans' =\u003e  [\n        'AutoRunProcessManager' =\u003e  [\n            'processes' =\u003e  [\n                // 加入队列消费进程，非必须，你也可以自己写进程消费\n                'QueueConsumer',\n            ],\n        ],\n        'imiQueue'  =\u003e  [\n            // 默认队列\n            'default'   =\u003e  'test1',\n            // 队列列表\n            'list'  =\u003e  [\n                // 队列名称\n                'test1' =\u003e  [\n                    // 使用的队列驱动\n                    'driver'        =\u003e  'AMQPQueueDriver',\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  'AConsumer',\n                    // 驱动类所需要的参数数组\n                    'config'        =\u003e  [\n                        // AMQP 连接池名称\n                        'poolName'      =\u003e  'amqp',\n                        // Redis 连接池名称\n                        'redisPoolName'=\u003e  'redis',\n                        // Redis 键名前缀\n                        'redisPrefix'   =\u003e  'test1:',\n                        // 可选配置：\n                        // 支持消息删除功能，依赖 Redis\n                        'supportDelete' =\u003e  true,\n                        // 支持消费超时队列功能，依赖 Redis，并且自动增加一个队列\n                        'supportTimeout' =\u003e  true,\n                        // 支持消费失败队列功能，自动增加一个队列\n                        'supportFail' =\u003e  true,\n                        // 循环尝试 pop 的时间间隔，单位：秒\n                        'timespan'  =\u003e  0.03,\n                        // 本地缓存的队列长度。由于 AMQP 不支持主动pop，而是主动推送，所以本地会有缓存队列，这个队列不宜过大。\n                        'queueLength'   =\u003e  16,\n                        // 消息类名\n                        'message'   =\u003e  \\Imi\\AMQP\\Queue\\JsonAMQPMessage::class,\n                    ]\n                ],\n            ],\n        ],\n    ]\n]\n```\n\n消费者类写法，与`imi-queue`组件用法一致。\n\n具体可以参考：\u003cexample/AMQP/QueueTest\u003e、\u003cexample/ApiServer/Controller/IndexController.php\u003e\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-amqp` 遵循 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-amqp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimiphp%2Fimi-amqp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-amqp/lists"}