繁体   English   中英

Laravel 通过 Redis 排队:local.ERROR:连接到 127.0.0.1:6379 时出现读取错误

[英]Laravel Queue via Redis: local.ERROR: read error on connection to 127.0.0.1:6379

我试图通过 redis 连接运行 laravel 队列,并且在工作人员启动几秒钟后,我收到以下错误。

队列命令:

$> php artisan queue:work --tries=1 --queue=test_queue

错误:

local.ERROR: read error on connection to 127.0.0.1:6379 {"exception":"[object] (RedisException(code: 0): read error on connection to 127.0.0.1:6379 at vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\Connection.php:116)
[stacktrace]
#0 vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\Connection.php(116): Redis->blPop(Array, 0)
#1 vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\PhpRedisConnection.php(440): Illuminate\\Redis\\Connections\\Connection->command('blpop', Array)
#2 vendor\\laravel\\framework\\src\\Illuminate\\Redis\\Connections\\PhpRedisConnection.php(179): Illuminate\\Redis\\Connections\\PhpRedisConnection->command('blpop', Array)
#3 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\RedisQueue.php(233): Illuminate\\Redis\\Connections\\PhpRedisConnection->blpop(Array, 0)
#4 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\RedisQueue.php(169): Illuminate\\Queue\\RedisQueue->retrieveNextJob('queues:test_queue')
#5 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Worker.php(270): Illuminate\\Queue\\RedisQueue->pop('test_queue')
#6 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Worker.php(123): Illuminate\\Queue\\Worker->getNextJob(Object(Illuminate\\Queue\\RedisQueue), 'test_queue')
#7 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Console\\WorkCommand.php(112): Illuminate\\Queue\\Worker->daemon('redis', 'test_queue', Object(Illuminate\\Queue\\WorkerOptions))
#8 vendor\\laravel\\framework\\src\\Illuminate\\Queue\\Console\\WorkCommand.php(96): Illuminate\\Queue\\Console\\WorkCommand->runWorker('redis', 'test_queue')
#9 [internal function]: Illuminate\\Queue\\Console\\WorkCommand->handle()
#10 vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(32): call_user_func_array(Array, Array)
#11 vendor\\laravel\\framework\\src\\Illuminate\\Container\\Util.php(36): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#12 vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(90): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#13 vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(34): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#14 vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(590): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#15 vendor\\laravel\\framework\\src\\Illuminate\\Console\\Command.php(201): Illuminate\\Container\\Container->call(Array)
#16 vendor\\symfony\\console\\Command\\Command.php(255): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#17 vendor\\laravel\\framework\\src\\Illuminate\\Console\\Command.php(188): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#18 vendor\\symfony\\console\\Application.php(1012): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#19 vendor\\symfony\\console\\Application.php(272): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Queue\\Console\\WorkCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#20 vendor\\symfony\\console\\Application.php(148): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#21 vendor\\laravel\\framework\\src\\Illuminate\\Console\\Application.php(93): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#22 vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Console\\Kernel.php(131): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#23 artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#24 {main}
"}

这是队列配置:

'redis' => [
    'driver' => 'redis',
    'connection' => 'laravel_queues',
    'queue' => env('REDIS_QUEUE', 'default'),
    'retry_after' => 190, 
    'block_for' => 0,
],

这是连接配置:

'laravel_queues' => [
    'host' => env('REDIS_HOST', 'localhost'),
    'password' => env('REDIS_PASSWORD', null),
    'port' => env('REDIS_PORT', '6379'),
    'database' => 2,
],

编辑 1

Redis 服务器已启动并运行,其他一切工作正常:

在此处输入图像描述

在此处输入图像描述


编辑 2

正如建议的那样,我在 CLI 上运行monitor ,这就是发生的事情:

1596784000.713047 [2 127.0.0.1:62141] "EVAL" "-- Get all of the jobs with an expired \"score\"...\nlocal val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1])\n\n-- If we have values in the array, we will remove them from the first queue\n-- and add them onto the destination queue in chunks of 100, which moves\n-- all of the appropriate jobs onto the destination queue very safely.\nif(next(val) ~= nil) then\n    redis.call('zremrangebyrank', KEYS[1], 0, #val - 1)\n\n    for i = 1, #val, 100 do\n        redis.call('rpush', KEYS[2], unpack(val, i, math.min(i+99, #val)))\n        -- Push a notification for every job that was migrated...\n        for j = i, math.min(i+99, #val) do\n            redis.call('rpush', KEYS[3], 1)\n        end\n    end\nend\n\nreturn val" "3" "LOCALHOSTqueues:test_queue:delayed" "LOCALHOSTqueues:test_queue" "LOCALHOSTqueues:test_queue:notify" "1596784000"
1596784000.713180 [2 lua] "zrangebyscore" "LOCALHOSTqueues:test_queue:delayed" "-inf" "1596784000"
1596784000.713362 [2 127.0.0.1:62141] "EVAL" "-- Get all of the jobs with an expired \"score\"...\nlocal val = redis.call('zrangebyscore', KEYS[1], '-inf', ARGV[1])\n\n-- If we have values in the array, we will remove them from the first queue\n-- and add them onto the destination queue in chunks of 100, which moves\n-- all of the appropriate jobs onto the destination queue very safely.\nif(next(val) ~= nil) then\n    redis.call('zremrangebyrank', KEYS[1], 0, #val - 1)\n\n    for i = 1, #val, 100 do\n        redis.call('rpush', KEYS[2], unpack(val, i, math.min(i+99, #val)))\n        -- Push a notification for every job that was migrated...\n        for j = i, math.min(i+99, #val) do\n            redis.call('rpush', KEYS[3], 1)\n        end\n    end\nend\n\nreturn val" "3" "LOCALHOSTqueues:test_queue:reserved" "LOCALHOSTqueues:test_queue" "LOCALHOSTqueues:test_queue:notify" "1596784000"
1596784000.713563 [2 lua] "zrangebyscore" "LOCALHOSTqueues:test_queue:reserved" "-inf" "1596784000"
1596784000.713972 [2 127.0.0.1:62141] "EVAL" "-- Pop the first job off of the queue...\nlocal job = redis.call('lpop', KEYS[1])\nlocal reserved = false\n\nif(job ~= false) then\n    -- Increment the attempt count and place job on the reserved queue...\n    reserved = cjson.decode(job)\n    reserved['attempts'] = reserved['attempts'] + 1\n    reserved = cjson.encode(reserved)\n    redis.call('zadd', KEYS[2], ARGV[1], reserved)\n    redis.call('lpop', KEYS[3])\nend\n\nreturn {job, reserved}" "3" "LOCALHOSTqueues:test_queue" "LOCALHOSTqueues:test_queue:reserved" "LOCALHOSTqueues:test_queue:notify" "1596784190"
1596784000.714130 [2 lua] "lpop" "LOCALHOSTqueues:test_queue"
1596784000.714340 [2 127.0.0.1:62141] "BLPOP" "LOCALHOSTqueues:test_queue:notify" "0"

我有一个 WebApp 应用程序连接到 Redis docker 并遇到了类似的问题。 这是我学到的:

我推荐使用 docker-compose(强烈推荐)它帮助解决了我的许多问题。 redis docker 的主机名将是 redis 容器的服务名称 - 请参见下面的“#”

   version: '3.9'
     services:
       php-service:
         image: <your-php-image>

         ports:
           - "8000:8000"
         depends_on:
           - redis
         expose:
           - "8000"

      redis:  # this is going to be the hostname of redis for you php-service
        image: <your-redis-image>
        ports:
          - "6379:6379"
        sysctls:
          net.core.somaxconn: '511'

要从 PHP-WebApp docker 中引用到 redis 主机的连接,请将主机名更改为

 redis://redis:6379/0 # notice the /0 after the port - I couldn't make it work without this -- also notice that the second redis (which is the host name - the first is the protocol name) matches the name of the service in docker-compose

If the PHP webapp is running on your local machine outside of a docker container while trying to connect to redis running inside a docker container, change the redis host name to

redis://localhost:6379/0 # notice the /0 is still necessary but instead of the service name inside of docker-compose being used as the host, localhost is used

祝你好运并评论你有任何问题 - 我想我可以提供帮助。

这是使用PHPRedis的一个很大的缺点,想知道Laravel开发人员是否知道这一点,因为 Predis 已被弃用以支持PHPRedis

这里的建议是使用Supervisor ,因为它将解决此问题和任何其他意外的连接丢失问题,并确保侦听器始终在运行,几乎不会停机。

命令的keep-alive选项应减轻此连接超时错误并立即重新开始侦听。 也许这会对你有所帮助。

详细解释请参考PHPRedis

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM