1 changed files with 113 additions and 0 deletions
@ -0,0 +1,113 @@ |
|||||
|
package com.dreamchaser.depository_manage.security.pool; |
||||
|
|
||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect; |
||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
||||
|
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
||||
|
import org.springframework.data.redis.core.RedisTemplate; |
||||
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
||||
|
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; |
||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer; |
||||
|
import javax.annotation.PostConstruct; |
||||
|
import java.nio.charset.Charset; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Configuration |
||||
|
public class RedisPool { |
||||
|
//redis地址
|
||||
|
@Value("${redisPool.host}") |
||||
|
private String host; |
||||
|
|
||||
|
//redis端口号
|
||||
|
@Value("${redisPool.port}") |
||||
|
private int port; |
||||
|
|
||||
|
//redis密码
|
||||
|
@Value("${redisPool.password}") |
||||
|
private String password; |
||||
|
|
||||
|
//默认数据库
|
||||
|
private int defaultDB; |
||||
|
|
||||
|
//多个数据库集合
|
||||
|
@Value("${redisPool.dbs}") |
||||
|
private List<Integer> dbList; |
||||
|
|
||||
|
//RedisTemplate实例
|
||||
|
private static Map<Integer, RedisTemplate<String, String>> redisTemplateMap = new HashMap<>(); |
||||
|
|
||||
|
/** |
||||
|
* 初始化连接池 |
||||
|
*/ |
||||
|
@PostConstruct |
||||
|
public void initRedisTemplate() { |
||||
|
defaultDB = dbList.get(0);//设置默认数据库
|
||||
|
for (Integer db : dbList) { |
||||
|
//存储多个RedisTemplate实例
|
||||
|
redisTemplateMap.put(db, redisTemplate(db)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public LettuceConnectionFactory redisConnection(int db) { |
||||
|
RedisStandaloneConfiguration server = new RedisStandaloneConfiguration(); |
||||
|
server.setHostName(host); // 指定地址
|
||||
|
server.setDatabase(db); // 指定数据库
|
||||
|
server.setPort(port); //指定端口
|
||||
|
server.setPassword(password); //指定密码
|
||||
|
LettuceConnectionFactory factory = new LettuceConnectionFactory(server); |
||||
|
factory.afterPropertiesSet(); //刷新配置
|
||||
|
return factory; |
||||
|
} |
||||
|
|
||||
|
//RedisTemplate模板
|
||||
|
public RedisTemplate<String, String> redisTemplate(int db) { |
||||
|
|
||||
|
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); |
||||
|
redisTemplate.setConnectionFactory(redisConnection(db)); |
||||
|
|
||||
|
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(StandardCharsets.UTF_8); |
||||
|
// key 的序列化规则
|
||||
|
redisTemplate.setKeySerializer(stringRedisSerializer); |
||||
|
//String的序列化
|
||||
|
|
||||
|
//key采用String的序列化方式
|
||||
|
redisTemplate.setKeySerializer(stringRedisSerializer); |
||||
|
//hash的key采用String的序列化方式
|
||||
|
redisTemplate.setHashKeySerializer(stringRedisSerializer); |
||||
|
//value序列化方式采用String
|
||||
|
redisTemplate.setValueSerializer(stringRedisSerializer); |
||||
|
//hash序列化方式采用String
|
||||
|
redisTemplate.setHashValueSerializer(stringRedisSerializer); |
||||
|
|
||||
|
|
||||
|
redisTemplate.afterPropertiesSet(); |
||||
|
|
||||
|
return redisTemplate; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 指定数据库进行切换 |
||||
|
* @param db 数据库索引 |
||||
|
* @return |
||||
|
*/ |
||||
|
public RedisTemplate<String, String> getRedisTemplateByDb(int db) { |
||||
|
return redisTemplateMap.get(db); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 使用默认数据库 |
||||
|
* |
||||
|
* @return |
||||
|
*/ |
||||
|
public RedisTemplate<String, String> getRedisTemplate() { |
||||
|
return redisTemplateMap.get(defaultDB); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue