2023-07-07 08:20:30 +00:00
|
|
|
|
# rocket-mq-thinkphp 适配客户端
|
|
|
|
|
|
|
|
|
|
## 使用说明
|
2023-07-09 10:19:57 +00:00
|
|
|
|
本客户端对接公司mq,实现mq消息推送与订阅,目前支持一对一与一对多订阅!
|
2023-07-07 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
### 安装
|
|
|
|
|
|
2023-07-09 10:19:57 +00:00
|
|
|
|
1. 添加镜像地址
|
|
|
|
|
```shell
|
|
|
|
|
{
|
|
|
|
|
"repositories": [{
|
|
|
|
|
"type": "composer",
|
|
|
|
|
"url": "http://composer.saas.test.tianmagroup.com"
|
|
|
|
|
}]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2. composer安装
|
|
|
|
|
```shell
|
|
|
|
|
composer require tm_xls/rocket-mq-client-thinkphp
|
|
|
|
|
```
|
2023-07-12 03:50:55 +00:00
|
|
|
|
> 安装过程中需要允许`tm_xls/composer-thinkphp-installer`插件运行以便自动生成配置文件
|
2023-07-07 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
### 配置
|
2023-07-09 10:19:57 +00:00
|
|
|
|
```php
|
2023-07-09 10:59:57 +00:00
|
|
|
|
|
|
|
|
|
|
2023-07-09 10:19:57 +00:00
|
|
|
|
return [
|
2023-07-09 10:59:57 +00:00
|
|
|
|
'host' => env('mq_host', 'http://192.168.21.170:8085'), //mq地址
|
|
|
|
|
'callback_host' => env('mq_callback_host', 'http://192.168.21.170:8085'),//回调host
|
|
|
|
|
'callback' => '/rocketmq/producer/notify',//回调地址默认不做修改
|
2023-07-09 11:00:29 +00:00
|
|
|
|
//订阅节点
|
2023-07-09 10:59:57 +00:00
|
|
|
|
'subscribe' => [
|
|
|
|
|
|
2023-07-09 10:19:57 +00:00
|
|
|
|
]
|
|
|
|
|
];
|
2023-07-07 08:20:30 +00:00
|
|
|
|
|
2023-07-09 10:19:57 +00:00
|
|
|
|
```
|
2023-07-07 08:20:30 +00:00
|
|
|
|
|
|
|
|
|
### 生产者
|
2023-07-09 10:19:57 +00:00
|
|
|
|
见example "Producer"
|
2023-07-07 08:20:30 +00:00
|
|
|
|
|
2023-07-09 10:12:14 +00:00
|
|
|
|
### 消费者
|
|
|
|
|
消费者执行逻辑由开发者定义,但是仍遵循如下规则:
|
|
|
|
|
1. 首先在config/rocket_mq.php 文件subscribe节点配置订阅
|
|
|
|
|
```php
|
|
|
|
|
return [
|
|
|
|
|
'subscribe' => [
|
|
|
|
|
//节点配置 由mq的topic.tag为标识
|
|
|
|
|
'[topic].[tag]' => [
|
|
|
|
|
Test:class
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2. `Test:class` 需要实现 `tm\xls\rocketMq\thinkphp\consumer\contract\ListenerInterface` 接口
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace example;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use tm\xls\rocketMq\thinkphp\consumer\contract\ListenerInterface;
|
|
|
|
|
use tm\xls\rocketMq\thinkphp\consumer\message\Message;
|
|
|
|
|
|
|
|
|
|
class Consumer implements ListenerInterface
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private $message;
|
|
|
|
|
|
2023-07-11 06:26:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* 所有订阅监听会自动注入 tm\xls\rocketMq\thinkphp\consumer\message\Message 对象
|
|
|
|
|
*
|
|
|
|
|
* @param Message $message
|
|
|
|
|
*/
|
2023-07-09 10:12:14 +00:00
|
|
|
|
public function __construct(Message $message)
|
|
|
|
|
{
|
|
|
|
|
$this->message = $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
|
{
|
2023-07-11 06:26:17 +00:00
|
|
|
|
//队列消息
|
|
|
|
|
$this->message;
|
2023-07-09 10:12:14 +00:00
|
|
|
|
//处理订阅逻辑
|
2023-07-11 06:26:17 +00:00
|
|
|
|
// code ...
|
2023-07-09 10:12:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function enable(): bool
|
|
|
|
|
{
|
|
|
|
|
// 这边判断是否执行
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 03:50:55 +00:00
|
|
|
|
```
|