Python开启SSL连接Azure Redis Cluster

今天使用Python连接Azure Redis的时候遇到了一系列问题。

首先,使用Python的redis包连接Redis的时候一切顺利,但是过了一段时间就报了Moved Exception。这才想起来自己初始化Azure Redis时开启了Cluster。

Python连接Redis Cluster到不是什么难事,安装 redis-py-cluster即可。

$ pip install redis-py-cluster

官方文档示例配置如下:

>>> from rediscluster import StrictRedisCluster

>>> # Requires at least one node for cluster discovery. Multiple nodes is recommended.
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]

>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)

>>> rc.set("foo", "bar")
True
>>> print(rc.get("foo"))
'bar'

但是这样配置无法正常连接,因为我还配置了Azure Redis Cluster只使用SSL连接。redis-py-cluster的文档虽然没有使用SSL的示例,但是Release Note里有这么一句:

1.3.4 (Mar 5, 2017)
...
*Add SSLClusterConnection for connecting over TLS/SSL to Redis Cluster
...

所以最新版肯定支持SSL访问Redis Cluster。于是直接去GitHub翻代码。找到注释如下:

    Manages TCP communication over TLS/SSL to and from a Redis cluster
    Usage:
        pool = ClusterConnectionPool(connection_class=SSLClusterConnection, ...)
        client = StrictRedisCluster(connection_pool=pool)

按此方法调用后本以为大功告成,但是又返回了新的错误。

redis.exceptions.ResponseError: unknown command: CONFIG

好在微软官方论坛搜到了答案:

Azure Redis cache doesn't support the CONFIG command, but redis-py-cluster has a workaround for this:
From their release notes for 1.3.2:
If your redis instance is configured to not have the CONFIG ... comannds enabled due to security reasons you need to pass this into the client object skip_full_coverage_check=True. Benefits is that the client class no longer requires the CONFIG ... commands to be enabled on the server. Downsides is that you can't use the option in your redis server and still use the same feature in this client.

同时因为并不需要配置多个节点,所以最终代码示例如下:

from rediscluster import StrictRedisCluster
from rediscluster.connection import *

startup_nodes = [{"host": "domain", "port": "6380"}]
pool = ClusterConnectionPool(connection_class=SSLClusterConnection, startup_nodes=startup_nodes, password="*****",skip_full_coverage_check=True)
rc = StrictRedisCluster(connection_pool=pool)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,168评论 19 139
  • 5/15/2017 7:06:35 PM 纵观各大组件,配置文件占据极其重要的地位。可配置化也是当下开发的一流行趋...
    爱做梦的胖子阅读 4,461评论 0 8
  • Redis 配置文件示例 注意:想要读取配置文件,Redis的第一个参数必须是文件的路径 ./redis-serv...
    起个名忒难阅读 1,260评论 0 1
  • # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1...
    iyimao阅读 839评论 0 2
  • 大致流程: 1.如果Linux自带了Java,先卸载自带的jdk 2.下载相应的jdk,方式有很多种,比如:命令行...
    jackLee阅读 322评论 0 1