application.properties配置
#redis 多数据源
redis.host=192.168.50.21
redis.port=6379
#indexdb
redis.db_phrase_index=3
#indexdb
redis.db_hash_index=4
#indexdb
redis.db_iflytek_index=5
@Configuration
public class RedisDataSourceConfig {
@Value("${redis.host}")
private String redisHost;
@Value("${redis.port}")
private int redisPort;
@Value("${redis.db_phrase_index}")
private int phraseIndex;
@Value("${redis.db_hash_index}")
private int hashIndex;
@Value("${redis.db_iflytek_index}")
private int iflytekIndex;
//保存指定分词
@Primary
@Bean
public JedisConnectionFactory phraseConnectionFactory(){
JedisConnectionFactory jedisConnectionFactory = getJedisConnectionFactory();
jedisConnectionFactory.setDatabase(phraseIndex);
return jedisConnectionFactory;
}
@Bean
public StringRedisTemplate redisTemplate(@Qualifier("phraseConnectionFactory") RedisConnectionFactory cf) {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(cf);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
return redisTemplate;
}
//保存hash分词
@Bean
public JedisConnectionFactory hashConnectionFactory(){
JedisConnectionFactory jedisConnectionFactory = getJedisConnectionFactory();
jedisConnectionFactory.setDatabase(hashIndex);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> hRedisTemplate(@Qualifier("hashConnectionFactory") RedisConnectionFactory cf) {
return getRedisTemplate(cf);
}
//保存iflytekIndex
@Bean
public JedisConnectionFactory iflytekConnectionFactory(){
JedisConnectionFactory jedisConnectionFactory = getJedisConnectionFactory();
jedisConnectionFactory.setDatabase(iflytekIndex);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> flyRedisTemplate(@Qualifier("iflytekConnectionFactory") RedisConnectionFactory cf) {
return getRedisTemplate(cf);
}
//公共方法
public JedisConnectionFactory getJedisConnectionFactory(){
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
// jedisConnectionFactory.setPassword(pwd);
jedisConnectionFactory.setHostName(redisHost);
jedisConnectionFactory.setPort(redisPort);
return jedisConnectionFactory;
}
public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory cf){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(cf);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
return redisTemplate;
}
}