SpringBoot 集成Redis(哨兵模式)

一、引入pom依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-redisartifactId>
    <version>1.4.1.RELEASEversion>
dependency>

二、配置属性文件

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.sentinel.master=master
springredis.sentinel.nodes=1.1.1.1:26379,1.1.1.2:26379
spring.redis.sentinel.password=123456

#连接池最大链接数默认值为8
spring.redis.lettuce.pool.max-active=8
#连接池最大阻塞时间(使用负值表示没有限制)默认为-1
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接数 默认为8
spring.redis.lettuce.pool.max-idle=8
#连接池中的最小空闲连接数 默认为8
spring.redis.lettuce.pool.min-idle=0

三、配置redis(有时项目要求配置文件密码需要加密)@Componet @ConfigurationProperties(prefix="")

@Configuration
public class RedisConfigure {
	@Autowired
	private Environment environmet;
	
	@Bean("lettuceConnectionFactory")
	public RedisConnectionFactory lettuceConnectionFactory() {
		String master = environmet.getProperty("");
		Set<String> nodes = StringUtils.commaDelimitedListToSet(environmet.getProperty(""));
		String password = environmet.getProperty("");

		RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration(master, nodes);
		sentinelConfig.setPassword(RedisPassword.of(password));
		return new LettuceConnectionFactory(sentinelConfig);
	}

	@Bean("redisTemplate")
	public RedisTemplate redisTemplate(@Qualifier("lettuceConnectionFactory") RedisConnectionFactory  factory) {
		RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		return redisTemplate;
	}
}

你可能感兴趣的:(后端,redis,spring,boot,spring,java,jedis)