CSRedisCore:3.8.802
yum -y install wget2、安装 gcc ,编译和安装 Redis 时需要:
yum -y install gcc3、下载 Redis 并安装:
cd /usr/local #进入到usr/local目录 tar xzf redis-6.2.14.tar.gz #解压Redis cd redis-6.2.14 #进入到解压到Redis目录 make MALLOC=libc #编译 make install #安装4、修改 Redis 配置文件并启动:
cd /usr/local/6.2.14 #进入redis目录 vi redis.conf #编辑配置文件编辑内容如下:
daemonize yes #修改配置文件中的daemonize为yes,为后台启动执行命令 redis-server redis.conf 进行 Redis 服务的启动。
ps -ef | grep redis #检查是否启动成功
vi redis.conf #编辑配置文件进行密码设置修改文件内容,去掉requirepass前面的#号:
requirepass Aa123456 redis-server redis.conf #修改完配置文件,重启redis配置主从(哨兵模式)
master:6380 slave1:6382 slave2:6383 slave3:6383 sentinel1:26379 sentinel2:26380 Sentinel3:26381 sentinel4:26382 Sentinel5:263831、在 /usr/local/redis-6.2.14 目录中创建 config 目录,在该目录中创建相应的目录存放配置文件和数据:
cd /usr/local/redis-6.2.14 mkdir config #创建config目录 cd config mkdir master-6380 #创建master-6380目录 cd master-6380 mkdir data #创建data目录用来存放数据 cp /usr/local/redis-6.2.14/redis.conf . #将配置文件复制到当前目录 cd .. #回退到config目录 cp -r master-6380/ slave-6381 cp -r master-6380/ slave-6382 cp -r master-6380/ slave-6383 # 堆代码 duidaima.com mkdir sentinel-26379 #创建哨兵1配置目录 cp /usr/local/redis-6.2.14/sentinel.conf /usr/local/redis-6.2.14/config/sentinel-26379/ cd sentinel-26379 mkdir data cd .. cp -r sentinel-26379/ sentinel-26380创建完成后目录结构如下:
bind 10.211.55.14 #修改成自己的IP地址 port 6380 #主服务器的端口号 daemonize yes #设置后台启动 requirepass Aa123456 pidfile /var/run/redis_6380.pid #redis 后台启动的时候会在/var/run/默认生成一个pid文件 protected-mode no #保护模式关闭 dir /usr/local/redis-6.2.14/config/master-6380/data #数据保存目录3、配置 slave 的 redis.conf 文件:
bind 10.211.55.14 #修改成自己的IP地址 port 6381 #从服务器的端口号 daemonize yes #设置后台启动 requirepass Aa123456 pidfile /var/run/redis_6381.pid protected-mode no #保护模式关闭 dir /usr/local/redis-6.2.14/config/slave-6381/data #数据保存目录 replicaof 10.211.55.14 6380 #主服务器的IP 主服务器端口号 masterauth Aa1234564、将端口 6382、6383 对应的从服务器的配置文件对照第三步进行修改。
port 26379 #指定哨兵1端口号 daemonize yes #设置后台启动 protected-mode no #关闭保护模式 requirepass "Aa123456" sentinel auth-pass mymaster Aa123456 sentinel monitor mymaster 10.211.55.14 6380 5 #监听主的端口,后面的数字2为哨兵的个数 logfile "/usr/local/redis-6.2.14/config/sentinel-26379/sentinel-26379.log" dir "/usr/local/redis-6.2.14/config/sentinel-26379/data"6、配置哨兵 2 的配置文件,其他的几个哨兵配置类似:
port 26380 #指定哨兵2端口号 daemonize yes #设置后台启动 protected-mode no #关闭保护模式 requirepass "Aa123456" sentinel auth-pass mymaster Aa123456 sentinel monitor mymaster 10.211.55.14 6380 5 #监听主的端口,后面的数字2为哨兵的个数 logfile "/usr/local/redis-6.2.14/config/sentinel-26380/sentinel-26380.log" dir "/usr/local/redis-6.2.14/config/sentinel-26380/data"注意 :mymaster 为主的名称,默认为 mymaster,如果要修改,该配置文件中所有涉及的地方都需要调整。
cd /usr/local/redis-6.2.14 redis-server ./config/master-6380/redis.conf redis-server ./config/slave-6381/redis.conf redis-server ./config/slave-6382/redis.conf redis-server ./config/slave-6383/redis.conf redis-sentinel ./config/sentinel-26379/sentinel.conf redis-sentinel ./config/sentinel-26380/sentinel.conf redis-sentinel ./config/sentinel-26381/sentinel.conf redis-sentinel ./config/sentinel-26382/sentinel.conf redis-sentinel ./config/sentinel-26383/sentinel.conf8、查看主从状态:
redis-cli -h 10.211.55.14 -p 6380 #连接到主库 >auth Aa123456 >info #使用info命令查看信息,如下图
redis-cli -h 10.211.55.14 -p 6380 #连接到主库 >auth Aa123456 >shutdown #停掉主库 >quit #退出 redis-cli -h 10.211.55.14 -p 6383 #连接到其中一个从库 >auth Aa123456 >info #查看状态,如下图:
redisServerIP = "10.211.55.14,10.211.55.14,10.211.55.14,10.211.55.14,10.211.55.14"; //哨兵IP列表 redisServerPort = "26379,26380,26381,26382,26383"; List<string> connectionList = GetRedisConnectionList(redisServerIP, redisServerPort); ConfigurationOptions sentinelOptions = new ConfigurationOptions(); foreach (var connection in connectionList) { sentinelOptions.EndPoints.Add(connection); } sentinelOptions.Password = "Aa123456"; //哨兵访问密码 sentinelOptions.TieBreaker = ""; sentinelOptions.CommandMap = CommandMap.Sentinel; sentinelOptions.AbortOnConnectFail = true; // 堆代码 duidaima.com // Connect ConnectionMultiplexer sentinelConnection = ConnectionMultiplexer.Connect(sentinelOptions); ISubscriber subscriber = sentinelConnection.GetSubscriber(); ConfigurationOptions redisServiceOptions = new ConfigurationOptions(); redisServiceOptions.ServiceName = "mymaster"; //master名称 redisServiceOptions.Password = "Aa123456"; //master访问密码 redisServiceOptions.AbortOnConnectFail = true; redisServiceOptions.AllowAdmin = true; ConnectionMultiplexer masterConnection = sentinelConnection.GetSentinelMasterConnection(redisServiceOptions);2、使用 CSRedisCore 操作主连接:
if (masterConnection.IsConnected) { var servers = masterConnection.GetServers(); if (servers?.Length > 0) { //获取主库配置 var masterService = servers.Where(x => !x.IsReplica).FirstOrDefault(); if (masterService == null) return; var endPoint = masterService.EndPoint as System.Net.IPEndPoint; redisServerIP = endPoint.Address.ToString(); redisServerPort = endPoint.Port.ToString(); CSRedisClient csredis = GetClient(redisServerIP, redisServerPort); RedisHelper.Initialization(csredis);//初始化 } }虽然有点绕,但暂时可以解决问题,希望 CSRedisCore 未来可以支持 Redis 节点和哨兵都设置密码的场景。