1、自定义redisConfig , 如果项目中要使用redis事务,最好将用事务和不用事务的redis分开。
@Configuration
public class RedisConfig {@Resourceprivate 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@Primarypublic 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());}@Beanpublic 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(); //取消