阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ubuntu 16.04.1上搭建Redis分布式集群并使用C#操作

166次阅读
没有评论

共计 18501 个字符,预计需要花费 47 分钟才能阅读完成。

为什么要集群:

通常为了提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis 是一个很好的 Cache 工具,当然了还有 Memcached,这里只讲 Redis。在我们的电商系统中,热点数据量往往巨大,比如单点登录、用户浏览商品的信息、用户信息、用户收藏的商品信息、短息提醒数据等等,也都用到了 redis,如何使 redis 可以横向可伸缩扩展,这需要由多台机器协同提供服务,一台挂掉了,另一台马上顶上去,即分布式的 redis 集群,就对系统的性能非常重要。

Redis 集群的几个重要特征:

(1).Redis 集群的分片特征在于将键空间分拆了 16384 个槽位,每一个节点负责其中一些槽位。

(2).Redis 提供一定程度的可用性, 可以在某个节点宕机或者不可达的情况下继续处理命令.

(3).Redis 集群中不存在中心(central)节点或者代理(proxy)节点,集群的其中一个主要设计目标是达到线性可扩展性(linear scalability)。

(4).Redis 集群要想正常工作,必须要三个主节点,在我搭建的集群环境中,三个主节点都需要有一个从节点,所以一共六个节点,通俗来讲也就是需要开启 6 个 redis 服务。

一、安装 Redis

关于如何在 Linux 上安装 Redis,可以参考我的这篇博客,里面有详细的安装步骤,注意在搭建 Redis 的集群环境我使用的 Redis 版本为 4.0.1《Redis 的安装以及在项目中使用 Redis 的一些总结和体会》。

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

CentOS 7 下 Redis 的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Ubuntu 14.04 安装 Redis 与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Redis 单机 & 集群离线安装部署 http://www.linuxidc.com/Linux/2017-03/141403.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm 

整体结构:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 

二、进群环境的配置

(1)在这里我就开启三台 Ubuntu16.04.1,因为电脑 8G 的内存不够用,如果你电脑内存大可以开启 4 台或者更多。在这三台中我都安装好了最新版本的 Redis。

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

(2)集群配置文件的修改

第一台配置文件的修改:首先在 usr/redis 目录下,把上面安装好的的 redis 文件放到该目录下面,具体哪些文件可参考下图。

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

需要的文件有:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 然后通过下面的命令把上面的文件复制到 usr/redis/ 目录下面:

首先在 usr 目录下面创建一个 redis 和 s2 目录:sudo mkdir redis;     sudo mkdir s2

目录创建好后,到上面图中的目录下面,打开终端,执行:cp redis* /usr/redis/,上面图中的文件就会到 usr/redis/ 目录下面,最后到 usr/redis/ 目录下面,打开终端,执行 sudo cp redis* /usr/redis/s2/,这是该目录下面也就会有相同的文件了。

 

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 修改 redis.conf 文件中 Cluster 的配置,修改如下:

首先由于权限的问题,我们先要切换到 root 身份:sudo passwd root 命令,先修改 root 的密码,修改之后,再执行 su root  接着输入你设置的 root 密码就可以切换到 root 身份,如下:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

最后在 root 身份执行下图中的命令就可修改 redis.conf 文件:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

这里只贴出来修改的代码:(红色标注的部分为修改部分)


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1  // 注释掉

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no   //yes 该为 no

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 700

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300
################################ REDIS CLUSTER  ###############################
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
# in order to mark it as "mature" we need to wait for a non trivial percentage
# of users to deploy it in production.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
cluster-config-file nodes-6379.conf

# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
#
# cluster-node-timeout 15000

