前言
在实际springboot集成redis使用过程中,针对不同类型的业务数据,可能存在不同的dbindex中,例如token存储db0,redis全局锁存储dbindex1,需要我们对RedisTemplate操作进行扩展,支持单次操作不同的dbindex
方案
系统加载时初始化根据redis使用库的dbindex,初始化对应个数的RedisTemplate,调用时根据dbindex获取对应的操作对象实例,本次实现是将15个db全部初始化
RedisRegist
初始化redis的Factory,线程池配置及RedisTemplate,StringRedisTemplate的Bean对象
public class RedisRegist implements EnvironmentAware,ImportBeanDefinitionRegistrar {
private static final Logger logger = LoggerFactory.getLogger(RedisRegist.class);
private static Map registerBean = new ConcurrentHashMap();
private Environment environment;
private Binder binder;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
this.binder = Binder.get(this.environment);
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RedisEntity redisEntity;
try {
redisEntity = binder.bind("spring.redis", RedisEntity.class).get();
} catch (NoSuchElementException e) {
logger.error("redis not setting.");
return;
}
boolean onPrimary = true;
//根据多个库实例化出多个连接池和Template
for (int i = 0; i lettuceConnectionFactorySupplier = () -> {
LettuceConnectionFactory factory = (LettuceConnectionFactory) registerBean.get("LettuceConnectionFactory" + database);
if (factory != null) {
return factory;
}
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
Duration shutdownTimeout = redisEntity.getLettuce().getShutdownTimeout();
if(shutdownTimeout == null){
shutdownTimeout = redisEntity.getTimeout();
}
if (shutdownTimeout != null) {
builder.shutdownTimeout(shutdownTimeout);
}
LettuceClientConfiguration clientConfiguration = builder.poolConfig(genericObjectPoolConfig).build();
factory = new LettuceConnectionFactory(configuration, clientConfiguration);
registerBean.put("LettuceConnectionFactory" + database, factory);
return factory;
};
LettuceConnectionFactory lettuceConnectionFactory = lettuceConnectionFactorySupplier.get();
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(LettuceConnectionFactory.class, lettuceConnectionFactorySupplier);
AbstractBeanDefinition factoryBean = builder.getRawBeanDefinition();
factoryBean.setPrimary(onPrimary);
registry.registerBeanDefinition("lettuceConnectionFactory" + database, factoryBean);
// StringRedisTemplate
GenericBeanDefinition stringRedisTemplate = new GenericBeanDefinition();
stringRedisTemplate.setBeanClass(StringRedisTemplate.class);
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
constructorArgumentValues.addIndexedArgumentValue(0, lettuceConnectionFactory);
stringRedisTemplate.setConstructorArgumentValues(constructorArgumentValues);
stringRedisTemplate.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
registry.registerBeanDefinition("stringRedisTemplate" + database, stringRedisTemplate);
// 定义RedisTemplate对象
GenericBeanDefinition redisTemplate = new GenericBeanDefinition();
redisTemplate.setBeanClass(RedisTemplate.class);
redisTemplate.getPropertyValues().add("connectionFactory", lettuceConnectionFactory);
redisTemplate.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
RedisSerializer stringSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// key采用String的序列化方式,value采用json序列化方式
redisTemplate.getPropertyValues().add("keySerializer",new StringRedisSerializer());
redisTemplate.getPropertyValues().add("hashKeySerializer",stringSerializer);
redisTemplate.getPropertyValues().add("valueSerializer",jackson2JsonRedisSerializer);
redisTemplate.getPropertyValues().add("hashValueSerializer",stringSerializer);
//注册Bean
registry.registerBeanDefinition("redisTemplate" + database, redisTemplate);
//logger.info("Registration redis ({}) !", database);
if (onPrimary) {
onPrimary = false;
}
}
}
}
RedisManage
RedisTemplate,StringRedisTemplate的Bean对象统一管理定义
public class RedisManage {
private Map redisTemplateMap;
private Map stringRedisTemplateMap;
public RedisManage(Map redisTemplateMap,
Map stringRedisTemplateMap) {
this.redisTemplateMap = redisTemplateMap;
this.stringRedisTemplateMap = stringRedisTemplateMap;
}
public RedisTemplate redisTemplate(int dbIndex) {
RedisTemplate redisTemplate = redisTemplateMap.get("redisTemplate" + dbIndex);
return redisTemplate;
}
public StringRedisTemplate stringRedisTemplate(int dbIndex) {
StringRedisTemplate stringRedisTemplate = stringRedisTemplateMap.get("stringRedisTemplate" + dbIndex);
stringRedisTemplate.setEnableTransactionSupport(true);
return stringRedisTemplate;
}
public Map getRedisTemplateMap() {
return redisTemplateMap;
}
public Map getStringRedisTemplateMap() {
return stringRedisTemplateMap;
}
}
RedisConfig
将操作类Bean对象注入道RedisManage中
@AutoConfigureBefore({RedisAutoConfiguration.class})
@Import(RedisRegist.class)
@Configuration
public class RedisConfig implements EnvironmentAware, ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
private static String key1 = "redisTemplate";
private static String key2 = "stringRedisTemplate";
Map redisTemplateMap = new HashMap();
Map stringRedisTemplateMap = new HashMap();
private Binder binder;
private Environment environment;
private ApplicationContext applicationContext;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
this.binder = Binder.get(this.environment);
}
@PostConstruct
public Map initRedisTemplate(){
RedisEntity redisEntity;
try {
redisEntity = binder.bind("spring.redis", RedisEntity.class).get();
} catch (NoSuchElementException e) {
throw new RuntimeException("Failed to configure spring.redis: 'spring.redis' attribute is not specified and no embedded redis could be configured.");
}
for (int i=0;i
RedisUtil
调用RedisManage封装具体相关操作调用
@Component
public class RedisUtil {
@Autowired
private RedisManage redisManage;
private int defatuDBIndex= 0;
public void redisTemplateSet(String key, Object value){
ValueOperations
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
部分内容为互联网分享,若有侵权请联系站长删除。
THE END
暂无评论内容