本文内容为 http://redis.io/commands/role 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
ROLE

返回当前复制上下文中(Context of replication),某个 Redis 实例(Instance)所处角色(Role)的具体信息,包括指定实例在当前情况下是一个主实例(Master)、从实例(Slave)还是一个哨兵实例(Sentinel),以及当前响应的状态(或果当前实例是一个主实例或从实例),或所监控的主实例的名字列表(如果当前实例是一个哨兵实例)。

Provide information on the role of a Redis instance in the context of replication, by returing if the instance is currently a master, slave, or sentinel. The command also returns additional information about the state of the replication (if the role is master or slave) or the list of monitored master names (if the role is sentinel).
  • 返回一个数组响应(Array reply),第一个元素为 masterslavesentinel 等值中的某个,附加信息依各个角色而定,具体如下:

    Array reply: where the first element is one of master, slave, sentinel and the additional elements are role-specific as illustrated above.

    主实例输出

    Master output

    An example of output when ROLE is called in a master instance:
    1) "master"
    2) (integer) 3129659
    3) 1) 1) "127.0.0.1"
          2) "9001"
          3) "3129242"
       2) 1) "127.0.0.1"
          2) "9002"
          3) "3129543"

    The master output is composed of the following parts:

    • The string master.
    • The current master replication offset, which is an offset that masters and slaves share to understand, in partial resynchronizations, the part of the replication stream the slave needs to fetch to continue.
    • An array composed of three elements array representing the connected slaves. Every sub-array contains the slave IP, port, and the last acknowledged replication offset.

    从实例输出

    Slave output

    An example of output when ROLE is called in a slave instance:
    1) "slave"
    2) "127.0.0.1"
    3) (integer) 9000
    4) "connected"
    5) (integer) 3167038
    The slave output is composed of the following parts:
    • The string slave.
    • The IP of the master.
    • The port number of the master.
    • The state of the replication from the point of view of the master, that can be connect (the instance needs to connect to its master), connecting (the slave-master connection is in progress), sync (the master and slave are trying to perform the synchronization), connected (the slave is online).
    • The amount of data received from the slave so far in terms of master replication offset.

    哨兵实例输出

    Sentinel output

    An example of Sentinel output:
    1) "sentinel" 2) 1) "resque-master" 2) "html-fragments-master" 3) "stats-master" 4) "metadata-master"
    The sentinel output is composed of the following parts:
    • The string sentinel.
    • An array of master names monitored by this Sentinel instance.
  • redis> ROLE
    1) "master"
    2) (integer) 0
    3) (empty list or set)
    redis> 
  • 版本支持

    2.8.12+

    注意 当前命令在 Redis 某个稳定版本的中期引入,在 2.8.12 中明确支持。

    This command was introduced in the middle of a Redis stable release, specifically with Redis 2.8.12.