如何在 laravel 中同时拥有集群和非集群 redis 连接
How to have both clustered and non-clustered redis connections in laravel
背景
过去我可以像这样在我的配置中使用非集群的 redis:
'redis' => [
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => true,
]
],
然而,由于我们的 redis 服务器上的负载,我必须对我的 redis 进行集群,当我拥有的 only redis 连接被集群时,此配置工作正常(在很多 ):
'redis' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'parameters' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_DEFAULT_PORT', 6379),
'database' => 0,
],
],
'clusters' => [
'default' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
'shard2' => [
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
'shard3' => [
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis'
],
]
]
我的任何 env 文件都像这样(无论如何对于我的本地主机):
QUEUE_DRIVER=redis // cluster compatible
BROADCAST_DRIVER=redis // cluster compatible
CACHE_CONNECTION=redis // cluster incompatible
REDIS_CLUSTER=true
REDIS_HOST=localhost
REDIS_DEFAULT_PORT=7000
REDIS_SHARD_1_HOST=localhost
REDIS_SHARD_2_HOST=localhost
REDIS_SHARD_3_HOST=localhost
REDIS_SHARD_1_PORT=7000
REDIS_SHARD_2_PORT=7001
REDIS_SHARD_3_PORT=7002
问题
事实是,目前,我们使用非集群redis的原因如下:
- 缓存:支持redis集群
- Queue/Jobs: 支持redis集群
- 广播(即websockets):不支持redis集群
这就是为什么我们需要同时拥有两个redis连接,这样我们就可以为caching/queues使用集群连接,为websockets使用非集群连接。
但这不起作用:
'redis' => [
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => true,
],
'clustered' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'parameters' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_DEFAULT_PORT', 6379),
'database' => 0,
],
],
'clusters' => [
'default' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
'shard2' => [
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
'shard3' => [
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis'
],
]
此外,一些用户 state 认为这样的任务对于 redis 来说根本不可能。是真的吗?
更新
我试过了
在Cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'clustered',
],
注意:在上面的连接中,我不能只是 copy/paste 集群选项,因为如果我不放置驱动程序选项它会崩溃
在database.php中我受到这个的启发,简单的在redis key下放了不同的连接:(即`database.redis.connection- 1, database.redis.connection-2 等)
'redis' => [
'clustered' => [
// clustered settings copied from above
],
],
'default' => [
// non clustered settings
],
]
为了测试,我运行以下修补匠
>>> use Illuminate\Support\Facades\Cache;
>>> Cache::put('foo','bar',1);
Predis/Response/ServerException with message 'MOVED 7837 127.0.0.1:7001'
移动错误是已知错误,只是 saying 我正在处理非集群 redis 连接。
想法?
我可以用 PR 解决这个问题。
这是我现在的配置:
'redis' => [
'clustered' => [
'client' => 'predis',
'cluster' => true,
'options' => [ 'cluster' => 'redis' ],
'clusters' => [
[
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
[
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
[
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
],
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => false,
],
]
很快就会向 Laravel 本身发送 PR。
背景
过去我可以像这样在我的配置中使用非集群的 redis:
'redis' => [
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => true,
]
],
然而,由于我们的 redis 服务器上的负载,我必须对我的 redis 进行集群,当我拥有的 only redis 连接被集群时,此配置工作正常(在很多
'redis' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'parameters' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_DEFAULT_PORT', 6379),
'database' => 0,
],
],
'clusters' => [
'default' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
'shard2' => [
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
'shard3' => [
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis'
],
]
]
我的任何 env 文件都像这样(无论如何对于我的本地主机):
QUEUE_DRIVER=redis // cluster compatible
BROADCAST_DRIVER=redis // cluster compatible
CACHE_CONNECTION=redis // cluster incompatible
REDIS_CLUSTER=true
REDIS_HOST=localhost
REDIS_DEFAULT_PORT=7000
REDIS_SHARD_1_HOST=localhost
REDIS_SHARD_2_HOST=localhost
REDIS_SHARD_3_HOST=localhost
REDIS_SHARD_1_PORT=7000
REDIS_SHARD_2_PORT=7001
REDIS_SHARD_3_PORT=7002
问题
事实是,目前,我们使用非集群redis的原因如下:
- 缓存:支持redis集群
- Queue/Jobs: 支持redis集群
- 广播(即websockets):不支持redis集群
这就是为什么我们需要同时拥有两个redis连接,这样我们就可以为caching/queues使用集群连接,为websockets使用非集群连接。
但这不起作用:
'redis' => [
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => true,
],
'clustered' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'cluster' => 'redis',
'parameters' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_DEFAULT_PORT', 6379),
'database' => 0,
],
],
'clusters' => [
'default' => [
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
'shard2' => [
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
'shard3' => [
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis'
],
]
此外,一些用户 state 认为这样的任务对于 redis 来说根本不可能。是真的吗?
更新
我试过了
在Cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'clustered',
],
注意:在上面的连接中,我不能只是 copy/paste 集群选项,因为如果我不放置驱动程序选项它会崩溃
在database.php中我受到这个
'redis' => [
'clustered' => [
// clustered settings copied from above
],
],
'default' => [
// non clustered settings
],
]
为了测试,我运行以下修补匠
>>> use Illuminate\Support\Facades\Cache;
>>> Cache::put('foo','bar',1);
Predis/Response/ServerException with message 'MOVED 7837 127.0.0.1:7001'
移动错误是已知错误,只是 saying 我正在处理非集群 redis 连接。
想法?
我可以用 PR 解决这个问题。
这是我现在的配置:
'redis' => [
'clustered' => [
'client' => 'predis',
'cluster' => true,
'options' => [ 'cluster' => 'redis' ],
'clusters' => [
[
'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_1_PORT', 6379),
'database' => 0,
],
[
'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_2_PORT', 6379),
'database' => 0,
],
[
'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_SHARD_3_PORT', 6379),
'database' => 0,
],
],
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => 6379,
'database' => 0,
'cluster' => false,
],
]
很快就会向 Laravel 本身发送 PR。