# A slave of a failing master will avoid to start a failover if its data
# looks too old.
#
# There is no simple way for a slave to actually have an exact measure of
# its "data age", so the following two checks are performed:
#
# 1) If there are multiple slaves able to failover, they exchange messages
#    in order to try to give an advantage to the slave with the best
#    replication offset (more data from the master processed).
#    Slaves will try to get their rank by offset, and apply to the start
#    of the failover a delay proportional to their rank.
#
# 2) Every single slave computes the time of the last interaction with
#    its master. This can be the last ping or command received (if the master
#    is still in the "connected" state), or the time that elapsed since the
#    disconnection with the master (if the replication link is currently down).
#    If the last interaction is too old, the slave will not try to failover
#    at all.
#
# The point "2" can be tuned by user. Specifically a slave will not perform
# the failover if, since the last interaction with the master, the time
# elapsed is greater than:
#
#   (node-timeout * slave-validity-factor) + repl-ping-slave-period
#
# So for example if node-timeout is 30 seconds, and the slave-validity-factor
# is 10, and assuming a default repl-ping-slave-period of 10 seconds, the
# slave will not try to failover if it was not able to talk with the master
# for longer than 310 seconds.
#
# A large slave-validity-factor may allow slaves with too old data to failover
# a master, while a too small value may prevent the cluster from being able to
# elect a slave at all.
#
# For maximum availability, it is possible to set the slave-validity-factor
# to a value of 0, which means, that slaves will always try to failover the
# master regardless of the last time they interacted with the master.
# (However they'll always try to apply a delay proportional to their
# offset rank).
#
# Zero is the only value able to guarantee that when all the partitions heal
# the cluster will always be able to continue.
#
# cluster-slave-validity-factor 10

# Cluster slaves are able to migrate to orphaned masters, that are masters
# that are left without working slaves. This improves the cluster ability
# to resist to failures as otherwise an orphaned master can't be failed over
# in case of failure if it has no working slaves.
#
# Slaves migrate to orphaned masters only if there are still at least a
# given number of other working slaves for their old master. This number
# is the "migration barrier". A migration barrier of 1 means that a slave
# will migrate only if there is at least 1 other working slave for its master
# and so forth. It usually reflects the number of slaves you want for every
# master in your cluster.
#
# Default is 1 (slaves migrate only if their masters remain with at least
# one slave). To disable migration just set it to a very large value.
# A value of 0 can be set but is useful only for debugging and dangerous
# in production.
#
# cluster-migration-barrier 1

# By default Redis Cluster nodes stop accepting queries if they detect there
# is at least an hash slot uncovered (no available node is serving it).
# This way if the cluster is partially down (for example a range of hash slots
# are no longer covered) all the cluster becomes, eventually, unavailable.
# It automatically returns available as soon as all the slots are covered again.
#
# However sometimes you want the subset of the cluster which is working,
# to continue to accept queries for the part of the key space that is still
# covered. In order to do so, just set the cluster-require-full-coverage
# option to no.
#
# cluster-require-full-coverage yes

# In order to setup your cluster make sure to read the documentation
# available at http://redis.io web site.

修改完之后 按下 ESC 键,再按下  :wq! 保存突出。同理,s2 中的 redis 也是这样修改的但是,需要修改一下端口号,不能喝上面的端口号重复,s2 中的端口号为 6390。

使用相同的方式修改第二台上 redis 的配置:只不过在该台机器上只有一个 redis 服务(节点)

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

上面这两台服务器中的三个 redis 服务地址分别为:192.168.0.109:6379    192.168.0.109:6390    192.168.0.111:6379,在进行第三部的时候要用到。

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-10/147461p2.htm

(3)第三台机器的配置

上面的两步中我们共开起了三台主 Redis 节点,所以在第三台服务器上我们也要开启三台从节点,结果如下:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

其中 s1、s2、s3 中文件和上面的一样,即三台从的 redis 节点。我们需要一个一个安装上面的配置修改 redis.conf 文件,只不过需要注意,三个节点的端口号是不一样的。s1、s2、s3 的端口号分别是:6379   6380  6381

到现在个个 redis 节点的配置文件也修改好了,下面我们要分别启动 s1  s2  s3 和上面的三个主 redis 服务了~~~~~~

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 接下来,这三个服务都会生产一个这样的文件:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

因为 redis-trib.rb 是 ruby 写的,不信可以打开看看。而在我们的 Ubuntu16.04.1 上是有 ruby 的环境的,但是还是强烈建议执行以下下面的指令 安装 ruby,同时还要安装 ruby 的 Redis 库,不过没关系,我们可以按照下面的步骤安装。

  • 安装 Redis-Cluster 的依赖库(可能在安装依赖库的时候特别慢,可以使用加速器,我就不在这里说了,你懂的)
    • 安装 Ruby

      sudo apt-get install ruby

    • 安装 Redis.gem

      sudo gem intall redis

