Redis 從入門到精通【應(yīng)用篇】之SpringBoot Redis 配置哨兵模式 Lettuce 和Jedis

前言
在Spring Boot 中配置Redis哨兵模式,可以使用 Lettuce 或 Jedis 配置 Redis 哨兵模式,實(shí)現(xiàn)高可用的 Redis 集群。
因?yàn)樵赟pring Boot中spring-boot-starter-data-redis
已經(jīng)實(shí)現(xiàn)了兩種客戶端鏈接方式,我們選擇其中一種就可以。
Lettuce和Jedis區(qū)別
Lettuce 和 Jedis 都是 Redis 客戶端庫,可以與 Redis 服務(wù)器進(jìn)行通信。它們的區(qū)別在于:
1. 連接方式
- Lettuce 采用 Netty 底層網(wǎng)絡(luò)框架,使用異步非阻塞式 IO 模型,支持多個 Redis 節(jié)點(diǎn)的連接池復(fù)用,適合高并發(fā)場景。
- Jedis 采用傳統(tǒng)的阻塞 IO 模型,每個 Redis 節(jié)點(diǎn)需要維護(hù)一個連接池,適合低并發(fā)場景。
2. 線程安全性
- Lettuce 是線程安全的,可以在多線程環(huán)境下共享連接,而 Jedis 不是線程安全的,需要使用連接池進(jìn)行連接復(fù)用。
在 Spring Boot 中配置 Redis 哨兵模式使用 Lettuce 和Jedis 作為 Redis 客戶端的步驟分別如下。
推薦使用Lettuce 方式配置。
教程如下
1. Lettuce 方式配置
1.1. 添加 Redis 和 Lettuce 依賴
在 pom.xml 文件中添加 Redis 和 Lettuce 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
因?yàn)槟J(rèn)情況下Spring-boot-starter-data-redis
已經(jīng)引入了lettuce
所以我們不需要再引入客戶端lettuce。只需要引入上面的Starter就OK。
1.2. 配置 Redis 哨兵模式
在 application.properties 或 application.yml 文件中添加 Redis 的配置信息:
兩種配置方式我都羅列出來
# Redis 主節(jié)點(diǎn)的名稱
spring.redis.sentinel.master=your-master-name
# Redis 哨兵節(jié)點(diǎn)的地址,多個節(jié)點(diǎn)之間使用逗號分隔
spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
# Redis 訪問密碼
spring.redis.password=your-redis-password
# Lettuce 連接池最大連接數(shù)
spring.redis.lettuce.pool.max-active=8
# Lettuce 連接池最大空閑連接數(shù)
spring.redis.lettuce.pool.max-idle=8
# Lettuce 連接池最小空閑連接數(shù)
spring.redis.lettuce.pool.min-idle=0
# Lettuce 連接池最大等待時間
spring.redis.lettuce.pool.max-wait=5000ms
或者使用yaml配置方式,要看大家已有項(xiàng)目的配置方式,保持一致。
spring:
redis:
sentinel:
master: your-master-name
nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
password: your-redis-password
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 5000ms
配置解釋一下 此處的配置中帶Lettuce 的事SpringBoot 配置Redis 使用Lettuce 的專項(xiàng)配置。不帶的事公共配置。
-
spring.redis.sentinel.master
:Redis 主節(jié)點(diǎn)的名稱,用于訪問 Redis 哨兵獲取主節(jié)點(diǎn)的信息。 -
spring.redis.sentinel.nodes
:Redis 哨兵節(jié)點(diǎn)的地址,多個節(jié)點(diǎn)之間使用逗號分隔。 -
spring.redis.password
:Redis 訪問密碼,用于連接 Redis 時進(jìn)行認(rèn)證。 -
spring.redis.lettuce.pool.max-active
:Lettuce 連接池最大連接數(shù),用于限制連接池中的最大連接數(shù)。 -
spring.redis.lettuce.pool.max-idle
:Lettuce 連接池最大空閑連接數(shù),用于限制連接池中的最大空閑連接數(shù)。 -
spring.redis.lettuce.pool.min-idle
:Lettuce 連接池最小空閑連接數(shù),用于限制連接池中的最小空閑連接數(shù)。 -
spring.redis.lettuce.pool.max-wait
:Lettuce 連接池最大等待時間,用于限制獲取連接的最大等待時間。
其實(shí)配置完上面的配置基本上就是可以使用了,因?yàn)镾pring Boot的實(shí)例化Redis客戶端的順序是先判斷是否哨兵Sentinel
模式,再接下來Cluster
模式,接下來才是單機(jī)模式Standalone
.
1.3. 創(chuàng)建自定義 RedisConnectionFactory以及RedisTemplate
在 Spring Boot 中,可以通過 RedisConnectionFactory 來連接 Redis。在配置文件中配置 RedisConnectionFactory 的相關(guān)信息后,可以通過在代碼中注入 RedisConnectionFactory Bean 來使用 Redis。其實(shí)也可以直接使用默認(rèn)的Bean。默認(rèn)的在LettuceConnectionConfiguration
中已經(jīng)配置,如果需要自定義配置可以使用以下方式。
我們定義了一個 RedisConfig 類,并創(chuàng)建了一個 RedisTemplate 的 Bean。在 RedisConfig 類中,我們注入了 RedisProperties 類,并將 Redis 的配置信息通過 RedisSentinelConfiguration的方式配置到 LettuceConnectionFactory 中,然后再將 LettuceConnectionFactory 注入到 RedisTemplate 中。
@Configuration
public class RedisConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration()
.master(redisProperties.getSentinel().getMaster())
.sentinel(redisProperties.getSentinel().getNodes());
LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
.commandTimeout(Duration.ofMillis(redisProperties.getTimeout()))
.poolConfig(new GenericObjectPoolConfig<>())
.build();
return new LettuceConnectionFactory(sentinelConfiguration, clientConfiguration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
2. Jedis 方式配置
2.1. 添加 Redis 和 Jedis 依賴
Jedis 和Lettuce 的方式的區(qū)別,Lettuce 是在Redis Spring Boot Redis依賴中內(nèi)置了Lettuce 的依賴,但jedis 方式雖Maven然引入了,但是可選項(xiàng)。所以需要手動引入
在 pom.xml 文件中添加 Redis 和 Jedis 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.2. 配置 Redis 哨兵模式
在 application.properties 或 application.yml 文件中添加 Redis 的配置信息:
spring.redis.sentinel.master=your-master-name
spring.redis.sentinel.nodes=redis-host1:port,redis-host2:port,redis-host3:port
spring.redis.password=your-redis-password
spring:
redis:
sentinel:
master: your-master-name
nodes: 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
password: your-redis-password
其中,your-master-name
是 Redis 主節(jié)點(diǎn)的名稱,redis-host1:port,redis-host2:port,redis-host3:port
是 Redis 哨兵節(jié)點(diǎn)的地址,多個節(jié)點(diǎn)之間使用逗號分隔。
2.3. 創(chuàng)建自定義 RedisTemplate
此處為可選項(xiàng),也可以使用Spring Boot 默認(rèn)的RedisTemplate 。
和上面Lettuce的方式類似。我們注入了 RedisProperties
類,并將 Redis 的配置信息通過 RedisSentinelConfiguration
的方式配置到 JedisConnectionFactory
中,然后再將 JedisConnectionFactory
注入到 RedisTemplate 中。
@Configuration
public class RedisConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master(redisProperties.getSentinel().getMaster())
.sentinel(redisProperties.getSentinel().getNodes());
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig);
connectionFactory.setPassword(redisProperties.getPassword());
connectionFactory.setUsePool(true);
return connectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
上面這兩種方式基本上就配置好了,具體如何使用,可參考 《Redis【應(yīng)用篇】之RedisTemplate基本操作》。文章來源:http://www.zghlxwxcb.cn/news/detail-603251.html
3. 總結(jié)
使用 Lettuce 或 Jedis 配置 Redis 哨兵模式的配置方法類似,都需要在配置文件中指定 Redis 哨兵的主節(jié)點(diǎn)名稱和地址端口號等參數(shù),然后創(chuàng)建相應(yīng)的 RedisConnectionFactory Bean,并使用 RedisTemplate 進(jìn)行 Redis 操作。Lettuce 適合高并發(fā)、多線程的場景,具有更好的性能和穩(wěn)定性;Jedis 適合低并發(fā)、單線程的場景,使用起來更加簡單、方便。根據(jù)實(shí)際需求和場景,可以選擇使用 Lettuce 或 Jedis 來配置 Redis 哨兵模式。文章來源地址http://www.zghlxwxcb.cn/news/detail-603251.html
3. Redis從入門到精通系列文章
- 《Redis【應(yīng)用篇】之RedisTemplate基本操作》
- 《Redis 從入門到精通【實(shí)踐篇】之SpringBoot配置Redis多數(shù)據(jù)源》
- 《Redis 從入門到精通【進(jìn)階篇】之三分鐘了解Redis HyperLogLog 數(shù)據(jù)結(jié)構(gòu)》
- 《Redis 從入門到精通【進(jìn)階篇】之三分鐘了解Redis地理位置數(shù)據(jù)結(jié)構(gòu)GeoHash》
- 《Redis 從入門到精通【進(jìn)階篇】之高可用哨兵機(jī)制(Redis Sentinel)詳解》
- 《Redis 從入門到精通【進(jìn)階篇】之redis主從復(fù)制詳解》
- 《Redis 從入門到精通【進(jìn)階篇】之Redis事務(wù)詳解》
- 《Redis從入門到精通【進(jìn)階篇】之對象機(jī)制詳解》
- 《Redis從入門到精通【進(jìn)階篇】之消息傳遞發(fā)布訂閱模式詳解》
- 《Redis從入門到精通【進(jìn)階篇】之持久化 AOF詳解》
- 《Redis從入門到精通【進(jìn)階篇】之持久化RDB詳解》
- 《Redis從入門到精通【高階篇】之底層數(shù)據(jù)結(jié)構(gòu)字典(Dictionary)詳解》
- 《Redis從入門到精通【高階篇】之底層數(shù)據(jù)結(jié)構(gòu)快表QuickList詳解》
- 《Redis從入門到精通【高階篇】之底層數(shù)據(jù)結(jié)構(gòu)簡單動態(tài)字符串(SDS)詳解》
- 《Redis從入門到精通【高階篇】之底層數(shù)據(jù)結(jié)構(gòu)壓縮列表(ZipList)詳解》
-
《Redis從入門到精通【進(jìn)階篇】之?dāng)?shù)據(jù)類型Stream詳解和使用示例》
大家好,我是冰點(diǎn),今天的Redis【實(shí)踐篇】之SpringBoot Redis 配置多數(shù)據(jù)源,全部內(nèi)容就是這些。如果你有疑問或見解可以在評論區(qū)留言。
到了這里,關(guān)于SpringBoot Redis 使用Lettuce和Jedis配置哨兵模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!