redis之哨兵 springboot配置
转载自https://blog.csdn.net/m0_37367413/article/details/82018125
springboot整合redis哨兵方式配置
1.springboot工程引入redis
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-redis</artifactId>
-
</dependency>
2.application.properties填写配置信息
-
####redis的配置信息###
-
spring.redis.sentinel.master=mymaster
-
spring.redis.sentinel.nodes=host1:port1,host2:port2,...
-
spring.redis.password=password......
-
#采用哪个数据库
-
spring.redis.database=0
-
# 连接池最大连接数,默认8个,(使用负值表示没有限制)
-
spring.redis.pool.max-active=8
-
# 连接池最大阻塞等待时间(使用负值表示没有限制)
-
spring.redis.pool.max-wait=-1
-
# 连接池中的最大空闲连接
-
spring.redis.pool.max-idle=8
-
# 连接池中的最小空闲连接
-
spring.redis.pool.min-idle=0
-
# 连接超时时间(毫秒)
-
spring.redis.timeout=0
3.注入使用
-
-
private RedisTemplate redisTemplate;
4.自定义redis序列化
-
import com.fasterxml.jackson.annotation.JsonAutoDetect;
-
import com.fasterxml.jackson.annotation.PropertyAccessor;
-
import com.fasterxml.jackson.databind.ObjectMapper;
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
-
import org.springframework.data.redis.core.RedisTemplate;
-
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
-
-
/**
-
* 〈redis模板的相应配置自定义序列化〉
-
* @create 2018/8/24
-
* @since 1.0.0
-
*/
-
-
public class RedisConfig {
-
-
-
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory redisConnectionFactory){
-
-
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
-
redisTemplate.setConnectionFactory(redisConnectionFactory);
-
-
//自定义序列化方式
-
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
-
ObjectMapper objectMapper = new ObjectMapper();
-
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
-
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
-
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
-
-
redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
-
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
-
redisTemplate.setHashKeySerializer(jackson2JsonRedisSerializer);
-
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
-
redisTemplate.afterPropertiesSet();
-
-
-
return redisTemplate;
-
}
-
-
}
5.采用自己的模板
-
-
private RedisTemplate<String,Object> redisTemplate;
搞定。。。。。。