耐心等待,因为很慢的(#^.^#)。安装好之后,执行下面图中划的命令:

 

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 解释:./redis-trib.rb  create –replicas 1 192.168.0.109:6379 192.168.0.109:6390 192.168.0.110:6379 192.168.0.111:6379 192.168.0.111:6380 192.168.0.111:6381

这些 IP 都是上面 redis 服务的 IP,注意他们之间是有空格分开的。

如果出现上面的信息说明安装成功了!!!

其中:192.168.0.109:6379、192.168.0.110:6379/192.168.0.111:6379 是主,192.168.0.111:6380/192.168.0.109:6390、192.168.0.111:6381 是从的。

(4)测试是否成功

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

在到 192.168.0.110:6379 中看看有没有值:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

看到没,值同步过来了!!!!

三、面对 Redis-ClusterC# 该如何操作

 step1:使用 VS2017 新建一个控制台程序

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

step2: Install-Package StackExchange.Redis

 step3: 编写代码:

using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinuxRedisCluster
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(“192.168.0.109:6379,192.168.0.109:6390,192.168.0.110:6379,192.168.0.111:6379,192.168.0.111:6380,192.168.0.111:6381”))
            {

                IDatabase db = redis.GetDatabase();
                for (int i = 0; i < 1000; i++)
                {
                    db.StringSet(“name”+i, “guozheng”+i);
                }
               

                //var myname = db.StringGet(“name”);
              // Console.WriteLine(myname);
            }

            Console.ReadKey();
        }
    }
}

step4: 查看结果:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 四、总结:

关于 redis 如何集群的详细信息,可以参考 Redis 手册:集群教程

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147461.htm

为什么要集群:

通常为了提高网站的响应速度,总是把一些经常用到的数据放到内存中,而不是放到数据库中,Redis 是一个很好的 Cache 工具,当然了还有 Memcached,这里只讲 Redis。在我们的电商系统中,热点数据量往往巨大,比如单点登录、用户浏览商品的信息、用户信息、用户收藏的商品信息、短息提醒数据等等,也都用到了 redis,如何使 redis 可以横向可伸缩扩展,这需要由多台机器协同提供服务,一台挂掉了,另一台马上顶上去,即分布式的 redis 集群,就对系统的性能非常重要。

Redis 集群的几个重要特征:

(1).Redis 集群的分片特征在于将键空间分拆了 16384 个槽位,每一个节点负责其中一些槽位。

(2).Redis 提供一定程度的可用性, 可以在某个节点宕机或者不可达的情况下继续处理命令.

(3).Redis 集群中不存在中心(central)节点或者代理(proxy)节点,集群的其中一个主要设计目标是达到线性可扩展性(linear scalability)。

(4).Redis 集群要想正常工作,必须要三个主节点,在我搭建的集群环境中,三个主节点都需要有一个从节点,所以一共六个节点,通俗来讲也就是需要开启 6 个 redis 服务。

一、安装 Redis

关于如何在 Linux 上安装 Redis,可以参考我的这篇博客,里面有详细的安装步骤,注意在搭建 Redis 的集群环境我使用的 Redis 版本为 4.0.1《Redis 的安装以及在项目中使用 Redis 的一些总结和体会》。

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

CentOS 7 下 Redis 的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Ubuntu 14.04 安装 Redis 与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Redis 单机 & 集群离线安装部署 http://www.linuxidc.com/Linux/2017-03/141403.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm 

整体结构:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 

二、进群环境的配置

(1)在这里我就开启三台 Ubuntu16.04.1,因为电脑 8G 的内存不够用,如果你电脑内存大可以开启 4 台或者更多。在这三台中我都安装好了最新版本的 Redis。

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

(2)集群配置文件的修改

第一台配置文件的修改:首先在 usr/redis 目录下,把上面安装好的的 redis 文件放到该目录下面,具体哪些文件可参考下图。

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

需要的文件有:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 然后通过下面的命令把上面的文件复制到 usr/redis/ 目录下面:

首先在 usr 目录下面创建一个 redis 和 s2 目录:sudo mkdir redis;     sudo mkdir s2

目录创建好后,到上面图中的目录下面,打开终端,执行:cp redis* /usr/redis/,上面图中的文件就会到 usr/redis/ 目录下面,最后到 usr/redis/ 目录下面,打开终端,执行 sudo cp redis* /usr/redis/s2/,这是该目录下面也就会有相同的文件了。

 

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

 修改 redis.conf 文件中 Cluster 的配置,修改如下:

首先由于权限的问题,我们先要切换到 root 身份:sudo passwd root 命令,先修改 root 的密码,修改之后,再执行 su root  接着输入你设置的 root 密码就可以切换到 root 身份,如下:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

