Laravel 不发布到 Redis [英] Laravel not publishing to Redis

查看:115
本文介绍了Laravel 不发布到 Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的本地 RESTful API 中实现 Redis 发布,该 API 是在 Laravel 中构建的,目的是稍后使用 Web 套接字实现聊天系统.我打算稍后从 Node.JS 服务器读取它们.

I am trying to implement Redis publishing in my local RESTful API which is built in Laravel for the purposes of implementing a chat system later on with Web Sockets. I intend to read them from a Node.JS server later on.

我正在使用 Redis::publish 向我的 test-channel 发布一条简单的消息.
然而,出于某种原因,Laravel 似乎没有发布到它.
我还注意到,当我调用 Redis::set 时,我设置的任何内容都不会保留在 Redis 中,但是使用 Redis::get 我可以读取我正在设置.

I am using Redis::publish to publish a simple message to my test-channel.
However, for some reason Laravel doesn't seem to publish to it.
I have also noticed that when I call Redis::set, whatever I set doesn't get persisted in Redis, but using Redis::get I can read the values that I'm setting.

public function redis(Request $request) {
    $data = $request->validate([
        'message' => 'string|required'
    ]);

    Redis::publish('test-channel', 'a test message');

    return 'Done';
}

我在 api/redis 路由中使用了上面的代码:

I am using the code above in the api/redis route:

Route::post('/redis', 'API\MessageController@redis');

我已使用 redis-cli 命令订阅了 test-channel.
如果我在终端实例中使用 redis-cli 手动将消息发布到 test-channel,我会正确接收我正在发布的消息.但是,出于某种原因,它们似乎没有与 Laravel 一起发布.
在运行 php artisan serve 并访问上述路由时,我可以注意到 Laravel 记录以下内容:

I have subscribed to the test-channel using the redis-cli command.
If I manually publish a message to the test-channel using the redis-cli in a terminal instance, I properly receive the messages that I am publishing. However, they don't seem to get published with Laravel for some reason.
What I can notice while running php artisan serve and visiting the aforementioned route is Laravel logging the following:

[*timestamp*] 127.0.0.1:39448 Accepted
[*timestamp*] 127.0.0.1:39448 Closing

127.0.0.1 之后的端口似乎是随机的.

The port after 127.0.0.1 appears to be random.

我尝试了 php-redis php 扩展和 predis 包,只是为了确保它不是其中的任何一个,但是我得到了相同的结果.我目前正在使用 php-redis,在 /etc/php/config.d 中启用了 igbinary 和 redis 扩展,并从 config/app.php 中删除了 Redis 别名.
我在 Manjaro 上使用 PHP 7.4、Laravel 6.0 和 Redis 5.0.7.

I tried both php-redis php extension and the predis package, just to be sure that it isn't any one of them, but I get the same result with both of them. I am currently using php-redis with both igbinary and redis extensions enabled in /etc/php/config.d and have removed the Redis alias from config/app.php.
I am using PHP 7.4, Laravel 6.0 and Redis 5.0.7 on Manjaro.

推荐答案

去过那里,发现:

$ redis-client
psubscribe *

会告诉你发生了什么.

您的默认 config/database.php 可能包含以下内容:

Chances are that your default config/database.php contains something like:

    'redis' => [

    'client' => env('REDIS_CLIENT', 'predis'),

    'options' => [
        'cluster' => env('REDIS_CLUSTER', 'redis'),
        'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
    ],

在这种情况下,频道名称将使用此前缀选项作为前缀.所以你可以评论这个选项,或者,如果你保留它,一定要订阅正确的频道

In that case, the channel name will be prefixed with this prefix option. So you can just comment this option, or, if you keep it, be sure to subscribe to the right channel

Redis::publish('test-channel', 'a test message');

$prefix = config('database.redis.options.prefix');
$channel = $prefix . 'test-channel';

return "Done. (published on $channel)";

这篇关于Laravel 不发布到 Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