SpringBoot2.x 整合Redis 哨兵模式
SpringBoot2.0 預設使用Lettuce作為Redis客戶端,在使用Lettuce時,需要額外引入commons-pool2包。建立一個SpringBoot2.x 專案之後,引入commons-pool2包。
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.2</version> </dependency>
配置Redis和Lettuce
spring: host: 127.0.0.1 port: 6380 password: test timeout: 6000 lettuce: pool: # 連線池最大連線數 max-active: 64 # 連線池最大空閒連線 max-idle: 32 # 連線池最小空閒連線 min-idle: 16 # 最大阻塞時間 max-wait: 3000 sentinel: master: master-redis nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
Spring容器啟動時,由於spring-boot-autoconfigure包中spring.factories檔案中的配置,會自動裝配RedisAutoConfiguration類。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration\
RedisAutoConfiguration類原始碼比較簡單,只是在自動注入時,先將Redis配置注入RedisProperties類,再例項化LettuceConnectionConfiguration類,最後在Miss RedisTemplate時建立兩個RedisTemplate例項。
@Configuration(
proxyBeanMethods = false
)
//spring-boot-starter-redis中已引入
@ConditionalOnClass({RedisOperations.class})
//將配置注入Properties類
@EnableConfigurationProperties({RedisProperties.class})
//預設僅建立LettuceConnectionConfiguration Bean,Jedis缺少包
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
在沒有自定義Redis連線池時,建立RedisConnectionFactory
@Bean
@ConditionalOnMissingBean(RedisConnectionFactory.class)
LettuceConnectionFactory redisConnectionFactory(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ClientResources clientResources) throws UnknownHostException {
LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(builderCustomizers, clientResources,
getProperties().getLettuce().getPool());
return createLettuceConnectionFactory(clientConfig);
}
LettuceClient裝配時,readFrom
預設為空,若需要配置讀寫分離,則需要將其重寫設定為readFrom = REPLICA_PREFERRED
,表明讀取從從庫中操作。
private LettuceClientConfiguration getLettuceClientConfiguration(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ClientResources clientResources, Pool pool) {
LettuceClientConfigurationBuilder builder = createBuilder(pool);
applyProperties(builder);
if (StringUtils.hasText(getProperties().getUrl())) {
customizeConfigurationFromUrl(builder);
}
builder.clientOptions(initializeClientOptionsBuilder().timeoutOptions(TimeoutOptions.enabled()).build());
builder.clientResources(clientResources);
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}
根據配置決定為Standalone、Sentinel、Cluster模式
private LettuceConnectionFactory createLettuceConnectionFactory(LettuceClientConfiguration clientConfiguration) {
if (getSentinelConfig() != null) {
return new LettuceConnectionFactory(getSentinelConfig(), clientConfiguration);
}
if (getClusterConfiguration() != null) {
return new LettuceConnectionFactory(getClusterConfiguration(), clientConfiguration);
}
return new LettuceConnectionFactory(getStandaloneConfig(), clientConfiguration);
}
參考自:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#redis