本文介绍: 1、自定义redisConfig , 如果项目中要使用redis事务,最好将用事务和不用事务的redis分开。
1、自定义redisConfig , 如果项目中要使用redis事务,最好将用事务和不用事务的redis分开。
@Configuration
public class RedisConfig {
@Resource
private RedisProperties redisProperties;
@Bean("redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redis1ConnectionFactory());
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(genericJackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Bean("redisTransactionTemplate")
public RedisTemplate<String, Object> redisTransactionTemplate() {
RedisTemplate<String, Object> redisTransactionTemplate = new RedisTemplate<>();
redisTransactionTemplate.setConnectionFactory(redis2ConnectionFactory());
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTransactionTemplate.setKeySerializer(new StringRedisSerializer());
redisTransactionTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
redisTransactionTemplate.setHashKeySerializer(genericJackson2JsonRedisSerializer);
redisTransactionTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
redisTransactionTemplate.afterPropertiesSet();
//开启事务
redisTransactionTemplate.setEnableTransactionSupport(true);
return redisTransactionTemplate;
}
@Bean
@Primary
public RedisConnectionFactory redis1ConnectionFactory() {
RedisStandaloneConfiguration localhost = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
if(StrUtil.isNotEmpty(redisProperties.getPassword())){
localhost.setPassword(redisProperties.getPassword());
}
if(Objects.nonNull(redisProperties.getDatabase())){
localhost.setDatabase(redisProperties.getDatabase());
}
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb =
(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
jedisPoolConfig.setEvictorShutdownTimeoutMillis(redisProperties.getTimeout());
jpb.poolConfig(jedisPoolConfig);
return new JedisConnectionFactory(localhost,jpb.build());
}
@Bean
public RedisConnectionFactory redis2ConnectionFactory() {
RedisStandaloneConfiguration localhost = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
if(StrUtil.isNotEmpty(redisProperties.getPassword())){
localhost.setPassword(redisProperties.getPassword());
}
if(Objects.nonNull(redisProperties.getDatabase())){
localhost.setDatabase(redisProperties.getDatabase());
}
JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb =
(JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
jedisPoolConfig.setEvictorShutdownTimeoutMillis(redisProperties.getTimeout());
jpb.poolConfig(jedisPoolConfig);
return new JedisConnectionFactory(localhost,jpb.build());
}
}
@Order(value = 1)
@Data
@Component
public class RedisProperties {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private Integer port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.database}")
private Integer database;
@Value("${spring.redis.timeout}")
private Integer timeout;
@Value("${spring.redis.lettuce.pool.max-idle}")
public Integer maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
public Integer minIdle;
@Value("${spring.redis.lettuce.pool.max-active}")
public Integer maxActive;
@Value("${spring.redis.lettuce.pool.max-wait}")
public Integer maxWait;
}
需要事务的注入
@Resource(name = "redisTransactionTemplate")
不需要事务的注入
@Resource(name = "redisTemplate")
事务完整执行流程
redisTransactionTemplate.multi(); //开启
redisTransactionTemplate.exec(); //执行
redisTransactionTemplate.discard(); //取消
原文地址:https://blog.csdn.net/lyp131422/article/details/136027291
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_66617.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。