我正在尝试使用信息https://github.com/nrk/predis连接到具有predis 1.1和SSL的Redis,其中在示例中使用以下配置:
// Named array of connection parameters:
$client = new Predis\Client([
'scheme' => 'tls',
'ssl' => ['cafile' => 'private.pem', 'verify_peer' => true],
]);
我的Laravel配置如下所示:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'options' => [
'cluster' => 'redis',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
'scheme' => 'tls',
'ssl' => ['verify_peer' => false],
],
],
由于我没有用于SSL的密钥,因此我禁用了对等验证(根据http://php.net/manual/en/context.ssl.php).
不幸的是我收到以下错误:
ConnectionException in AbstractConnection.php line 155:
Error while reading line from the server. [tcp://MY_REDIS_SERVER_URL:6380]
建议表示赞赏:)
解决方法:
我能够让它上班!
您需要将’scheme’从’options’移动到’default’:
我的工作配置:
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'scheme' => 'tls',
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'options' => [
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
'ssl' => ['verify_peer' => false],
],
],
注意:我还从’options’中删除了’cluster’选项,但我不怀疑这是这个问题的成败.
在我的最终决赛配置中,我将其更改为:’scheme’=> env(‘REDIS_SCHEME’,’tcp’),然后在我的env文件中定义REDIS_SCHEME = tls.
使用启用了TLS的AWS ElastiCache进行测试.
编辑:
以上配置仅适用于单节点redis.如果您碰巧启用了群集和TLS,那么您将完全需要不同的配置.
'redis' => [
'client' => 'predis',
'cluster' => env('REDIS_CLUSTER', false),
// Note! for single redis nodes, the default is defined here.
// keeping it here for clusters will actually prevent the cluster config
// from being used, it'll assume single node only.
//'default' => [
// ...
//],
// #pro-tip, you can use the Cluster config even for single instances!
'clusters' => [
'default' => [
[
'scheme' => env('REDIS_SCHEME', 'tcp'),
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
],
'options' => [ // Clustering specific options
'cluster' => 'redis', // This tells Redis Client lib to follow redirects (from cluster)
]
],
'options' => [
'parameters' => [ // Parameters provide defaults for the Connection Factory
'password' => env('REDIS_PASSWORD', null), // Redirects need PW for the other nodes
'scheme' => env('REDIS_SCHEME', 'tcp'), // Redirects also must match scheme
],
'ssl' => ['verify_peer' => false], // Since we dont have TLS cert to verify
]
]
解释上述内容:
>’client’=> ‘predis’:这指定要使用的PHP Library Redis驱动程序(predis).
>’cluster’=> ‘redis’:这告诉Predis假设服务器端集群.这只是意味着“跟随重定向”(例如-MOVED响应).使用群集运行时,节点将使用-MOVED响应您必须要求输入特定密钥的节点.
>如果没有使用Redis群集启用此功能,Laravel将抛出-MOVED异常1 / n次,n是Redis群集中的节点数(它会很幸运并且每隔一段时间就会询问正确的节点)
>’clusters’=> […]:指定一个节点列表,但只设置一个’default’并将其指向AWS ‘Configuration endpoint’将让它动态找到任何/所有其他节点(推荐用于Elasticache,因为你不知道节点何时出现’或goin’).
>’options’:对于Laravel,可以在*,集群级和节点选项中指定. (他们在被传递到Predis之前被合并在Illuminate中)
>’parameters’:这些’覆盖’Predis用于新连接的默认连接设置/假设.由于我们明确地为“默认”连接设置它们,因此不使用它们.但对于群集设置,它们至关重要. “主”节点可以发回重定向(-MOVED),除非为密码和方案设置参数,否则它将假设默认值,并且与新节点的新连接将失败.