Parcourir la source

tested redis queue

mx il y a 3 ans
Parent
commit
ea50426f7e

+ 4 - 1
README.md

@@ -64,4 +64,7 @@ see [https://www.workerman.net/doc/webman/process.html](https://www.workerman.ne
 
 ### Middlewares
 
-see [official doc](https://www.workerman.net/doc/webman/middleware.html)
+see [official doc](https://www.workerman.net/doc/webman/middleware.html)
+
+### Redis Queue
+

+ 17 - 0
app/controller/User.php

@@ -4,6 +4,7 @@ namespace app\controller;
 
 use support\Request;
 use app\model\User as UserModel;
+use Webman\RedisQueue\Client;
 
 class User
 {
@@ -19,4 +20,20 @@ class User
         return json($user);
     }
 
+    public function access(Request $request)
+    {
+        $queue = 'access';
+        $data = [
+            'ip' => $request->getRemoteIp(),
+            'time' => date('Y-m-d H:i:s'),
+        ];
+
+        Client::send($queue, $data);
+
+        $data['delay'] = true;
+        // delay handler
+        Client::send($queue, $data, 60);
+
+        return json($data);
+    }
 }

+ 21 - 0
app/queue/redis/PrintAccessor.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace app\queue\redis;
+
+use Webman\RedisQueue\Consumer;
+
+class PrintAccessor implements Consumer
+{
+    // 要消费的队列名
+    public $queue = 'access';
+
+    // 连接名,对应 plugin/webman/redis-queue/redis.php 里的连接`
+    public $connection = 'default';
+
+    // 消费
+    public function consume($data)
+    {
+        // 无需反序列化
+        var_export($data);
+    }
+}

+ 2 - 1
composer.json

@@ -33,7 +33,8 @@
     "robmorgan/phinx": "^0.12.10",
     "illuminate/redis": "^8.83",
     "symfony/cache": "^5.4",
-    "webman/console": "^1.0"
+    "webman/console": "^1.0",
+    "webman/redis-queue": "^1.1"
   },
   "suggest": {
     "ext-event": "For better performance. "

+ 103 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "1a5c089f6890394e174e43150171a25f",
+    "content-hash": "f716421c6bb3c5add8c4fd4b6c47eba9",
     "packages": [
         {
             "name": "cakephp/core",
@@ -3049,6 +3049,108 @@
             },
             "time": "2022-03-25T02:01:45+00:00"
         },
+        {
+            "name": "webman/redis-queue",
+            "version": "v1.1.2",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/webman-php/redis-queue.git",
+                "reference": "70c430453c6ddf5256660d0d33ee4e5f4ce7530d"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/webman-php/redis-queue/zipball/70c430453c6ddf5256660d0d33ee4e5f4ce7530d",
+                "reference": "70c430453c6ddf5256660d0d33ee4e5f4ce7530d",
+                "shasum": ""
+            },
+            "require": {
+                "workerman/redis-queue": "^1.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Webman\\RedisQueue\\": "./src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "description": "Redis message queue plugin for webman.",
+            "support": {
+                "issues": "https://github.com/webman-php/redis-queue/issues",
+                "source": "https://github.com/webman-php/redis-queue/tree/v1.1.2"
+            },
+            "time": "2022-03-25T08:58:10+00:00"
+        },
+        {
+            "name": "workerman/redis",
+            "version": "v1.0.7",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/walkor/redis.git",
+                "reference": "5e65740b72042130b9c928a2be7bebb8609a1f2e"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/walkor/redis/zipball/5e65740b72042130b9c928a2be7bebb8609a1f2e",
+                "reference": "5e65740b72042130b9c928a2be7bebb8609a1f2e",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.4",
+                "workerman/workerman": ">=3.5.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Workerman\\Redis\\": "./src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "homepage": "http://www.workerman.net",
+            "support": {
+                "issues": "https://github.com/walkor/redis/issues",
+                "source": "https://github.com/walkor/redis/tree/v1.0.7"
+            },
+            "time": "2021-10-13T10:28:56+00:00"
+        },
+        {
+            "name": "workerman/redis-queue",
+            "version": "v1.0.6",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/walkor/redis-queue.git",
+                "reference": "6f6d51aa2e485965625a06c42881c9394dff7548"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/walkor/redis-queue/zipball/6f6d51aa2e485965625a06c42881c9394dff7548",
+                "reference": "6f6d51aa2e485965625a06c42881c9394dff7548",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.4",
+                "workerman/redis": "^1.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "Workerman\\RedisQueue\\": "./src"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "description": "Message queue system written in PHP based on workerman and backed by Redis.",
+            "homepage": "http://www.workerman.net",
+            "support": {
+                "issues": "https://github.com/walkor/redis-queue/issues",
+                "source": "https://github.com/walkor/redis-queue/tree/v1.0.6"
+            },
+            "time": "2022-03-22T15:27:02+00:00"
+        },
         {
             "name": "workerman/webman-framework",
             "version": "v1.3.6",

+ 1 - 1
config/app.php

@@ -16,7 +16,7 @@ use support\Request;
 
 return [
     'debug' => true,
-    'default_timezone' => 'Asia/Shanghai',
+    'default_timezone' => 'Asia/Bangkok',
     'request_class' => Request::class,
     'public_path' => base_path() . DIRECTORY_SEPARATOR . 'public',
     'runtime_path' => base_path(false) . DIRECTORY_SEPARATOR . 'runtime',

+ 14 - 14
config/database.php

@@ -30,19 +30,19 @@ return [
             'sslmode'=>'prefer',
         ],
 
-        'mysql' => [
-            'driver' => 'mysql',
-            'host'  => '127.0.0.1',
-            'port' => 3306,
-            'database' => 'test',
-            'username' => 'root',
-            'password' => '',
-            'unix_socket' => '',
-            'charset' => 'utf8',
-            'collation' => 'utf8_unicode_ci',
-            'prefix' => '',
-            'strict' => true,
-            'engine' => null,
-        ],
+        // 'mysql' => [
+        //     'driver' => 'mysql',
+        //     'host'  => '127.0.0.1',
+        //     'port' => 3306,
+        //     'database' => 'test',
+        //     'username' => 'root',
+        //     'password' => '',
+        //     'unix_socket' => '',
+        //     'charset' => 'utf8',
+        //     'collation' => 'utf8_unicode_ci',
+        //     'prefix' => '',
+        //     'strict' => true,
+        //     'engine' => null,
+        // ],
     ],
 ];

+ 4 - 0
config/plugin/webman/redis-queue/app.php

@@ -0,0 +1,4 @@
+<?php
+return [
+    'enable' => true,
+];

+ 7 - 0
config/plugin/webman/redis-queue/command.php

@@ -0,0 +1,7 @@
+<?php
+
+use Webman\RedisQueue\Command\MakeConsumerCommand;
+
+return [
+    MakeConsumerCommand::class
+];

+ 11 - 0
config/plugin/webman/redis-queue/process.php

@@ -0,0 +1,11 @@
+<?php
+return [
+    'consumer'  => [
+        'handler'     => Webman\RedisQueue\Process\Consumer::class,
+        'count'       => 1, // 可以设置多进程同时消费
+        'constructor' => [
+            // 消费者类目录
+            'consumer_dir' => app_path() . '/queue/redis'
+        ]
+    ]
+];

+ 12 - 0
config/plugin/webman/redis-queue/redis.php

@@ -0,0 +1,12 @@
+<?php
+return [
+    'default' => [
+        'host' => 'redis://127.0.0.1:6379',
+        'options' => [
+            'auth' => '123456',   // 密码,可选参数
+            'db' => 5,            // 数据库
+            'max_attempts'  => 2, // 消费失败后,重试次数
+            'retry_seconds' => 5, // 重试间隔,单位秒
+        ]
+    ],
+];

+ 2 - 2
phinx.php

@@ -11,7 +11,7 @@ return
         'default_environment' => 'development',
         'prod' => [
             'adapter' => 'pgsql',
-            'host' => 'localhost',
+            'host' => 'prod',
             'name' => 'production_db',
             'user' => 'root',
             'pass' => '',
@@ -29,7 +29,7 @@ return
         ],
         'test' => [
             'adapter' => 'pgsql',
-            'host' => 'localhost',
+            'host' => 'test',
             'name' => 'testing_db',
             'user' => 'root',
             'pass' => '',