Redis proxy 组件之 Predixy

发布时间 2023-07-31 15:41:08作者: 201432273

Predixy 是一款高性能全特征 redis 代理,支持 redis-sentinel 和 redis-cluster
组件特性: Predixy 支持的功能

为什么需要 redis proxy ?

  1. 屏蔽 redis 架构的复杂性,使后端开发人员 无论是用 redis sentinel 还是 redis cluster 集群,都像使用单机 redis 实例一样方便。
  2. 集群扩缩容时,对业务更友好,开发人员无需修改后端配置。
  3. 实现基于 proxy 的 负载均衡、读写分离、权限控制。
  4. 便于集群管理。
  5. 纵观 各大云厂商,横向对比 top 互联网团队,redis 集群架构 proxy 模式已成为标配。

predixy 可以管理 多套 redis sentinel 集群。redis cluster 模式支持 一对一管理,本文主要记录 predixy + redis cluster 的使用。

开始之前你需要有一套 redis cluster 集群,本文使用的集群节点分布:( 3分片,一主一副本)
shards master slave
shard 1 192.168.98.111:16380 192.168.98.113:16381
shard 2 192.168.98.112:16380 192.168.98.111:16381
shard 3 192.168.98.113:16380 192.168.98.112:16381

1. predixy 安装:

下载地址: wget https://github.com/joyieldInc/predixy/releases/tag/1.0.5

[root@db]# tar -xf predixy-1.0.5-bin-amd64-linux.tar.gz -C  /usr/local/predixy
[root@db]# cd  /usr/local/predixy  && ls ./ ./conf/
./:
bin  conf  doc  logs  README_CN.md  test
 
./conf/:
auth.conf  cluster.conf  command.conf  dc.conf  latency.conf  predixy.conf  sentinel.conf  standalone.conf  try.conf
 
## 配置文件
 predixy.conf 全局配置文件,会引用下面的配置文件
 cluster.conf 用于Redis Cluster时,配置后端redis信息
 sentinel.conf 用于Redis Sentinel时,配置后端redis信息
 auth.conf 访问权限控制配置,可以定义多个验证密码,可每个密码指定读、写、管理权限,以及定义可访问的健空间
 dc.conf 多数据中心支持,可以定义读写分离规则,读流量权重分配
 latency.conf 延迟监控规则定义,可以指定需要监控的命令以及延时时间间隔

2. predixy 配置:

predixy.conf 全局配置

[root@db conf]# cat predixy.conf
## Specify a name for this predixy service
## redis command INFO can get this
Name Predixy_001
 
## Default is 0.0.0.0:7617
Bind 0.0.0.0:7617
 
## Worker threads
WorkerThreads 4
 
## Memory limit, 0 means unlimited
## MaxMemory can change online by CONFIG SET MaxMemory xxx
MaxMemory 0
 
## Close the connection after a client is idle for N seconds (0 to disable)
## ClientTimeout can change online by CONFIG SET ClientTimeout N
## Default is 0
ClientTimeout 300   # redis cluster 节点的  timeout 参数最好配置为 0
 
## IO buffer size
## Default is 4096
## IO Buffer大小,predixy内部分配BufSize大小的缓冲区用于接受客户端命令和服务端响应,完全零拷贝的转发给服务端或者客户端,该值太小的话影响性能,太大的话浪费空间也可能对性能无益。但是具体多少合适要看实际应用场景
BufSize 2048
 
## Log file path
Log /data/predixy/logs/predixy.log
## LogRotate LogLevelSample
LogVerbSample 0
LogDebugSample 0
LogInfoSample 1000
LogNoticeSample 1
LogWarnSample 1
LogErrorSample 1
 
## SERVERS
Include auth.conf
Include cluster.conf

auth.conf 配置

[root@db conf]# cat auth.conf
 
Authority {
    Auth 123456qewr {    # predixy 代理密码,业务后端认证需要配置的密码
        Mode admin   # 权限
    }
}

cluster.conf 配置

[root@db conf]# cat cluster.conf
## Cluster_001:
ClusterServerPool {
    Password 123456    # redis cluste 集群密码
    MasterReadPriority 60
    StaticSlaveReadPriority 50
    DynamicSlaveReadPriority 50
    RefreshInterval 1
    ServerTimeout 1
    ServerFailureLimit 10
    ServerRetryTimeout 1
    KeepAlive 120
    Servers {   # redis cluster 集群所有节点
        + 192.168.98.111:16380
        + 192.168.98.111:16381
        + 192.168.98.112:16380
        + 192.168.98.112:16381
        + 192.168.98.113:16380
        + 192.168.98.113:16381
    }
}

3. predixy 日常维护

predixy 完美的实现了对 redis 单例模式及集群模式的支持,几乎完整的实现了 redis 原生的所有用于客户端的命令。多 key 命令、列表阻塞操作、发布订阅、脚本、扫描等高级功能全支持,在使用 redis 单例模式下也支持事物。

# 启动 predixy
[root@db]# nohuo predixy conf/predixy.conf &
 
# 通过 predixy 代理连接 redis cluster
[root@db]# redis-cli -h 127.0.0.1 -p 7617 -a 123456qewr
127.0.0.1:7617> info proxy  # 查看 Proxy 元信息
# Proxy
Version:1.0.5
Name:Predixy_001
Bind:0.0.0.0:7617
RedisMode:proxy
SingleThread:false
WorkerThreads:4
Uptime:1681269847
UptimeSince:2023-04-12 11:24:07
 
127.0.0.1:7617> info stats  # 查看 Proxy 状态
# Stats
Accept:8
ClientConnections:1
TotalRequests:444313
TotalResponses:442434
TotalRecvClientBytes:1880
TotalSendServerBytes:12386300
TotalRecvServerBytes:348915311
TotalSendClientBytes:13728
 
127.0.0.1:7617> info servers   # 查看 redis cluster 节点信息
# Servers
Server:192.168.98.111:16380
Role:master
Group:ecbdd26257c8b5efff89732f0ff04f08fac2996c
DC:
CurrentIsFail:0
Connections:4
Connect:120
Requests:74080
Responses:73728
SendBytes:2064284
RecvBytes:58146243

至此我们的 redis cluster 变成了如下架构

              |--------+                 
              | Clitne1| Jedis jedis = new Jedis("predixy ip","7617");
              | Clitne2| jedis.auth("123456qewr")
              | ClitneN| jedis.ping();
              +--------+               
                   |
                   |
+--------+         |         +--------+
| Predixy|---------+---------| Predixy|
+--------+         |         +--------+
                   |
      +------------+--------------+   
      |      Redis Cluster        |
+-----------+ +-----------+ +-----------+
| shard 1   | | shard 2   | | shard N...|
+-----------+ +-----------+ +-----------+