在 Laravel 中配置高可用的 Redis 集群
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28309671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring highly available Redis cluster in Laravel
提问by Webnet
I'm trying to update my Laravel application so that Queue::push()
pushes jobs to a redis queue cluster. The guy setting that up is communicating to me that our application needs to be configured with connection details for the primary master and several slaves. If this is the correct way to set this up I'm struggling to figure out how to configure this.
我正在尝试更新我的 Laravel 应用程序,以便Queue::push()
将作业推送到 redis 队列集群。负责设置的人告诉我,我们的应用程序需要配置主主站和几个从站的连接详细信息。如果这是设置它的正确方法,我正在努力弄清楚如何配置它。
Out of the box the redis config looks something like...
开箱即用的 redis 配置看起来像......
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
I've been digging through the Laravel driver trying to figure out how to configure masters and slaves within Laravel and haven't been able to figure it out. How can I add slaves here?
我一直在研究 Laravel 驱动程序,试图弄清楚如何在 Laravel 中配置主设备和从设备,但一直无法弄清楚。我如何在这里添加奴隶?
Or is this the wrong direction?
或者这是错误的方向?
回答by AndrewMcLagan
As of nrk/predisv1.1.0
there is built in support for the Redis SentinelAPI.
从nrk/predis 开始,v1.1.0
内置了对Redis SentinelAPI 的支持。
There are slight configuration changes you will need to apply to Laravel in order to utilise this capability. The easiest way is to do this is use one of the redis sentinel packages for Laravel. These are simply wrappers for the Laravel driver and take care of the configuration. One that I personally use in a large project:
为了利用此功能,您需要对 Laravel 应用一些细微的配置更改。最简单的方法是使用 Laravel 的 redis 哨兵包之一。这些只是 Laravel 驱动程序的包装器并负责配置。我个人在一个大型项目中使用的一个:
cooperaj/laravel-redis-sentinel
cooperaj/laravel-redis-sentinel
What is Redis Sentinel?
什么是Redis哨兵?
Redis Sentinel is a seperate service that works with a Redis Cluster, monitoring its health for a highly available cluster with automatic failover.
Redis Sentinel 是一个单独的服务,它与Redis Cluster 一起工作,通过自动故障转移来监控高可用集群的运行状况。
How do you use it?
你如何使用它?
We run our entire application in Kubernetes, that includes a Laravel 5.2 API and React/Node websites running from that API. We have a Redis Sentinel cluster configured similar to this. We also run an Elasticsearch cluster similar to this.
我们在Kubernetes 中运行我们的整个应用程序,其中包括 Laravel 5.2 API 和从该 API 运行的 React/Node 网站。我们有一个与此配置类似的 Redis Sentinel 集群。我们还运行了一个与此类似的 Elasticsearch 集群。
What is your hardware?
你的硬件是什么?
Our entire operations / systems layer is built on a Google Container Enginecluster.
我们的整个运营/系统层建立在Google Container Engine集群上。
Why is this important?
为什么这很重要?
If you rely on Redis for your Laravel caching or queues your application will fail if Redis fails for any reason.
如果您依赖 Redis 进行 Laravel 缓存或队列,如果 Redis 因任何原因失败,您的应用程序将失败。
回答by Webnet
Laravel's Redis driver only supports Redis Cluster, which is sharded. If you need an HA Redis system you'll need to use Sentinelswhich means you can use https://github.com/Indatus/laravel-PSRedis
Laravel 的 Redis 驱动只支持 Redis Cluster,它是分片的。如果您需要 HA Redis 系统,则需要使用Sentinels,这意味着您可以使用https://github.com/Indatus/laravel-PSRedis
回答by devcube
With Predis you can try like this:
使用 Predis,您可以这样尝试:
<?php
# file: config/database.php
return [
// (...)
'redis' => [
'client' => 'predis',
'cluster' => true,
'options' => [
'replication' => true,
],
'default' => [
'scheme' => 'tcp',
'host' => 'localhost',
'password' => null,
'port' => 6379,
'alias' => 'master',
'database' => 0,
],
'slave-001' => [
'scheme' => 'tcp',
'host' => 'slave1host',
'port' => 6379,
'alias' => 'slave-001',
'database' => 0,
],
'slave-002' => [
'scheme' => 'tcp',
'host' => 'slave2host',
'port' => 6379,
'alias' => 'slave-002',
'database' => 0,
],
// add more slaves if needed
],
]