springboot2整合redis集群的三种方式
pom
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 默认
#系统默认连接池,这种方式 redisTemplate 可直接使用默认,在使用的地方直接注入即可
spring.redis.cluster.nodes=192.168.100.1:6379,192.168.100.1:6380,192.168.100.2:6379,192.168.100.2:6380,192.168.100.3:6379,192.168.100.3:6380
spring.redis.cluster.max-redirects=3
spring.redis.pool.max-active=8
spring.redis.poll.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout=6000
@Autowired
private RedisTemplate<String, String> redisTemplate;
@RequestMapping("/hello")
public Object hello() {
redisTemplate.opsForValue().set("1","111");
Object result = redisTemplate.opsForValue().get("1");
return result;
}
- jedis
#使用jedis连接池,需要连接池注入配置信息,在使用的地方直接注入即可
spring.redis.cluster.nodes=192.168.100.1:6379,192.168.100.1:6380,192.168.100.2:6379,192.168.100.2:6380,192.168.100.3:6379,192.168.100.3:6380
spring.redis.cluster.max-redirects=3
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=6000
@Configuration
public class RedisJedisClusterConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@RequestMapping("/hello")
public Object hello() {
redisTemplate.opsForValue().set("1","111");
Object result = redisTemplate.opsForValue().get("1");
return result;
}
- lettcue
#使用lettuce连接池,需要连接池注入配置信息,在使用的地方直接注入即可
spring.redis.cluster.nodes=192.168.100.1:6379,192.168.100.1:6380,192.168.100.2:6379,192.168.100.2:6380,192.168.100.3:6379,192.168.100.3:6380
spring.redis.cluster.max-redirects=3
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.min-idle=0
spring.redis.timeout=6000
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisLettuceClusterConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@RequestMapping("/hello")
public Object hello() {
redisTemplate.opsForValue().set("1","111");
Object result = redisTemplate.opsForValue().get("1");
return result;
}
使用时可选择切换不同的方式,经测试ok。
本文详细介绍了Spring Boot 2中如何通过默认配置、Jedis连接池和Lettuce连接池实现Redis集群的整合,包括各自的配置和使用方法,并分享了切换不同方式的实践经验。
845

被折叠的 条评论
为什么被折叠?



