依赖坐标:
<!-- org.redisson/redisson 分布式专用-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.16.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!--<version>2.5.3</version>-->
</dependency>
redis模板工具类:
package com.inesa.shwater.adapter.lock;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* 定义RedisTemplate模板
* @return
* @author 4-13 14:50
*/
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
// Json序列化配置
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// String序列化配置
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
redisTemplate.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
注入bean:
@Bean
public Redisson redisson(){
// 单机模式
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379").setDatabase(0);
return (Redisson) Redisson.create(config);
}
@Resource
private RedisTemplate<String,Integer> redisTemplate;
@Autowired
private Redisson redisson;
@GetMapping("/test02")
@ApiOperation("测试02")
@Scheduled(cron = "0 12 22 * * ? ")
public void test02(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String nowTime = simpleDateFormat.format(new Date());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date time = calendar.getTime();
String tomorrow = simpleDateFormat.format(time);
String key = nowTime;
String lock = "lock";
RLock redissonLock = redisson.getLock(lock);
try {
redissonLock.lock(30, TimeUnit.SECONDS);
Integer stock = redisTemplate.opsForValue().get(key);
if (stock!=null &&stock>0){
int realStock = stock - 1;
log.info("今天执行成功了....");
redisTemplate.opsForValue().set(key,realStock);
redisTemplate.delete(key);
}else {
log.info("今天已经执行过了,无需重复执行");
}
} finally {
// 释放锁
// 添加明天的key值
redisTemplate.opsForValue().set(tomorrow,1);
redissonLock.unlock();
}
}