使用springboot自带redis配置redisTemplate的单例模式和哨兵模式

文章目录

  • 1 配置文件
  • 2 配置模板
    • 2.1 使用Jackson2JsonRedisSerializer的模板
    • 2.2 使用GenericJackson2JsonRedisSerializer的模板
  • 3 配置模板的工厂方法
    • 3.1 单例模式的工厂方法
    • 3.2 哨兵模式的工厂方法

1 配置文件

application.properties

#哨兵监听的Redis Server的名称
sentinel.master=mySelfMaster
#哨兵模式的配置host列表用 , 隔开
sentinel.nodes=*.*.*.80:27000,*.*.*.81:27000,*.*.*.82:27000
#单例模式的redis的host
host=127.0.0.1
password=
port=6379
#最大分配对象数
pool.maxTotal=1024
#最大能够保持闲置的对象数
pool.maxIdle=50
#当池内没有返回对象时,最大等待时间
pool.maxWait=1000

2 配置模板

如下两种的配置模板方法,无论是单例模式还是哨兵模式都可以使用,不过更推荐第二种,因为sona扫描不会报bug没解决

2.1 使用Jackson2JsonRedisSerializer的模板

@Configuration
@Slf4j
public class RedisConfig(){
	//配置模板
	@Bean
	public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
		RedisTemplate<Object,Object> template= new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		//使用Jackson2JsonRedisSerializer来序列化和反序列话redis的value值
		Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
		ObjectMapper mapper= new ObjectMapper();
		mapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
		//如果包序列化那里的错误可以把此句话注释掉
		mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		redisSerializer.setObjectMapper(mapper);
		template.setValueSerializer(redisSerializer);
		//使用StringRedisSerializer来序列化和反序列化redis的key值
		template.setKeySerializer(new StringRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		template.afterPropertiesSet();
		return tempalte;
	}
}

2.2 使用GenericJackson2JsonRedisSerializer的模板

@Configuration
@Slf4j
public class RedisConfig(){
	
	//配置模板
	@Bean
	public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
		RedisTemplate<Object,Object> template= new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		//使用GenericJackson2JsonRedisSerializer来序列化和反序列话redis的value值
		GenericJackson2JsonRedisSerializer redisSerializer = new GenericJackson2JsonRedisSerializer();
		template.setValueSerializer(redisSerializer);
		//使用StringRedisSerializer来序列化和反序列化redis的key值
		template.setKeySerializer(new StringRedisSerializer());
		template.setHashKeySerializer(new StringRedisSerializer());
		template.afterPropertiesSet();
		return tempalte;
	}
}

3 配置模板的工厂方法

3.1 单例模式的工厂方法

@Configuration
@Slf4j
public class RedisConfig(){
	@Value("${hosts}")
	private String host;
	@Value("${port}")
	private int port;
	@Value("${password}")
	private String password;
	//配置工厂
	@Bean
	public LettuceConnectionFactory lettuceConnectionFactory (){
		RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
		redisStandaloneConfiguration.setDatabase(0);
		redisStandaloneConfiguration.setHostName(host);
		redisStandaloneConfiguration.setPort(port);
		redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
		LettuceClientConfiguration.LettuceClientConfigurationBuilder lettuceClientConfigurationBuilder =
				LettuceClientConfiguration.builder();
		LettuceConnectionFactory factory = 
				new LettuceConnectionFactory(redisStandaloneConfiguration,LettuceClientConfigurationBuilder.build());
		return factory;
	}
}

3.2 哨兵模式的工厂方法

@Configuration
@Slf4j
public class RedisConfig(){
	@Value("${sentinel.nodes}")
	private String nodes;
	@Value("${sentinel.master}")
	private String master;
	@Value("${password}")
	private String password;
	@Value("${pool.maxIdle}")
	private String maxIdle;
	@Value("${pool.maxTotal}")
	private String maxTotal;

	//配置工厂
	@Bean
	public LettuceConnectionFactory lettuceConnectionFactory (){
		Set<String> host = new HashSet<>();
		List<String> strings = Arrays.asList(nodes.split(","));
		host.addAll(strings);
		RedisSentinelConfiguration redisSentinelConfiguration =new RedisSentinelConfiguration(master,host);
		redisSentinelConfiguration.setPassword(password);
		//redis 线程池配置
		GenericObejctPoolConfig poolConfig = new GenericObejctPoolConfig();
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMaxTotal(maxTotal);
		LettuceClientConfiguration lettuceClientConfiguration =
				LettuceClientConfiguration.builder().poolConfig(poolConfig).build();
		LettuceConnectionFactory factory = 
				new LettuceConnectionFactory(redisSentinelConfiguration,lettuceClientConfiguration);
		return factory;
	}
}

你可能感兴趣的:(redis)