Archery支持Redis cluster查询

2023-05-15  本文已影响0人  青水山

Archery 目前在很多公司,都被DBA所青睐。不仅可以作为数据库的发布平台,还可以方便的进行数据查询,权限控制等等。
不过大部分公司都只接入了MySQL部分。
最近业务有部分需求,需要对Redis进行查询,测试了之后,发现只支持接入单实例的Redis,Cluster模式的Redis查询不了。
下面就Archery 如何接入 Redis cluster 进行了整理。
部分核心逻辑:

 #查询时,确定待查询实例的存储引擎 (query.py)
 query_engine = get_engine(instance=instance)

#存储引擎类型 (models.py)
DB_TYPE_CHOICES = (
    ('mysql', 'MySQL'),
    ('mssql', 'MsSQL'),
    ('redis', 'Redis'),
    ('rediscluster', 'RedisCluster'),   # 新增
    ('pgsql', 'PgSQL'),
    ('oracle', 'Oracle'),
    ('mongo', 'Mongo'),
    ('phoenix', 'Phoenix'),
    ('inception', 'Inception'),
    ('goinception', 'goInception'))
image.png

上图中,RedisClusterEngine 是新增加的。 为啥要新增加一个 RedisClusterEngine 呢?
因为我们在使用python 链接redis 单实例、集群时候,就知道这个地方是使用的不同的接口。
对于单实例,常见连接代码

import redis
class RedisClusterEngine(EngineBase):
    def get_connection(self, db_name=None):
        db_name = db_name or self.db_name
        return redis.Redis(host=self.host, port=self.port, db=db_name, password=self.password,
                           encoding_errors='ignore', decode_responses=True)

对于cluster 架构, 常见代码

#需要安装一个 redis-py-cluster,有版本限制,下面单独讲
from rediscluster import RedisCluster

def get_connection(host,port,password,db_name=None):
    return RedisCluster(host=host, port=port, password=password,
                encoding_errors='ignore', decode_responses=True) 

conn = get_connection(host='10.2.22.138',port=6400,password='xxx')

#  查询一个集群的key
print("My name is: ", conn.get('foo'))

说到这里,大家差不多都知道了,为啥原生的不支持集群,只支持单实例。就是因为单实例和集群的接口模块不一样。原理知道了,剩下的就是加 RedisClusterEngine 了。

1、拷贝,拷贝,再拷贝。根据sql/engine/redis.py,生成集群的引擎脚本 sql/engine/rediscluster.py

#import redis
from rediscluster import RedisCluster
#return redis.Redis(host=self.host, port=self.port, db=db_name, password=self.password,
        #                   encoding_errors='ignore', decode_responses=True)
        return RedisCluster(host=self.host, port=self.port, password=self.password,
                           encoding_errors='ignore', decode_responses=True) 

#注释原来单实例的接口,db=db_name 集群模式db参数也不支持,需要删除。
2、所有py脚本中,包含redis的脚本部分,拷贝复制,增加一个rediscluster部分,
如:sql/engine/__init__.py


    elif instance.db_type == "redis":
        from .redis import RedisEngine
        return RedisEngine(instance=instance)
    
    #拷贝上面的redis部分,增加下面的rediscluster部分
    elif instance.db_type == "rediscluster":
        from .rediscluster import RedisClusterEngine
        return RedisClusterEngine(instance=instance)
3、所有html页面中,包含redis的部分,拷贝复制,增加一个rediscluster部分,
如:queryapplylist.html 需要增加3个地方
NO1:
<optgroup id="optgroup-rediscluster" label="RedisCluster"></optgroup>

NO2:
$("#optgroup-rediscluster").empty();

NO3:
} else if (result[i]['db_type'] === 'rediscluster') {
$("#optgroup-rediscluster").append(instance);

需要修改的html如下:
queryapplylist.html
sqlquery.html
sqlsubmit.html
instance.html

至此,所有脚本部分,修改完成,测试访问一下:

报错如下:
cannot import name 'dict_merge' from 'redis.client' (/venv4archery/lib/python3.7/site-packages/redis/client.py)

百度了下,这个报错主要是接口版本不匹配,需要安装其他版本的 redis-py-cluster

当前版本
Archery:v1.8.0
python:3.7.9
redis-py-cluster: 2.1.3

尝试 安装 pip3 install redis-py-cluster==1.3.6,还是报错
尝试 安装 pip3 install redis-py-cluster==2.0.0,还是报错
尝试 安装 pip3 install redis-py-cluster==2.1.0,报错解决,可以查询了。

到这里,Archery就可以支持单实例、和集群的数据查询了。

上一篇 下一篇

猜你喜欢

热点阅读