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)

你可能感兴趣的:(Python开启SSL连接Azure Redis Cluster)