首先原文是用了3 個服務器,我是用了一個服務器;
然后再原文的基礎上,稍加了自己的整理。
前提:
redis中,主從切換場景中,沒有絕對的主和從,只有初始化的主和從,然后當主down后,從就變成主了,而主即使連接上,也是從,不會變?yōu)橹?/p>
1.redis-server的主備關系:
master : redis-1
slave1 : redis-2
slave3 : redis-3
2. 首先進行主從備份:
修改從服務 redis-1 redis-2 的redis.conf
在從服務上 修改redis.conf 加入 slaveof 127.0.0.1 6379
主從備份: 這里設置成功之后,會進行主服務進行set之后,可在從服務進行get key ,可是一旦主服務宕機,從服務無法再進行set key
3.設置主從切換
三個服務器都修改 sentinel-test.conf
加入
sentinel monitor MyMaster 127.0.0.1 6381 1
sentinel down-after-milliseconds MyMaster 5000
sentinel failover-timeout MyMaster 900000
sentinel parallel-syncs MyMaster 2
第一行配置指示 Sentinel 去監(jiān)視一個名為 mymaster 的主服務器, 這個主服務器的 IP 地址為 127.0.0.1 , 端口號為 6379 , 而將這個主服務器判斷為失效至少需要 2 個 Sentinel 同意 (只要同意 Sentinel 的數(shù)量不達標,自動故障遷移就不會執(zhí)行)。
第二行down-after-milliseconds 選項指定了 Sentinel 認為服務器已經(jīng)斷線所需的毫秒數(shù)。
如果服務器在給定的毫秒數(shù)之內(nèi), 沒有返回 Sentinel 發(fā)送的 PING 命令的回復, 或者返回一個錯誤, 那么 Sentinel 將這個服務器標記為主觀下線(subjectively down,簡稱 SDOWN )。
不過只有一個 Sentinel 將服務器標記為主觀下線并不一定會引起服務器的自動故障遷移: 只有在足夠數(shù)量的 Sentinel 都將一個服務器標記為主觀下線之后, 服務器才會被標記為客觀下線(objectively down, 簡稱 ODOWN ), 這時自動故障遷移才會執(zhí)行。
將服務器標記為客觀下線所需的 Sentinel 數(shù)量由對主服務器的配置決定。
第三行暫時不知道是什么意思;
第四行 parallel-syncs 選項指定了在執(zhí)行故障轉(zhuǎn)移時, 最多可以有多少個從服務器同時對新的主服務器進行同步, 這個數(shù)字越小, 完成故障轉(zhuǎn)移所需的時間就越長。
如果從服務器被設置為允許使用過期數(shù)據(jù)集(參見對 redis.conf 文件中對 slave-serve-stale-data 選項的說明), 那么你可能不希望所有從服務器都在同一時間向新的主服務器發(fā)送同步請求, 因為盡管復制過程的絕大部分步驟都不會阻塞從服務器, 但從服務器在載入主服務器發(fā)來的 RDB 文件時, 仍然會造成從服務器在一段時間內(nèi)不能處理命令請求: 如果全部從服務器一起對新的主服務器進行同步, 那么就可能會造成所有從服務器在短時間內(nèi)全部不可用的情況出現(xiàn)。
你可以通過將這個值設為 1 來保證每次只有一個從服務器處于不能處理命令請求的狀態(tài)。
4.啟動
啟動redis-server
# ./src/redis-server redis.conf
啟動redis-sentinel
# ./src/redis-sentinelsentinel-test.conf
注意: 三臺服務器都是這么啟動的哦!~
補充:Redis CLuster主備切換、故障轉(zhuǎn)移測試
redis版本5.0.5
測試redis Cluster主備切換、故障轉(zhuǎn)移
1.下線一個從節(jié)點,此時它的主節(jié)點打印的日志
集群狀態(tài)
2.下線一臺主節(jié)點,此時它的從節(jié)點打印的日志
集群狀態(tài)
測試主備切換時客戶端狀態(tài)
第一步:查看當前集群狀態(tài)
可以看到六個節(jié)點都是可用狀態(tài),其中83.46的6379是81.64上的6380的從節(jié)點,計劃Kill掉81.64上的6380主節(jié)點,然后觀察83.46的6379節(jié)點日志
第二步:kill掉81.64上的6380
10:11:25:kill掉81.64上的6380,可以看到其從節(jié)點很快提示連接主節(jié)點失敗,并且開始一秒鐘一次的重連操作
此時查看集群的節(jié)點狀態(tài)如下,可以看到槽 5461-10922在這個主節(jié)點上,此時整個reidis集群處于不可用狀態(tài)
10:12:24:應用程序報錯,redis操作超時
10:11:43 :在重連17次失敗次數(shù)之后,從節(jié)點將主節(jié)點標記為失敗,并且整個集群的狀態(tài)切換為不可用,之后不甘心,又去嘗試連接主節(jié)點
10:12:03:在重連20次失敗后,從節(jié)點打印日志,等待投票選舉,但是沒有達到多數(shù)贊成,于是繼續(xù)重連之前的主節(jié)點
10:12:14:提示選舉失敗,選舉過期,又繼續(xù)重連
10:12:45:選舉成功勝出,成為了新的主節(jié)點,整個集群的狀態(tài)變?yōu)榭捎?/p>
10:13:39:大概一分鐘之后,redis客戶端自動刷新了集群配置,成功連接上redis集群,此時主備切換和故障轉(zhuǎn)移完成
此前項目中存在的問題
redis master宕機之后,會出現(xiàn)應用程序連接不上redis cluster的問題,需要重啟服務才能解決
排查原因之后發(fā)現(xiàn)是spring boot 2.x版本默認使用了lettuce作為redis客戶端,而lettuce默認是不開啟自動刷新集群拓撲的,當redis master宕機并且集群完成故障轉(zhuǎn)移/主從切換之后,客戶端使用的還是之前錯誤的集群信息,就會導致應用程序一直連接不上redis集群。解決方案就是修改redis客戶端配置,開啟開啟自適應刷新拓撲
配置文件如下
spring.redis.cluster.nodes=${redis.nodes}
spring.redis.password=${redis.pass}
spring.redis.timeout=60000
# 最大重定向次數(shù)
spring.redis.cluster.max-redirects=3
spring.redis.lettuce.pool.max-active=64
spring.redis.lettuce.pool.max-idle=16
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.pool.max-wait=60000ms
spring.redis.lettuce.shutdown-timeout=100ms
完整的配置類如下
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Configuration
public class RedisConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
@SuppressWarnings("all")
public RedisTemplateString, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplateString, Object> template = new RedisTemplateString, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
/**
* 為RedisTemplate配置Redis連接工廠實現(xiàn)
* LettuceConnectionFactory實現(xiàn)了RedisConnectionFactory接口
* 這里要注意的是,在構(gòu)建LettuceConnectionFactory 時,如果不使用內(nèi)置的destroyMethod,可能會導致Redis連接早于其它Bean被銷毀
*
* @return 返回LettuceConnectionFactory
*/
@Bean(destroyMethod = "destroy")
public LettuceConnectionFactory lettuceConnectionFactory() {
ListString> clusterNodes = redisProperties.getCluster().getNodes();
SetRedisNode> nodes = new HashSet>();
clusterNodes.forEach(address -> nodes.add(new RedisNode(address.split(":")[0].trim(), Integer.parseInt(address.split(":")[1]))));
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
clusterConfiguration.setClusterNodes(nodes);
clusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
clusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
poolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
poolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
return new LettuceConnectionFactory(clusterConfiguration, getLettuceClientConfiguration(poolConfig));
}
/**
* 配置LettuceClientConfiguration 開啟自適應刷新拓撲 包括線程池配置和安全項配置
*
* @param genericObjectPoolConfig common-pool2線程池
* @return lettuceClientConfiguration
*/
private LettuceClientConfiguration getLettuceClientConfiguration(GenericObjectPoolConfig genericObjectPoolConfig) {
/*
ClusterTopologyRefreshOptions配置用于開啟自適應刷新和定時刷新。如自適應刷新不開啟,Redis集群變更時將會導致連接異常!
*/
ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder()
//開啟自適應刷新
//.enableAdaptiveRefreshTrigger(ClusterTopologyRefreshOptions.RefreshTrigger.MOVED_REDIRECT, ClusterTopologyRefreshOptions.RefreshTrigger.PERSISTENT_RECONNECTS)
//開啟所有自適應刷新,MOVED,ASK,PERSISTENT都會觸發(fā)
.enableAllAdaptiveRefreshTriggers()
// 自適應刷新超時時間(默認30秒)
.adaptiveRefreshTriggersTimeout(Duration.ofSeconds(25)) //默認關閉開啟后時間為30秒
// 開周期刷新
.enablePeriodicRefresh(Duration.ofSeconds(20)) // 默認關閉開啟后時間為60秒 ClusterTopologyRefreshOptions.DEFAULT_REFRESH_PERIOD 60 .enablePeriodicRefresh(Duration.ofSeconds(2)) = .enablePeriodicRefresh().refreshPeriod(Duration.ofSeconds(2))
.build();
return LettucePoolingClientConfiguration.builder()
.poolConfig(genericObjectPoolConfig)
.clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(topologyRefreshOptions).build())
.build();
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Redis Sentinel的使用方法
- Redis Sentinel的基本搭建
- Redis服務之高可用組件sentinel詳解
- 詳解SpringBoot Redis自適應配置(Cluster Standalone Sentinel)
- 基于docker搭建redis-sentinel集群的方法示例
- Linux redis-Sentinel配置詳解
- Redis Sentinel實現(xiàn)哨兵模式搭建小結(jié)
- 玩轉(zhuǎn)Redis搭建集群之Sentinel詳解
- 解決redis sentinel 頻繁主備切換的問題