最后在 root 身份执行下图中的命令就可修改 redis.conf 文件:

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

这里只贴出来修改的代码:(红色标注的部分为修改部分)


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1  // 注释掉

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
protected-mode no   //yes 该为 no

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379

# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511

# Unix socket.
#
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 700

# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0

# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
#    equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 300 seconds, which is the new
# Redis default starting with Redis 3.2.1.
tcp-keepalive 300
################################ REDIS CLUSTER  ###############################
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
# in order to mark it as "mature" we need to wait for a non trivial percentage
# of users to deploy it in production.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
cluster-enabled yes

# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
cluster-config-file nodes-6379.conf

# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
#
# cluster-node-timeout 15000

# A slave of a failing master will avoid to start a failover if its data
# looks too old.
#
# There is no simple way for a slave to actually have an exact measure of
# its "data age", so the following two checks are performed:
#
# 1) If there are multiple slaves able to failover, they exchange messages
#    in order to try to give an advantage to the slave with the best
#    replication offset (more data from the master processed).
#    Slaves will try to get their rank by offset, and apply to the start
#    of the failover a delay proportional to their rank.
#
# 2) Every single slave computes the time of the last interaction with
#    its master. This can be the last ping or command received (if the master
#    is still in the "connected" state), or the time that elapsed since the
#    disconnection with the master (if the replication link is currently down).
#    If the last interaction is too old, the slave will not try to failover
#    at all.
#
# The point "2" can be tuned by user. Specifically a slave will not perform
# the failover if, since the last interaction with the master, the time
# elapsed is greater than:
#
#   (node-timeout * slave-validity-factor) + repl-ping-slave-period
#
# So for example if node-timeout is 30 seconds, and the slave-validity-factor
# is 10, and assuming a default repl-ping-slave-period of 10 seconds, the
# slave will not try to failover if it was not able to talk with the master
# for longer than 310 seconds.
#
# A large slave-validity-factor may allow slaves with too old data to failover
# a master, while a too small value may prevent the cluster from being able to
# elect a slave at all.
#
# For maximum availability, it is possible to set the slave-validity-factor
# to a value of 0, which means, that slaves will always try to failover the
# master regardless of the last time they interacted with the master.
# (However they'll always try to apply a delay proportional to their
# offset rank).
#
# Zero is the only value able to guarantee that when all the partitions heal
# the cluster will always be able to continue.
#
# cluster-slave-validity-factor 10

# Cluster slaves are able to migrate to orphaned masters, that are masters
# that are left without working slaves. This improves the cluster ability
# to resist to failures as otherwise an orphaned master can't be failed over
# in case of failure if it has no working slaves.
#
# Slaves migrate to orphaned masters only if there are still at least a
# given number of other working slaves for their old master. This number
# is the "migration barrier". A migration barrier of 1 means that a slave
# will migrate only if there is at least 1 other working slave for its master
# and so forth. It usually reflects the number of slaves you want for every
# master in your cluster.
#
# Default is 1 (slaves migrate only if their masters remain with at least
# one slave). To disable migration just set it to a very large value.
# A value of 0 can be set but is useful only for debugging and dangerous
# in production.
#
# cluster-migration-barrier 1

# By default Redis Cluster nodes stop accepting queries if they detect there
# is at least an hash slot uncovered (no available node is serving it).
# This way if the cluster is partially down (for example a range of hash slots
# are no longer covered) all the cluster becomes, eventually, unavailable.
# It automatically returns available as soon as all the slots are covered again.
#
# However sometimes you want the subset of the cluster which is working,
# to continue to accept queries for the part of the key space that is still
# covered. In order to do so, just set the cluster-require-full-coverage
# option to no.
#
# cluster-require-full-coverage yes

# In order to setup your cluster make sure to read the documentation
# available at http://redis.io web site.

修改完之后 按下 ESC 键,再按下  :wq! 保存突出。同理,s2 中的 redis 也是这样修改的但是,需要修改一下端口号,不能喝上面的端口号重复,s2 中的端口号为 6390。

使用相同的方式修改第二台上 redis 的配置:只不过在该台机器上只有一个 redis 服务(节点)

Ubuntu 16.04.1 上搭建 Redis 分布式集群并使用 C# 操作

上面这两台服务器中的三个 redis 服务地址分别为:192.168.0.109:6379    192.168.0.109:6390    192.168.0.111:6379,在进行第三部的时候要用到。

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2017-10/147461p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计18501字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中