如何在 REDIS 从机之间平衡只读指令的负载?

use*_*521 5 haproxy redis

我已经设置了 REDIS 复制(1 个主站 + 2 个从站)以及 3 个哨兵和 1 个 HAProxy 负载均衡器。它可以工作,但除了故障转移之外,我想在从属设备之间平衡只读指令的负载。可以使用HAProxy吗?这是我的 HAProxy 配置的一部分:

backend bk_redis
option tcp-check
tcp-check connect
tcp-check send PING\r\n
tcp-check expect string +PONG
tcp-check send info\ replication\r\n
tcp-check expect string role:master
tcp-check send QUIT\r\n
tcp-check expect string +OK
server redis_1 localhost:6379 check inter 1s
server redis_2 localhost:6380 check inter 1s
Run Code Online (Sandbox Code Playgroud)

(据我所知)这将只是将所有流量重定向到当前主服务器,而我想在这里进行负载平衡。

Woo*_*Dzu 0

处理它的一种方法是为主数据库提供一个单独的后端,为从数据库提供另一个后端。

frontend fe_redis
  ... no changes

backend bk_redis
  ... no changes



frontend fe_redis_readonly
  bind *:6380                             # Different port 
  mode tcp
  option tcplog
  default_backend bk_redis_readonly

backend bk_redis_readonly
  option tcp-check
  tcp-check connect
  tcp-check send PING\r\n
  tcp-check expect string +PONG
  tcp-check send info\ replication\r\n
  tcp-check expect string role:slave      # Verify slave 
  tcp-check send QUIT\r\n
  tcp-check expect string +OK

  server redis_1 localhost:6379 check inter 1s
  server redis_2 localhost:6380 check inter 1s
Run Code Online (Sandbox Code Playgroud)

第二部分是更新应用程序以区分读取请求并将其发送到读取器后端。