【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?

问题描述

使用6379端口连接Azure Redis服务,连接失败。由于默认情况下Azure Redis的设置没有打开6379的端口。需要使用SSL(6380端口)进行连接,但是遇见了无法连接的问题。

使用非SSL(6379端口)的连接代码

<?php
echo "Azure Redis Connect Test";
echo  "<br/>";

//连接本地的 Redis 服务
$redis = new Redis();
$redis->connect( xxxxxxxx.redis.cache.chinacloudapi.cn , 6379);
$redis->auth("秘钥");

echo "Connection to server sucessfully";
echo "<br/>";

//设置 redis 字符串数据
$redis->set("test-name", "Redis test value");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("test-name");
?>

如果直接修改端口号 6379 为 6380, 报错无法访问。那么使用6380的正确方式为?

问题解答

实则,代码的改动方式超级小。在 connection 方法中添加 TLS及6380端口。

改动前:$redis->connect( xxxxxxxx.redis.cache.chinacloudapi.cn , 6379);

改动后:$redis->connect( tls://xxxxxxxx.redis.cache.chinacloudapi.cn , 6380);

但是必定要注意php redis 扩展,由于扩展版本过低,有可能会引发解析问题。如类似如下的错误消息:

Azure Redis Connect Test<br/>PHP Warning:  
      Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/myweb/redis_tes
t2.php on line 6 PHP Fatal error:  Uncaught RedisException: php_network_getaddresses: getaddrinfo failed: Name or service not known in /data/myweb/redis_test2.php:6 Stack trace:
#0 /data/myweb/redis_test2.php(6): Redis->connect( tls://xxxx-xxxx... , 6380) #1 {main}
  thrown in /data/myweb/redis_test2.php on line 6

php redis 扩展的版本:

【Azure Redis 缓存】Azure Redis服务开启了SSL(6380端口), PHP如何访问缓存呢?

使用TLS 6380 端口连接Redis的示例代码为:

<?php
echo "Azure Redis Connect Test";
echo  "<br/>";

//连接本地的 Redis 服务
$redis = new Redis();
//$redis->connect( xxxxxxxx.redis.cache.chinacloudapi.cn , 6379);
$redis->connect( tls://xxxxxxxx.redis.cache.chinacloudapi.cn , 6380);
$redis->auth("秘钥");

echo "Connection to server sucessfully";
echo "<br/>";

//设置 redis 字符串数据
$redis->set("test-name", "Redis test value");
// 获取存储的数据并输出
echo "Stored string in redis:: " . $redis->get("test-name");
?>

参考资料

php redis example : https://github.com/phpredis/phpredis#example-2

php redis extension : https://pecl.php.net/package/redis

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

分类: 【Azure Redis 缓存】

标签: PHP Redis, connect( xxxxxxxx.redis.cache.xxx.cn , 6379);, $redis->connect( tls://xxxxxxxx.cn , 6380);, Azure Redis Connect Test

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
浙江少儿频道的头像 - 鹿快
评论 抢沙发

请登录后发表评论

    暂无评论内容